Ethereum PHP
PHP interface to Ethereum JSON-RPC API.
EthQTest.php
Go to the documentation of this file.
1 <?php
2 namespace Ethereum;
6 
12 class EthQTest extends TestStatic
13 {
21  public function testEthQ__simple()
22  {
23  $x = new EthQ(999);
24  $this->assertSame($x->val(), 999);
25  $this->assertSame($x->hexVal(), '0x00000000000000000000000000000000000000000000000000000000000003e7');
26  $this->assertSame($x->getSchemaType(), "Q");
27  }
28 
32  public function testEthQ__zero()
33  {
34 
35  $hex_null_short = new EthQ('0x');
36  $this->assertSame($hex_null_short->val(), 0);
37  $this->assertSame($hex_null_short->hexVal(), '0x0000000000000000000000000000000000000000000000000000000000000000');
38 
39  $hex_null = new EthQ('0x0');
40  $this->assertSame($hex_null->val(), 0);
41  $this->assertSame($hex_null->hexVal(), '0x0000000000000000000000000000000000000000000000000000000000000000');
42 
43  $int_null = new EthQ(0);
44  $this->assertSame($int_null->val(), 0);
45  $this->assertSame($int_null->hexVal(), '0x0000000000000000000000000000000000000000000000000000000000000000');
46  }
47 
51  public function testEthQ__one()
52  {
53 
54  $hex_one = new EthQ('0x0000000000000000000000000000000000000000000000000000000000000001');
55  $this->assertSame($hex_one->val(), 1);
56  $this->assertSame($hex_one->hexVal(), '0x0000000000000000000000000000000000000000000000000000000000000001');
57 
58  $int_one = new EthQ(1);
59  $this->assertSame($int_one->val(), 1);
60  $this->assertSame($int_one->hexVal(), '0x0000000000000000000000000000000000000000000000000000000000000001');
61 
62  }
63 
67  public function testEthQ__random()
68  {
69  $hex_number = new EthQ('0x000003e7');
70  $this->assertSame($hex_number->val(), 999);
71  $this->assertSame($hex_number->hexVal(), '0x00000000000000000000000000000000000000000000000000000000000003e7');
72  }
73 
77  public function testEthQ__negative()
78  {
79 
80  $x = new EthQ(-999);
81  $this->assertSame($x->val(), -999);
82  $this->assertSame($x->hexVal(), '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc19');
83 
84  // @deprecated Not supporting unpadded negative numbers anymore
85  // $y = new EthQ('0xfffffffffffffc19');
86  // $this->assertSame($y->val(), -999);
87  // $this->assertSame($y->hexVal(), '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc19');
88 
89  $z = new EthQ('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc19');
90  $this->assertSame($z->hexVal(), '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc19');
91  $this->assertSame($z->val(), -999);
92 
93  // You might still do the following for a unpadded negative hex number.
94  $unpadded = new Math_BigInteger('0xfffffffffffffc19', -16);
95  $y = new EthQ($unpadded->toString());
96  $this->assertSame($y->val(), -999);
97  $this->assertSame($y->hexVal(), '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc19');
98  }
99 
100  // Given ABI.
101 
105  public function testEthQ__given_abi8()
106  {
107  $x = new EthQ('0x000000000000000000000000000000000000000000000000000000000000270f', ['abi' => 'uint16']);
108  $this->assertSame($x->val(), 9999);
109  $this->assertSame($x->hexVal(), '0x000000000000000000000000000000000000000000000000000000000000270f');
110  }
111 
115  public function testEthQ__abi16()
116  {
117  $x = new EthQ('0x0000270f', ['abi' => 'uint16']);
118  $this->assertSame((int)$x->val(), 9999);
119  $this->assertSame($x->hexVal(), '0x000000000000000000000000000000000000000000000000000000000000270f');
120  }
121 
125  public function testEthQ__abi8()
126  {
127  $x = new EthQ(255);
128  $this->assertSame($x->val(), 255);
129  $this->assertSame($x->hexVal(), '0x00000000000000000000000000000000000000000000000000000000000000ff');
130  $this->assertSame($x->getAbi(), 'uint8');
131  }
132 
136  public function testEthQ__abi8_plus1()
137  {
138  $x = new EthQ(256);
139  $this->assertSame($x->val(), 256);
140  $this->assertSame($x->hexVal(), '0x0000000000000000000000000000000000000000000000000000000000000100');
141  $this->assertSame($x->getAbi(), 'uint16');
142  }
143 
147  public function testEthQ__abi8_neg()
148  {
149  $x = new EthQ(-255);
150  $this->assertSame($x->val(), -255);
151  $this->assertSame($x->hexVal(), '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff01');
152  $this->assertSame($x->getAbi(), 'int8');
153  }
154 
158  public function testEthQ__abi8_neg_plus1()
159  {
160  $x = new EthQ(-256);
161  $this->assertSame($x->val(), -256);
162  $this->assertSame($x->hexVal(), '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00');
163  $this->assertSame($x->getAbi(), 'int16');
164  }
165 
169  public function testEthQ__abi256()
170  {
171  $x = new EthQ('115792089237316000000000000000000000000000000000000000000000000000000000000000');
172  $this->assertSame($x->val(), '115792089237316000000000000000000000000000000000000000000000000000000000000000');
173  $this->assertSame($x->hexVal(), '0xffffffffffff86633a9e8f1256d61ed5325ebf2a4b4366ba0000000000000000');
174  }
175 
176  // Made TO FAIL
178  {
179  $this->expectException(\InvalidArgumentException::class);
180  new EthQ(9999, ['abi' => 'uint8']);
181  }
182 
183  public function testEthQ__abi256_plus1()
184  {
185  $this->expectException(\InvalidArgumentException::class);
186  new EthQ('115792089237316000000000000000000000000000000000000000000000000000000000000000115792089237316000000000000000000000000000000000000000000000000000000000000000');
187  }
188 }
189 
190 
191 /*
192 
193 NUMBER MAGIC CSV
194 
195 EXPONENT;2^EXPONENT;2^Exponent -1
196 8;256;255
197 16;65536;65535
198 24;16777216;16777215
199 32;4294967296;4294967295
200 40;1099511627776;1099511627775
201 48;281474976710656;281474976710655
202 56;72057594037927900;72057594037927900
203 64;18446744073709600000;18446744073709600000
204 72;4722366482869650000000;4722366482869650000000
205 80;1208925819614630000000000;1208925819614630000000000
206 88;309485009821345000000000000;309485009821345000000000000
207 96;79228162514264300000000000000;79228162514264300000000000000
208 104;20282409603651700000000000000000;20282409603651700000000000000000
209 112;5192296858534830000000000000000000;5192296858534830000000000000000000
210 120;1329227995784920000000000000000000000;1329227995784920000000000000000000000
211 128;340282366920938000000000000000000000000;340282366920938000000000000000000000000
212 136;87112285931760200000000000000000000000000;87112285931760200000000000000000000000000
213 144;22300745198530600000000000000000000000000000;22300745198530600000000000000000000000000000
214 152;5708990770823840000000000000000000000000000000;5708990770823840000000000000000000000000000000
215 160;1461501637330900000000000000000000000000000000000;1461501637330900000000000000000000000000000000000
216 168;374144419156711000000000000000000000000000000000000;374144419156711000000000000000000000000000000000000
217 176;95780971304118100000000000000000000000000000000000000;95780971304118100000000000000000000000000000000000000
218 184;24519928653854200000000000000000000000000000000000000000;24519928653854200000000000000000000000000000000000000000
219 192;6277101735386680000000000000000000000000000000000000000000;6277101735386680000000000000000000000000000000000000000000
220 200;1606938044258990000000000000000000000000000000000000000000000;1606938044258990000000000000000000000000000000000000000000000
221 208;411376139330302000000000000000000000000000000000000000000000000;411376139330302000000000000000000000000000000000000000000000000
222 216;105312291668557000000000000000000000000000000000000000000000000000;105312291668557000000000000000000000000000000000000000000000000000
223 224;26959946667150600000000000000000000000000000000000000000000000000000;26959946667150600000000000000000000000000000000000000000000000000000
224 232;6901746346790560000000000000000000000000000000000000000000000000000000;6901746346790560000000000000000000000000000000000000000000000000000000
225 240;1766847064778380000000000000000000000000000000000000000000000000000000000;1766847064778380000000000000000000000000000000000000000000000000000000000
226 248;452312848583266000000000000000000000000000000000000000000000000000000000000;452312848583266000000000000000000000000000000000000000000000000000000000000
227 256;115792089237316000000000000000000000000000000000000000000000000000000000000000;115792089237316000000000000000000000000000000000000000000000000000000000000000
228 */
testEthQ__given_abi8_should_be_wrong()
Definition: EthQTest.php:177
Definition: Abi.php:3