1ffe3c632Sopenharmony_ci<?php
2ffe3c632Sopenharmony_ci
3ffe3c632Sopenharmony_cirequire_once('test_util.php');
4ffe3c632Sopenharmony_ci
5ffe3c632Sopenharmony_ciuse Google\Protobuf\Internal\GPBType;
6ffe3c632Sopenharmony_ciuse Google\Protobuf\Internal\MapField;
7ffe3c632Sopenharmony_ciuse Foo\TestMessage;
8ffe3c632Sopenharmony_ciuse Foo\TestMessage\Sub;
9ffe3c632Sopenharmony_ci
10ffe3c632Sopenharmony_ciclass MapFieldTest extends \PHPUnit\Framework\TestCase {
11ffe3c632Sopenharmony_ci
12ffe3c632Sopenharmony_ci    #########################################################
13ffe3c632Sopenharmony_ci    # Test int32 field.
14ffe3c632Sopenharmony_ci    #########################################################
15ffe3c632Sopenharmony_ci
16ffe3c632Sopenharmony_ci    public function testInt32() {
17ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::INT32, GPBType::INT32);
18ffe3c632Sopenharmony_ci
19ffe3c632Sopenharmony_ci        // Test integer argument.
20ffe3c632Sopenharmony_ci        $arr[MAX_INT32] = MAX_INT32;
21ffe3c632Sopenharmony_ci        $this->assertSame(MAX_INT32, $arr[MAX_INT32]);
22ffe3c632Sopenharmony_ci        $arr[MIN_INT32] = MIN_INT32;
23ffe3c632Sopenharmony_ci        $this->assertSame(MIN_INT32, $arr[MIN_INT32]);
24ffe3c632Sopenharmony_ci        $this->assertEquals(2, count($arr));
25ffe3c632Sopenharmony_ci        $this->assertTrue(isset($arr[MAX_INT32]));
26ffe3c632Sopenharmony_ci        $this->assertTrue(isset($arr[MIN_INT32]));
27ffe3c632Sopenharmony_ci        unset($arr[MAX_INT32]);
28ffe3c632Sopenharmony_ci        unset($arr[MIN_INT32]);
29ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
30ffe3c632Sopenharmony_ci
31ffe3c632Sopenharmony_ci        // Test float argument.
32ffe3c632Sopenharmony_ci        $arr[1.9] = 1.9;
33ffe3c632Sopenharmony_ci        $arr[2.1] = 2.1;
34ffe3c632Sopenharmony_ci        $this->assertSame(1, $arr[1]);
35ffe3c632Sopenharmony_ci        $this->assertSame(2, $arr[2]);
36ffe3c632Sopenharmony_ci        $arr[MAX_INT32_FLOAT] = MAX_INT32_FLOAT;
37ffe3c632Sopenharmony_ci        $this->assertSame(MAX_INT32, $arr[MAX_INT32]);
38ffe3c632Sopenharmony_ci        $arr[MIN_INT32_FLOAT] = MIN_INT32_FLOAT;
39ffe3c632Sopenharmony_ci        $this->assertSame(MIN_INT32, $arr[MIN_INT32]);
40ffe3c632Sopenharmony_ci        $this->assertEquals(4, count($arr));
41ffe3c632Sopenharmony_ci        unset($arr[1.9]);
42ffe3c632Sopenharmony_ci        unset($arr[2.9]);
43ffe3c632Sopenharmony_ci        unset($arr[MAX_INT32_FLOAT]);
44ffe3c632Sopenharmony_ci        unset($arr[MIN_INT32_FLOAT]);
45ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
46ffe3c632Sopenharmony_ci
47ffe3c632Sopenharmony_ci        // Test string argument.
48ffe3c632Sopenharmony_ci        $arr['2'] = '2';
49ffe3c632Sopenharmony_ci        $this->assertSame(2, $arr[2]);
50ffe3c632Sopenharmony_ci        $arr['3.1'] = '3.1';
51ffe3c632Sopenharmony_ci        $this->assertSame(3, $arr[3]);
52ffe3c632Sopenharmony_ci        $arr[MAX_INT32_STRING] = MAX_INT32_STRING;
53ffe3c632Sopenharmony_ci        $this->assertSame(MAX_INT32, $arr[MAX_INT32]);
54ffe3c632Sopenharmony_ci        $this->assertEquals(3, count($arr));
55ffe3c632Sopenharmony_ci        unset($arr['2']);
56ffe3c632Sopenharmony_ci        unset($arr['3.1']);
57ffe3c632Sopenharmony_ci        unset($arr[MAX_INT32_STRING]);
58ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
59ffe3c632Sopenharmony_ci
60ffe3c632Sopenharmony_ci        // Test foreach.
61ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::INT32, GPBType::INT32);
62ffe3c632Sopenharmony_ci        for ($i = 0; $i < 3; $i++) {
63ffe3c632Sopenharmony_ci          $arr[$i] = $i;
64ffe3c632Sopenharmony_ci        }
65ffe3c632Sopenharmony_ci        $i = 0;
66ffe3c632Sopenharmony_ci        $arr_test = [];
67ffe3c632Sopenharmony_ci        foreach ($arr as $key => $val) {
68ffe3c632Sopenharmony_ci          $this->assertSame($key, $val);
69ffe3c632Sopenharmony_ci          $arr_test[] = $key;
70ffe3c632Sopenharmony_ci          $i++;
71ffe3c632Sopenharmony_ci        }
72ffe3c632Sopenharmony_ci        $this->assertTrue(isset($arr_test[0]));
73ffe3c632Sopenharmony_ci        $this->assertTrue(isset($arr_test[1]));
74ffe3c632Sopenharmony_ci        $this->assertTrue(isset($arr_test[2]));
75ffe3c632Sopenharmony_ci        $this->assertSame(3, $i);
76ffe3c632Sopenharmony_ci    }
77ffe3c632Sopenharmony_ci
78ffe3c632Sopenharmony_ci    #########################################################
79ffe3c632Sopenharmony_ci    # Test uint32 field.
80ffe3c632Sopenharmony_ci    #########################################################
81ffe3c632Sopenharmony_ci
82ffe3c632Sopenharmony_ci    public function testUint32() {
83ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::UINT32, GPBType::UINT32);
84ffe3c632Sopenharmony_ci
85ffe3c632Sopenharmony_ci        // Test integer argument.
86ffe3c632Sopenharmony_ci        $arr[MAX_UINT32] = MAX_UINT32;
87ffe3c632Sopenharmony_ci        $this->assertSame(-1, $arr[-1]);
88ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
89ffe3c632Sopenharmony_ci        unset($arr[MAX_UINT32]);
90ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
91ffe3c632Sopenharmony_ci
92ffe3c632Sopenharmony_ci        $arr[-1] = -1;
93ffe3c632Sopenharmony_ci        $this->assertSame(-1, $arr[-1]);
94ffe3c632Sopenharmony_ci        $arr[MIN_UINT32] = MIN_UINT32;
95ffe3c632Sopenharmony_ci        $this->assertSame(MIN_UINT32, $arr[MIN_UINT32]);
96ffe3c632Sopenharmony_ci        $this->assertEquals(2, count($arr));
97ffe3c632Sopenharmony_ci        unset($arr[-1]);
98ffe3c632Sopenharmony_ci        unset($arr[MIN_UINT32]);
99ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
100ffe3c632Sopenharmony_ci
101ffe3c632Sopenharmony_ci        // Test float argument.
102ffe3c632Sopenharmony_ci        $arr[MAX_UINT32_FLOAT] = MAX_UINT32_FLOAT;
103ffe3c632Sopenharmony_ci        $this->assertSame(-1, $arr[-1]);
104ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
105ffe3c632Sopenharmony_ci        unset($arr[MAX_UINT32_FLOAT]);
106ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
107ffe3c632Sopenharmony_ci
108ffe3c632Sopenharmony_ci        $arr[3.1] = 3.1;
109ffe3c632Sopenharmony_ci        $this->assertSame(3, $arr[3]);
110ffe3c632Sopenharmony_ci        $arr[-1.0] = -1.0;
111ffe3c632Sopenharmony_ci        $this->assertSame(-1, $arr[-1]);
112ffe3c632Sopenharmony_ci        $arr[MIN_UINT32_FLOAT] = MIN_UINT32_FLOAT;
113ffe3c632Sopenharmony_ci        $this->assertSame(MIN_UINT32, $arr[MIN_UINT32]);
114ffe3c632Sopenharmony_ci        $this->assertEquals(3, count($arr));
115ffe3c632Sopenharmony_ci        unset($arr[3.1]);
116ffe3c632Sopenharmony_ci        unset($arr[-1.0]);
117ffe3c632Sopenharmony_ci        unset($arr[MIN_UINT32_FLOAT]);
118ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
119ffe3c632Sopenharmony_ci
120ffe3c632Sopenharmony_ci        // Test string argument.
121ffe3c632Sopenharmony_ci        $arr[MAX_UINT32_STRING] = MAX_UINT32_STRING;
122ffe3c632Sopenharmony_ci        $this->assertSame(-1, $arr[-1]);
123ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
124ffe3c632Sopenharmony_ci        unset($arr[MAX_UINT32_STRING]);
125ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
126ffe3c632Sopenharmony_ci
127ffe3c632Sopenharmony_ci        $arr['7'] = '7';
128ffe3c632Sopenharmony_ci        $this->assertSame(7, $arr[7]);
129ffe3c632Sopenharmony_ci        $arr['3.1'] = '3.1';
130ffe3c632Sopenharmony_ci        $this->assertSame(3, $arr[3]);
131ffe3c632Sopenharmony_ci        $arr['-1.0'] = '-1.0';
132ffe3c632Sopenharmony_ci        $this->assertSame(-1, $arr[-1]);
133ffe3c632Sopenharmony_ci        $arr[MIN_UINT32_STRING] = MIN_UINT32_STRING;
134ffe3c632Sopenharmony_ci        $this->assertSame(MIN_UINT32, $arr[MIN_UINT32]);
135ffe3c632Sopenharmony_ci        $this->assertEquals(4, count($arr));
136ffe3c632Sopenharmony_ci        unset($arr['7']);
137ffe3c632Sopenharmony_ci        unset($arr['3.1']);
138ffe3c632Sopenharmony_ci        unset($arr['-1.0']);
139ffe3c632Sopenharmony_ci        unset($arr[MIN_UINT32_STRING]);
140ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
141ffe3c632Sopenharmony_ci    }
142ffe3c632Sopenharmony_ci
143ffe3c632Sopenharmony_ci    #########################################################
144ffe3c632Sopenharmony_ci    # Test int64 field.
145ffe3c632Sopenharmony_ci    #########################################################
146ffe3c632Sopenharmony_ci
147ffe3c632Sopenharmony_ci    public function testInt64() {
148ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::INT64, GPBType::INT64);
149ffe3c632Sopenharmony_ci
150ffe3c632Sopenharmony_ci        // Test integer argument.
151ffe3c632Sopenharmony_ci        $arr[MAX_INT64] = MAX_INT64;
152ffe3c632Sopenharmony_ci        $arr[MIN_INT64] = MIN_INT64;
153ffe3c632Sopenharmony_ci        if (PHP_INT_SIZE == 4) {
154ffe3c632Sopenharmony_ci            $this->assertSame(MAX_INT64_STRING, $arr[MAX_INT64_STRING]);
155ffe3c632Sopenharmony_ci            $this->assertSame(MIN_INT64_STRING, $arr[MIN_INT64_STRING]);
156ffe3c632Sopenharmony_ci        } else {
157ffe3c632Sopenharmony_ci            $this->assertSame(MAX_INT64, $arr[MAX_INT64]);
158ffe3c632Sopenharmony_ci            $this->assertSame(MIN_INT64, $arr[MIN_INT64]);
159ffe3c632Sopenharmony_ci        }
160ffe3c632Sopenharmony_ci        $this->assertEquals(2, count($arr));
161ffe3c632Sopenharmony_ci        unset($arr[MAX_INT64]);
162ffe3c632Sopenharmony_ci        unset($arr[MIN_INT64]);
163ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
164ffe3c632Sopenharmony_ci
165ffe3c632Sopenharmony_ci        // Test float argument.
166ffe3c632Sopenharmony_ci        $arr[1.1] = 1.1;
167ffe3c632Sopenharmony_ci        if (PHP_INT_SIZE == 4) {
168ffe3c632Sopenharmony_ci            $this->assertSame('1', $arr['1']);
169ffe3c632Sopenharmony_ci        } else {
170ffe3c632Sopenharmony_ci            $this->assertSame(1, $arr[1]);
171ffe3c632Sopenharmony_ci        }
172ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
173ffe3c632Sopenharmony_ci        unset($arr[1.1]);
174ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
175ffe3c632Sopenharmony_ci
176ffe3c632Sopenharmony_ci        // Test string argument.
177ffe3c632Sopenharmony_ci        $arr['2'] = '2';
178ffe3c632Sopenharmony_ci        $arr['3.1'] = '3.1';
179ffe3c632Sopenharmony_ci        $arr[MAX_INT64_STRING] = MAX_INT64_STRING;
180ffe3c632Sopenharmony_ci        $arr[MIN_INT64_STRING] = MIN_INT64_STRING;
181ffe3c632Sopenharmony_ci        if (PHP_INT_SIZE == 4) {
182ffe3c632Sopenharmony_ci            $this->assertSame('2', $arr['2']);
183ffe3c632Sopenharmony_ci            $this->assertSame('3', $arr['3']);
184ffe3c632Sopenharmony_ci            $this->assertSame(MAX_INT64_STRING, $arr[MAX_INT64_STRING]);
185ffe3c632Sopenharmony_ci            $this->assertSame(MIN_INT64_STRING, $arr[MIN_INT64_STRING]);
186ffe3c632Sopenharmony_ci        } else {
187ffe3c632Sopenharmony_ci            $this->assertSame(2, $arr[2]);
188ffe3c632Sopenharmony_ci            $this->assertSame(3, $arr[3]);
189ffe3c632Sopenharmony_ci            $this->assertSame(MAX_INT64, $arr[MAX_INT64]);
190ffe3c632Sopenharmony_ci            $this->assertSame(MIN_INT64, $arr[MIN_INT64]);
191ffe3c632Sopenharmony_ci        }
192ffe3c632Sopenharmony_ci        $this->assertEquals(4, count($arr));
193ffe3c632Sopenharmony_ci        unset($arr['2']);
194ffe3c632Sopenharmony_ci        unset($arr['3.1']);
195ffe3c632Sopenharmony_ci        unset($arr[MAX_INT64_STRING]);
196ffe3c632Sopenharmony_ci        unset($arr[MIN_INT64_STRING]);
197ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
198ffe3c632Sopenharmony_ci    }
199ffe3c632Sopenharmony_ci
200ffe3c632Sopenharmony_ci    #########################################################
201ffe3c632Sopenharmony_ci    # Test uint64 field.
202ffe3c632Sopenharmony_ci    #########################################################
203ffe3c632Sopenharmony_ci
204ffe3c632Sopenharmony_ci    public function testUint64() {
205ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::UINT64, GPBType::UINT64);
206ffe3c632Sopenharmony_ci
207ffe3c632Sopenharmony_ci        // Test integer argument.
208ffe3c632Sopenharmony_ci        $arr[MAX_UINT64] = MAX_UINT64;
209ffe3c632Sopenharmony_ci        if (PHP_INT_SIZE == 4) {
210ffe3c632Sopenharmony_ci            $this->assertSame(MAX_UINT64_STRING, $arr[MAX_UINT64_STRING]);
211ffe3c632Sopenharmony_ci        } else {
212ffe3c632Sopenharmony_ci            $this->assertSame(MAX_UINT64, $arr[MAX_UINT64]);
213ffe3c632Sopenharmony_ci        }
214ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
215ffe3c632Sopenharmony_ci        unset($arr[MAX_UINT64]);
216ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
217ffe3c632Sopenharmony_ci
218ffe3c632Sopenharmony_ci        // Test float argument.
219ffe3c632Sopenharmony_ci        $arr[1.1] = 1.1;
220ffe3c632Sopenharmony_ci        if (PHP_INT_SIZE == 4) {
221ffe3c632Sopenharmony_ci            $this->assertSame('1', $arr['1']);
222ffe3c632Sopenharmony_ci        } else {
223ffe3c632Sopenharmony_ci            $this->assertSame(1, $arr[1]);
224ffe3c632Sopenharmony_ci        }
225ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
226ffe3c632Sopenharmony_ci        unset($arr[1.1]);
227ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
228ffe3c632Sopenharmony_ci
229ffe3c632Sopenharmony_ci        // Test string argument.
230ffe3c632Sopenharmony_ci        $arr['2'] = '2';
231ffe3c632Sopenharmony_ci        $arr['3.1'] = '3.1';
232ffe3c632Sopenharmony_ci        $arr[MAX_UINT64_STRING] = MAX_UINT64_STRING;
233ffe3c632Sopenharmony_ci
234ffe3c632Sopenharmony_ci        if (PHP_INT_SIZE == 4) {
235ffe3c632Sopenharmony_ci            $this->assertSame('2', $arr['2']);
236ffe3c632Sopenharmony_ci            $this->assertSame('3', $arr['3']);
237ffe3c632Sopenharmony_ci            $this->assertSame(MAX_UINT64_STRING, $arr[MAX_UINT64_STRING]);
238ffe3c632Sopenharmony_ci        } else {
239ffe3c632Sopenharmony_ci            $this->assertSame(2, $arr[2]);
240ffe3c632Sopenharmony_ci            $this->assertSame(3, $arr[3]);
241ffe3c632Sopenharmony_ci            $this->assertSame(MAX_UINT64, $arr[MAX_UINT64]);
242ffe3c632Sopenharmony_ci        }
243ffe3c632Sopenharmony_ci
244ffe3c632Sopenharmony_ci        $this->assertEquals(3, count($arr));
245ffe3c632Sopenharmony_ci        unset($arr['2']);
246ffe3c632Sopenharmony_ci        unset($arr['3.1']);
247ffe3c632Sopenharmony_ci        unset($arr[MAX_UINT64_STRING]);
248ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
249ffe3c632Sopenharmony_ci    }
250ffe3c632Sopenharmony_ci
251ffe3c632Sopenharmony_ci    #########################################################
252ffe3c632Sopenharmony_ci    # Test float field.
253ffe3c632Sopenharmony_ci    #########################################################
254ffe3c632Sopenharmony_ci
255ffe3c632Sopenharmony_ci    public function testFloat() {
256ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::INT32, GPBType::FLOAT);
257ffe3c632Sopenharmony_ci
258ffe3c632Sopenharmony_ci        // Test set.
259ffe3c632Sopenharmony_ci        $arr[0] = 1;
260ffe3c632Sopenharmony_ci        $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
261ffe3c632Sopenharmony_ci
262ffe3c632Sopenharmony_ci        $arr[1] = 1.1;
263ffe3c632Sopenharmony_ci        $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
264ffe3c632Sopenharmony_ci
265ffe3c632Sopenharmony_ci        $arr[2] = '2';
266ffe3c632Sopenharmony_ci        $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
267ffe3c632Sopenharmony_ci        $arr[3] = '3.1';
268ffe3c632Sopenharmony_ci        $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
269ffe3c632Sopenharmony_ci
270ffe3c632Sopenharmony_ci        $this->assertEquals(4, count($arr));
271ffe3c632Sopenharmony_ci    }
272ffe3c632Sopenharmony_ci
273ffe3c632Sopenharmony_ci    #########################################################
274ffe3c632Sopenharmony_ci    # Test double field.
275ffe3c632Sopenharmony_ci    #########################################################
276ffe3c632Sopenharmony_ci
277ffe3c632Sopenharmony_ci    public function testDouble() {
278ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::INT32, GPBType::DOUBLE);
279ffe3c632Sopenharmony_ci
280ffe3c632Sopenharmony_ci        // Test set.
281ffe3c632Sopenharmony_ci        $arr[0] = 1;
282ffe3c632Sopenharmony_ci        $this->assertEquals(1.0, $arr[0], '', MAX_FLOAT_DIFF);
283ffe3c632Sopenharmony_ci
284ffe3c632Sopenharmony_ci        $arr[1] = 1.1;
285ffe3c632Sopenharmony_ci        $this->assertEquals(1.1, $arr[1], '', MAX_FLOAT_DIFF);
286ffe3c632Sopenharmony_ci
287ffe3c632Sopenharmony_ci        $arr[2] = '2';
288ffe3c632Sopenharmony_ci        $this->assertEquals(2.0, $arr[2], '', MAX_FLOAT_DIFF);
289ffe3c632Sopenharmony_ci        $arr[3] = '3.1';
290ffe3c632Sopenharmony_ci        $this->assertEquals(3.1, $arr[3], '', MAX_FLOAT_DIFF);
291ffe3c632Sopenharmony_ci
292ffe3c632Sopenharmony_ci        $this->assertEquals(4, count($arr));
293ffe3c632Sopenharmony_ci    }
294ffe3c632Sopenharmony_ci
295ffe3c632Sopenharmony_ci    #########################################################
296ffe3c632Sopenharmony_ci    # Test bool field.
297ffe3c632Sopenharmony_ci    #########################################################
298ffe3c632Sopenharmony_ci
299ffe3c632Sopenharmony_ci    public function testBool() {
300ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::BOOL, GPBType::BOOL);
301ffe3c632Sopenharmony_ci
302ffe3c632Sopenharmony_ci        // Test boolean.
303ffe3c632Sopenharmony_ci        $arr[True] = True;
304ffe3c632Sopenharmony_ci        $this->assertSame(True, $arr[True]);
305ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
306ffe3c632Sopenharmony_ci        unset($arr[True]);
307ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
308ffe3c632Sopenharmony_ci
309ffe3c632Sopenharmony_ci        $arr[False] = False;
310ffe3c632Sopenharmony_ci        $this->assertSame(False, $arr[False]);
311ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
312ffe3c632Sopenharmony_ci        unset($arr[False]);
313ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
314ffe3c632Sopenharmony_ci
315ffe3c632Sopenharmony_ci        // Test integer.
316ffe3c632Sopenharmony_ci        $arr[-1] = -1;
317ffe3c632Sopenharmony_ci        $this->assertSame(True, $arr[True]);
318ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
319ffe3c632Sopenharmony_ci        unset($arr[-1]);
320ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
321ffe3c632Sopenharmony_ci
322ffe3c632Sopenharmony_ci        $arr[0] = 0;
323ffe3c632Sopenharmony_ci        $this->assertSame(False, $arr[False]);
324ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
325ffe3c632Sopenharmony_ci        unset($arr[0]);
326ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
327ffe3c632Sopenharmony_ci
328ffe3c632Sopenharmony_ci        // Test float.
329ffe3c632Sopenharmony_ci        $arr[1.1] = 1.1;
330ffe3c632Sopenharmony_ci        $this->assertSame(True, $arr[True]);
331ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
332ffe3c632Sopenharmony_ci        unset($arr[1.1]);
333ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
334ffe3c632Sopenharmony_ci
335ffe3c632Sopenharmony_ci        $arr[0.0] = 0.0;
336ffe3c632Sopenharmony_ci        $this->assertSame(False, $arr[False]);
337ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
338ffe3c632Sopenharmony_ci        unset($arr[0.0]);
339ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
340ffe3c632Sopenharmony_ci
341ffe3c632Sopenharmony_ci        // Test string.
342ffe3c632Sopenharmony_ci        $arr['a'] = 'a';
343ffe3c632Sopenharmony_ci        $this->assertSame(True, $arr[True]);
344ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
345ffe3c632Sopenharmony_ci        unset($arr['a']);
346ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
347ffe3c632Sopenharmony_ci
348ffe3c632Sopenharmony_ci        $arr[''] = '';
349ffe3c632Sopenharmony_ci        $this->assertSame(False, $arr[False]);
350ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
351ffe3c632Sopenharmony_ci        unset($arr['']);
352ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
353ffe3c632Sopenharmony_ci    }
354ffe3c632Sopenharmony_ci
355ffe3c632Sopenharmony_ci    #########################################################
356ffe3c632Sopenharmony_ci    # Test string field.
357ffe3c632Sopenharmony_ci    #########################################################
358ffe3c632Sopenharmony_ci
359ffe3c632Sopenharmony_ci    public function testString() {
360ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::STRING, GPBType::STRING);
361ffe3c632Sopenharmony_ci
362ffe3c632Sopenharmony_ci        // Test set.
363ffe3c632Sopenharmony_ci        $arr['abc'] = 'abc';
364ffe3c632Sopenharmony_ci        $this->assertSame('abc', $arr['abc']);
365ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
366ffe3c632Sopenharmony_ci        unset($arr['abc']);
367ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
368ffe3c632Sopenharmony_ci
369ffe3c632Sopenharmony_ci        $arr[1] = 1;
370ffe3c632Sopenharmony_ci        $this->assertSame('1', $arr['1']);
371ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
372ffe3c632Sopenharmony_ci        unset($arr[1]);
373ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
374ffe3c632Sopenharmony_ci
375ffe3c632Sopenharmony_ci        $arr[1.1] = 1.1;
376ffe3c632Sopenharmony_ci        $this->assertSame('1.1', $arr['1.1']);
377ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
378ffe3c632Sopenharmony_ci        unset($arr[1.1]);
379ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
380ffe3c632Sopenharmony_ci
381ffe3c632Sopenharmony_ci        $arr[True] = True;
382ffe3c632Sopenharmony_ci        $this->assertSame('1', $arr['1']);
383ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
384ffe3c632Sopenharmony_ci        unset($arr[True]);
385ffe3c632Sopenharmony_ci        $this->assertEquals(0, count($arr));
386ffe3c632Sopenharmony_ci
387ffe3c632Sopenharmony_ci        // Test foreach.
388ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::STRING, GPBType::STRING);
389ffe3c632Sopenharmony_ci        for ($i = 0; $i < 3; $i++) {
390ffe3c632Sopenharmony_ci          $arr[$i] = $i;
391ffe3c632Sopenharmony_ci        }
392ffe3c632Sopenharmony_ci        $i = 0;
393ffe3c632Sopenharmony_ci        $arr_test = [];
394ffe3c632Sopenharmony_ci        foreach ($arr as $key => $val) {
395ffe3c632Sopenharmony_ci          $this->assertSame($key, $val);
396ffe3c632Sopenharmony_ci          $arr_test[] = $key;
397ffe3c632Sopenharmony_ci          $i++;
398ffe3c632Sopenharmony_ci        }
399ffe3c632Sopenharmony_ci        $this->assertTrue(isset($arr_test['0']));
400ffe3c632Sopenharmony_ci        $this->assertTrue(isset($arr_test['1']));
401ffe3c632Sopenharmony_ci        $this->assertTrue(isset($arr_test['2']));
402ffe3c632Sopenharmony_ci        $this->assertSame(3, $i);
403ffe3c632Sopenharmony_ci    }
404ffe3c632Sopenharmony_ci
405ffe3c632Sopenharmony_ci    #########################################################
406ffe3c632Sopenharmony_ci    # Test message field.
407ffe3c632Sopenharmony_ci    #########################################################
408ffe3c632Sopenharmony_ci
409ffe3c632Sopenharmony_ci    public function testMessage() {
410ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::INT32,
411ffe3c632Sopenharmony_ci            GPBType::MESSAGE, Sub::class);
412ffe3c632Sopenharmony_ci
413ffe3c632Sopenharmony_ci        // Test append.
414ffe3c632Sopenharmony_ci        $sub_m = new Sub();
415ffe3c632Sopenharmony_ci        $sub_m->setA(1);
416ffe3c632Sopenharmony_ci        $arr[0] = $sub_m;
417ffe3c632Sopenharmony_ci        $this->assertSame(1, $arr[0]->getA());
418ffe3c632Sopenharmony_ci
419ffe3c632Sopenharmony_ci        $this->assertEquals(1, count($arr));
420ffe3c632Sopenharmony_ci
421ffe3c632Sopenharmony_ci        // Test foreach.
422ffe3c632Sopenharmony_ci        $arr = new MapField(GPBType::INT32,
423ffe3c632Sopenharmony_ci            GPBType::MESSAGE, Sub::class);
424ffe3c632Sopenharmony_ci        for ($i = 0; $i < 3; $i++) {
425ffe3c632Sopenharmony_ci          $arr[$i] = new Sub();;
426ffe3c632Sopenharmony_ci          $arr[$i]->setA($i);
427ffe3c632Sopenharmony_ci        }
428ffe3c632Sopenharmony_ci        $i = 0;
429ffe3c632Sopenharmony_ci        $key_test = [];
430ffe3c632Sopenharmony_ci        $value_test = [];
431ffe3c632Sopenharmony_ci        foreach ($arr as $key => $val) {
432ffe3c632Sopenharmony_ci          $key_test[] = $key;
433ffe3c632Sopenharmony_ci          $value_test[] = $val->getA();
434ffe3c632Sopenharmony_ci          $i++;
435ffe3c632Sopenharmony_ci        }
436ffe3c632Sopenharmony_ci        $this->assertTrue(isset($key_test['0']));
437ffe3c632Sopenharmony_ci        $this->assertTrue(isset($key_test['1']));
438ffe3c632Sopenharmony_ci        $this->assertTrue(isset($key_test['2']));
439ffe3c632Sopenharmony_ci        $this->assertTrue(isset($value_test['0']));
440ffe3c632Sopenharmony_ci        $this->assertTrue(isset($value_test['1']));
441ffe3c632Sopenharmony_ci        $this->assertTrue(isset($value_test['2']));
442ffe3c632Sopenharmony_ci        $this->assertSame(3, $i);
443ffe3c632Sopenharmony_ci    }
444ffe3c632Sopenharmony_ci
445ffe3c632Sopenharmony_ci    #########################################################
446ffe3c632Sopenharmony_ci    # Test reference in map
447ffe3c632Sopenharmony_ci    #########################################################
448ffe3c632Sopenharmony_ci
449ffe3c632Sopenharmony_ci    public function testMapElementIsReference()
450ffe3c632Sopenharmony_ci    {
451ffe3c632Sopenharmony_ci        // Bool elements
452ffe3c632Sopenharmony_ci        $values = [true => true];
453ffe3c632Sopenharmony_ci        array_walk($values, function (&$value) {});
454ffe3c632Sopenharmony_ci        $m = new TestMessage();
455ffe3c632Sopenharmony_ci        $m->setMapBoolBool($values);
456ffe3c632Sopenharmony_ci
457ffe3c632Sopenharmony_ci        // Int32 elements
458ffe3c632Sopenharmony_ci        $values = [1 => 1];
459ffe3c632Sopenharmony_ci        array_walk($values, function (&$value) {});
460ffe3c632Sopenharmony_ci        $m = new TestMessage();
461ffe3c632Sopenharmony_ci        $m->setMapInt32Int32($values);
462ffe3c632Sopenharmony_ci
463ffe3c632Sopenharmony_ci        // Double elements
464ffe3c632Sopenharmony_ci        $values = [1 => 1.0];
465ffe3c632Sopenharmony_ci        array_walk($values, function (&$value) {});
466ffe3c632Sopenharmony_ci        $m = new TestMessage();
467ffe3c632Sopenharmony_ci        $m->setMapInt32Double($values);
468ffe3c632Sopenharmony_ci
469ffe3c632Sopenharmony_ci        // String elements
470ffe3c632Sopenharmony_ci        $values = ['a' => 'a'];
471ffe3c632Sopenharmony_ci        array_walk($values, function (&$value) {});
472ffe3c632Sopenharmony_ci        $m = new TestMessage();
473ffe3c632Sopenharmony_ci        $m->setMapStringString($values);
474ffe3c632Sopenharmony_ci
475ffe3c632Sopenharmony_ci        // Message elements
476ffe3c632Sopenharmony_ci        $values = [1 => new Sub()];
477ffe3c632Sopenharmony_ci        array_walk($values, function (&$value) {});
478ffe3c632Sopenharmony_ci        $m = new TestMessage();
479ffe3c632Sopenharmony_ci        $m->setMapInt32Message($values);
480ffe3c632Sopenharmony_ci    }
481ffe3c632Sopenharmony_ci
482ffe3c632Sopenharmony_ci    #########################################################
483ffe3c632Sopenharmony_ci    # Test memory leak
484ffe3c632Sopenharmony_ci    #########################################################
485ffe3c632Sopenharmony_ci
486ffe3c632Sopenharmony_ci    // TODO(teboring): Add it back.
487ffe3c632Sopenharmony_ci    // public function testCycleLeak()
488ffe3c632Sopenharmony_ci    // {
489ffe3c632Sopenharmony_ci    //     $arr = new MapField(GPBType::INT32,
490ffe3c632Sopenharmony_ci    //         GPBType::MESSAGE, TestMessage::class);
491ffe3c632Sopenharmony_ci    //     $arr[0] = new TestMessage;
492ffe3c632Sopenharmony_ci    //     $arr[0]->SetMapRecursive($arr);
493ffe3c632Sopenharmony_ci
494ffe3c632Sopenharmony_ci    //     // Clean up memory before test.
495ffe3c632Sopenharmony_ci    //     gc_collect_cycles();
496ffe3c632Sopenharmony_ci    //     $start = memory_get_usage();
497ffe3c632Sopenharmony_ci    //     unset($arr);
498ffe3c632Sopenharmony_ci
499ffe3c632Sopenharmony_ci    //     // Explicitly trigger garbage collection.
500ffe3c632Sopenharmony_ci    //     gc_collect_cycles();
501ffe3c632Sopenharmony_ci
502ffe3c632Sopenharmony_ci    //     $end = memory_get_usage();
503ffe3c632Sopenharmony_ci    //     $this->assertLessThan($start, $end);
504ffe3c632Sopenharmony_ci    // }
505ffe3c632Sopenharmony_ci}
506