Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(393)

Side by Side Diff: third_party/protobuf/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 14 matching lines...) Expand all
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 30
31 package com.google.protobuf; 31 package com.google.protobuf;
32 32
33 import static java.util.Arrays.asList; 33 import static java.util.Arrays.asList;
34 34
35 import junit.framework.TestCase;
36
37 import java.util.Collections; 35 import java.util.Collections;
38 import java.util.ConcurrentModificationException; 36 import java.util.ConcurrentModificationException;
39 import java.util.Iterator; 37 import java.util.Iterator;
38 import junit.framework.TestCase;
40 39
41 /** 40 /**
42 * Tests for {@link BooleanArrayList}. 41 * Tests for {@link BooleanArrayList}.
43 * 42 *
44 * @author dweis@google.com (Daniel Weis) 43 * @author dweis@google.com (Daniel Weis)
45 */ 44 */
46 public class BooleanArrayListTest extends TestCase { 45 public class BooleanArrayListTest extends TestCase {
47 46
48 private static final BooleanArrayList UNARY_LIST = newImmutableBooleanArrayLis t(true); 47 private static final BooleanArrayList UNARY_LIST =
48 newImmutableBooleanArrayList(true);
49 private static final BooleanArrayList TERTIARY_LIST = 49 private static final BooleanArrayList TERTIARY_LIST =
50 newImmutableBooleanArrayList(true, true, false); 50 newImmutableBooleanArrayList(true, false, true);
51 51
52 private BooleanArrayList list; 52 private BooleanArrayList list;
53 53
54 @Override 54 @Override
55 protected void setUp() throws Exception { 55 protected void setUp() throws Exception {
56 list = new BooleanArrayList(); 56 list = new BooleanArrayList();
57 } 57 }
58 58
59 public void testEmptyListReturnsSameInstance() { 59 public void testEmptyListReturnsSameInstance() {
60 assertSame(BooleanArrayList.emptyList(), BooleanArrayList.emptyList()); 60 assertSame(BooleanArrayList.emptyList(), BooleanArrayList.emptyList());
61 } 61 }
62 62
63 public void testEmptyListIsImmutable() { 63 public void testEmptyListIsImmutable() {
64 assertImmutable(BooleanArrayList.emptyList()); 64 assertImmutable(BooleanArrayList.emptyList());
65 } 65 }
66 66
67 public void testMakeImmutable() { 67 public void testMakeImmutable() {
68 list.addBoolean(true); 68 list.addBoolean(true);
69 list.addBoolean(false); 69 list.addBoolean(false);
70 list.addBoolean(true); 70 list.addBoolean(true);
71 list.addBoolean(true); 71 list.addBoolean(true);
72 list.makeImmutable(); 72 list.makeImmutable();
73 assertImmutable(list); 73 assertImmutable(list);
74 } 74 }
75 75
76 public void testModificationWithIteration() { 76 public void testModificationWithIteration() {
77 list.addAll(asList(true, false, false, true)); 77 list.addAll(asList(true, false, true, false));
78 Iterator<Boolean> iterator = list.iterator(); 78 Iterator<Boolean> iterator = list.iterator();
79 assertEquals(4, list.size()); 79 assertEquals(4, list.size());
80 assertEquals(true, (boolean) list.get(0)); 80 assertEquals(true, (boolean) list.get(0));
81 assertEquals(true, (boolean) iterator.next()); 81 assertEquals(true, (boolean) iterator.next());
82 list.set(0, true); 82 list.set(0, true);
83 assertEquals(false, (boolean) iterator.next()); 83 assertEquals(false, (boolean) iterator.next());
84 84
85 list.remove(0); 85 list.remove(0);
86 try { 86 try {
87 iterator.next(); 87 iterator.next();
88 fail(); 88 fail();
89 } catch (ConcurrentModificationException e) { 89 } catch (ConcurrentModificationException e) {
90 // expected 90 // expected
91 } 91 }
92 92
93 iterator = list.iterator(); 93 iterator = list.iterator();
94 list.add(0, false); 94 list.add(0, false);
95 try { 95 try {
96 iterator.next(); 96 iterator.next();
97 fail(); 97 fail();
98 } catch (ConcurrentModificationException e) { 98 } catch (ConcurrentModificationException e) {
99 // expected 99 // expected
100 } 100 }
101 } 101 }
102 102
103 public void testGet() { 103 public void testGet() {
104 assertEquals(true, (boolean) TERTIARY_LIST.get(0)); 104 assertEquals(true, (boolean) TERTIARY_LIST.get(0));
105 assertEquals(true, (boolean) TERTIARY_LIST.get(1)); 105 assertEquals(false, (boolean) TERTIARY_LIST.get(1));
106 assertEquals(false, (boolean) TERTIARY_LIST.get(2)); 106 assertEquals(true, (boolean) TERTIARY_LIST.get(2));
107 107
108 try { 108 try {
109 TERTIARY_LIST.get(-1); 109 TERTIARY_LIST.get(-1);
110 fail(); 110 fail();
111 } catch (IndexOutOfBoundsException e) { 111 } catch (IndexOutOfBoundsException e) {
112 // expected 112 // expected
113 } 113 }
114 114
115 try { 115 try {
116 TERTIARY_LIST.get(3); 116 TERTIARY_LIST.get(3);
117 fail(); 117 fail();
118 } catch (IndexOutOfBoundsException e) { 118 } catch (IndexOutOfBoundsException e) {
119 // expected 119 // expected
120 } 120 }
121 } 121 }
122 122
123 public void testGetInt() { 123 public void testGetBoolean() {
124 assertEquals(true, TERTIARY_LIST.getBoolean(0)); 124 assertEquals(true, TERTIARY_LIST.getBoolean(0));
125 assertEquals(true, TERTIARY_LIST.getBoolean(1)); 125 assertEquals(false, TERTIARY_LIST.getBoolean(1));
126 assertEquals(false, TERTIARY_LIST.getBoolean(2)); 126 assertEquals(true, TERTIARY_LIST.getBoolean(2));
127 127
128 try { 128 try {
129 TERTIARY_LIST.get(-1); 129 TERTIARY_LIST.get(-1);
130 fail(); 130 fail();
131 } catch (IndexOutOfBoundsException e) { 131 } catch (IndexOutOfBoundsException e) {
132 // expected 132 // expected
133 } 133 }
134 134
135 try { 135 try {
136 TERTIARY_LIST.get(3); 136 TERTIARY_LIST.get(3);
137 fail(); 137 fail();
138 } catch (IndexOutOfBoundsException e) { 138 } catch (IndexOutOfBoundsException e) {
139 // expected 139 // expected
140 } 140 }
141 } 141 }
142 142
143 public void testSize() { 143 public void testSize() {
144 assertEquals(0, BooleanArrayList.emptyList().size()); 144 assertEquals(0, BooleanArrayList.emptyList().size());
145 assertEquals(1, UNARY_LIST.size()); 145 assertEquals(1, UNARY_LIST.size());
146 assertEquals(3, TERTIARY_LIST.size()); 146 assertEquals(3, TERTIARY_LIST.size());
147 147
148 list.addBoolean(true); 148 list.addBoolean(true);
149 list.addBoolean(false); 149 list.addBoolean(false);
150 list.addBoolean(false); 150 list.addBoolean(false);
151 list.addBoolean(false); 151 list.addBoolean(false);
152 assertEquals(4, list.size()); 152 assertEquals(4, list.size());
153 153
154 list.remove(0); 154 list.remove(0);
155 assertEquals(3, list.size()); 155 assertEquals(3, list.size());
156 156
157 list.add(true); 157 list.add(true);
158 assertEquals(4, list.size()); 158 assertEquals(4, list.size());
159 } 159 }
160 160
161 public void testSet() { 161 public void testSet() {
162 list.addBoolean(false); 162 list.addBoolean(false);
163 list.addBoolean(false); 163 list.addBoolean(false);
164 164
165 assertEquals(false, (boolean) list.set(0, true)); 165 assertEquals(false, (boolean) list.set(0, true));
166 assertEquals(true, list.getBoolean(0)); 166 assertEquals(true, list.getBoolean(0));
167 167
168 assertEquals(false, (boolean) list.set(1, false)); 168 assertEquals(false, (boolean) list.set(1, false));
169 assertEquals(false, list.getBoolean(1)); 169 assertEquals(false, list.getBoolean(1));
170 170
171 try { 171 try {
172 list.set(-1, true); 172 list.set(-1, false);
173 fail(); 173 fail();
174 } catch (IndexOutOfBoundsException e) { 174 } catch (IndexOutOfBoundsException e) {
175 // expected 175 // expected
176 } 176 }
177 177
178 try { 178 try {
179 list.set(2, false); 179 list.set(2, false);
180 fail(); 180 fail();
181 } catch (IndexOutOfBoundsException e) { 181 } catch (IndexOutOfBoundsException e) {
182 // expected 182 // expected
183 } 183 }
184 } 184 }
185 185
186 public void testSetInt() { 186 public void testSetBoolean() {
187 list.addBoolean(true); 187 list.addBoolean(true);
188 list.addBoolean(true); 188 list.addBoolean(true);
189 189
190 assertEquals(true, list.setBoolean(0, false)); 190 assertEquals(true, list.setBoolean(0, false));
191 assertEquals(false, list.getBoolean(0)); 191 assertEquals(false, list.getBoolean(0));
192 192
193 assertEquals(true, list.setBoolean(1, false)); 193 assertEquals(true, list.setBoolean(1, false));
194 assertEquals(false, list.getBoolean(1)); 194 assertEquals(false, list.getBoolean(1));
195 195
196 try { 196 try {
197 list.setBoolean(-1, false); 197 list.setBoolean(-1, false);
198 fail(); 198 fail();
199 } catch (IndexOutOfBoundsException e) { 199 } catch (IndexOutOfBoundsException e) {
200 // expected 200 // expected
201 } 201 }
202 202
203 try { 203 try {
204 list.setBoolean(2, true); 204 list.setBoolean(2, false);
205 fail(); 205 fail();
206 } catch (IndexOutOfBoundsException e) { 206 } catch (IndexOutOfBoundsException e) {
207 // expected 207 // expected
208 } 208 }
209 } 209 }
210 210
211 public void testAdd() { 211 public void testAdd() {
212 assertEquals(0, list.size()); 212 assertEquals(0, list.size());
213 213
214 assertTrue(list.add(false));
215 assertEquals(asList(false), list);
216
214 assertTrue(list.add(true)); 217 assertTrue(list.add(true));
215 assertEquals(asList(true), list); 218 list.add(0, false);
219 assertEquals(asList(false, false, true), list);
216 220
217 assertTrue(list.add(false)); 221 list.add(0, true);
218 list.add(0, false); 222 list.add(0, false);
219 assertEquals(asList(false, true, false), list);
220
221 list.add(0, false);
222 list.add(0, true);
223 // Force a resize by getting up to 11 elements. 223 // Force a resize by getting up to 11 elements.
224 for (int i = 0; i < 6; i++) { 224 for (int i = 0; i < 6; i++) {
225 list.add(true); 225 list.add(i % 2 == 0);
226 } 226 }
227 assertEquals(asList(true, false, false, true, false, true, true, true, true, true, true), list); 227 assertEquals(
228 228 asList(false, true, false, false, true, true, false, true, false, true, false),
229 list);
230
229 try { 231 try {
230 list.add(-1, false); 232 list.add(-1, true);
231 } catch (IndexOutOfBoundsException e) { 233 } catch (IndexOutOfBoundsException e) {
232 // expected 234 // expected
233 } 235 }
234 236
235 try { 237 try {
236 list.add(4, true); 238 list.add(4, true);
237 } catch (IndexOutOfBoundsException e) { 239 } catch (IndexOutOfBoundsException e) {
238 // expected 240 // expected
239 } 241 }
240 } 242 }
241 243
242 public void testAddInt() { 244 public void testAddBoolean() {
243 assertEquals(0, list.size()); 245 assertEquals(0, list.size());
244 246
247 list.addBoolean(false);
248 assertEquals(asList(false), list);
249
245 list.addBoolean(true); 250 list.addBoolean(true);
246 assertEquals(asList(true), list); 251 assertEquals(asList(false, true), list);
252 }
247 253
248 list.addBoolean(false);
249 assertEquals(asList(true, false), list);
250 }
251
252 public void testAddAll() { 254 public void testAddAll() {
253 assertEquals(0, list.size()); 255 assertEquals(0, list.size());
254 256
255 assertTrue(list.addAll(Collections.singleton(false))); 257 assertTrue(list.addAll(Collections.singleton(true)));
256 assertEquals(1, list.size()); 258 assertEquals(1, list.size());
257 assertEquals(false, (boolean) list.get(0)); 259 assertEquals(true, (boolean) list.get(0));
258 assertEquals(false, list.getBoolean(0)); 260 assertEquals(true, list.getBoolean(0));
259 261
260 assertTrue(list.addAll(asList(true, false, false, false, true))); 262 assertTrue(list.addAll(asList(false, true, false, true, false)));
261 assertEquals(asList(false, true, false, false, false, true), list); 263 assertEquals(asList(true, false, true, false, true, false), list);
262 264
263 assertTrue(list.addAll(TERTIARY_LIST)); 265 assertTrue(list.addAll(TERTIARY_LIST));
264 assertEquals(asList(false, true, false, false, false, true, true, true, fals e), list); 266 assertEquals(asList(true, false, true, false, true, false, true, false, true ), list);
265 267
266 assertFalse(list.addAll(Collections.<Boolean>emptyList())); 268 assertFalse(list.addAll(Collections.<Boolean>emptyList()));
267 assertFalse(list.addAll(BooleanArrayList.emptyList())); 269 assertFalse(list.addAll(BooleanArrayList.emptyList()));
268 } 270 }
269 271
270 public void testRemove() { 272 public void testRemove() {
271 list.addAll(TERTIARY_LIST); 273 list.addAll(TERTIARY_LIST);
272 assertEquals(true, (boolean) list.remove(0)); 274 assertEquals(true, (boolean) list.remove(0));
273 assertEquals(asList(true, false), list); 275 assertEquals(asList(false, true), list);
274 276
275 assertTrue(list.remove(Boolean.TRUE)); 277 assertTrue(list.remove(Boolean.TRUE));
276 assertEquals(asList(false), list); 278 assertEquals(asList(false), list);
277 279
278 assertFalse(list.remove(Boolean.TRUE)); 280 assertFalse(list.remove(Boolean.TRUE));
279 assertEquals(asList(false), list); 281 assertEquals(asList(false), list);
280 282
281 assertEquals(false, (boolean) list.remove(0)); 283 assertEquals(false, (boolean) list.remove(0));
282 assertEquals(asList(), list); 284 assertEquals(asList(), list);
283 285
284 try { 286 try {
285 list.remove(-1); 287 list.remove(-1);
286 fail(); 288 fail();
287 } catch (IndexOutOfBoundsException e) { 289 } catch (IndexOutOfBoundsException e) {
288 // expected 290 // expected
289 } 291 }
290 292
291 try { 293 try {
292 list.remove(0); 294 list.remove(0);
293 } catch (IndexOutOfBoundsException e) { 295 } catch (IndexOutOfBoundsException e) {
294 // expected 296 // expected
295 } 297 }
296 } 298 }
297 299
298 private void assertImmutable(BooleanArrayList list) { 300 private void assertImmutable(BooleanArrayList list) {
301
299 try { 302 try {
300 list.add(false); 303 list.add(true);
301 fail(); 304 fail();
302 } catch (UnsupportedOperationException e) { 305 } catch (UnsupportedOperationException e) {
303 // expected 306 // expected
304 } 307 }
305 308
306 try { 309 try {
307 list.add(0, true); 310 list.add(0, true);
308 fail(); 311 fail();
309 } catch (UnsupportedOperationException e) { 312 } catch (UnsupportedOperationException e) {
310 // expected 313 // expected
311 } 314 }
312 315
313 try { 316 try {
314 list.addAll(Collections.<Boolean>emptyList()); 317 list.addAll(Collections.<Boolean>emptyList());
315 fail(); 318 fail();
316 } catch (UnsupportedOperationException e) { 319 } catch (UnsupportedOperationException e) {
317 // expected 320 // expected
318 } 321 }
319 322
320 try { 323 try {
321 list.addAll(Collections.singletonList(false)); 324 list.addAll(Collections.singletonList(true));
322 fail(); 325 fail();
323 } catch (UnsupportedOperationException e) { 326 } catch (UnsupportedOperationException e) {
324 // expected 327 // expected
325 } 328 }
326 329
327 try { 330 try {
328 list.addAll(new BooleanArrayList()); 331 list.addAll(new BooleanArrayList());
329 fail(); 332 fail();
330 } catch (UnsupportedOperationException e) { 333 } catch (UnsupportedOperationException e) {
331 // expected 334 // expected
332 } 335 }
333 336
334 try { 337 try {
335 list.addAll(UNARY_LIST); 338 list.addAll(UNARY_LIST);
336 fail(); 339 fail();
337 } catch (UnsupportedOperationException e) { 340 } catch (UnsupportedOperationException e) {
338 // expected 341 // expected
339 } 342 }
340 343
341 try { 344 try {
342 list.addAll(0, Collections.singleton(true)); 345 list.addAll(0, Collections.singleton(true));
343 fail(); 346 fail();
344 } catch (UnsupportedOperationException e) { 347 } catch (UnsupportedOperationException e) {
345 // expected 348 // expected
346 } 349 }
347 350
348 try { 351 try {
349 list.addAll(0, UNARY_LIST); 352 list.addAll(0, UNARY_LIST);
350 fail(); 353 fail();
351 } catch (UnsupportedOperationException e) { 354 } catch (UnsupportedOperationException e) {
352 // expected 355 // expected
353 } 356 }
354 357
355 try { 358 try {
356 list.addAll(0, Collections.<Boolean>emptyList()); 359 list.addAll(0, Collections.<Boolean>emptyList());
357 fail(); 360 fail();
358 } catch (UnsupportedOperationException e) { 361 } catch (UnsupportedOperationException e) {
359 // expected 362 // expected
360 } 363 }
361 364
362 try { 365 try {
363 list.addBoolean(true); 366 list.addBoolean(false);
364 fail(); 367 fail();
365 } catch (UnsupportedOperationException e) { 368 } catch (UnsupportedOperationException e) {
366 // expected 369 // expected
367 } 370 }
368 371
369 try { 372 try {
370 list.clear(); 373 list.clear();
371 fail(); 374 fail();
372 } catch (UnsupportedOperationException e) { 375 } catch (UnsupportedOperationException e) {
373 // expected 376 // expected
374 } 377 }
375 378
376 try { 379 try {
377 list.remove(1); 380 list.remove(1);
378 fail(); 381 fail();
379 } catch (UnsupportedOperationException e) { 382 } catch (UnsupportedOperationException e) {
380 // expected 383 // expected
381 } 384 }
382 385
383 try { 386 try {
384 list.remove(new Object()); 387 list.remove(new Object());
385 fail(); 388 fail();
386 } catch (UnsupportedOperationException e) { 389 } catch (UnsupportedOperationException e) {
387 // expected 390 // expected
388 } 391 }
389 392
390 try { 393 try {
391 list.removeAll(Collections.<Boolean>emptyList()); 394 list.removeAll(Collections.<Boolean>emptyList());
392 fail(); 395 fail();
393 } catch (UnsupportedOperationException e) { 396 } catch (UnsupportedOperationException e) {
394 // expected 397 // expected
395 } 398 }
396 399
397 try { 400 try {
398 list.removeAll(Collections.singleton(Boolean.TRUE)); 401 list.removeAll(Collections.singleton(Boolean.TRUE));
399 fail(); 402 fail();
400 } catch (UnsupportedOperationException e) { 403 } catch (UnsupportedOperationException e) {
401 // expected 404 // expected
402 } 405 }
403 406
404 try { 407 try {
405 list.removeAll(UNARY_LIST); 408 list.removeAll(UNARY_LIST);
406 fail(); 409 fail();
407 } catch (UnsupportedOperationException e) { 410 } catch (UnsupportedOperationException e) {
408 // expected 411 // expected
409 } 412 }
410 413
411 try { 414 try {
412 list.retainAll(Collections.<Boolean>emptyList()); 415 list.retainAll(Collections.<Boolean>emptyList());
413 fail(); 416 fail();
414 } catch (UnsupportedOperationException e) { 417 } catch (UnsupportedOperationException e) {
415 // expected 418 // expected
416 } 419 }
417 420
418 try { 421 try {
419 list.retainAll(Collections.singleton(Boolean.TRUE)); 422 list.removeAll(Collections.singleton(Boolean.TRUE));
420 fail(); 423 fail();
421 } catch (UnsupportedOperationException e) { 424 } catch (UnsupportedOperationException e) {
422 // expected 425 // expected
423 } 426 }
424 427
425 try { 428 try {
426 list.retainAll(UNARY_LIST); 429 list.retainAll(UNARY_LIST);
427 fail(); 430 fail();
428 } catch (UnsupportedOperationException e) { 431 } catch (UnsupportedOperationException e) {
429 // expected 432 // expected
430 } 433 }
431 434
432 try { 435 try {
433 list.set(0, true); 436 list.set(0, false);
434 fail(); 437 fail();
435 } catch (UnsupportedOperationException e) { 438 } catch (UnsupportedOperationException e) {
436 // expected 439 // expected
437 } 440 }
438 441
439 try { 442 try {
440 list.setBoolean(0, false); 443 list.setBoolean(0, false);
441 fail(); 444 fail();
442 } catch (UnsupportedOperationException e) { 445 } catch (UnsupportedOperationException e) {
443 // expected 446 // expected
444 } 447 }
445 } 448 }
446 449
447 private static BooleanArrayList newImmutableBooleanArrayList(boolean... elemen ts) { 450 private static BooleanArrayList newImmutableBooleanArrayList(boolean... elemen ts) {
448 BooleanArrayList list = new BooleanArrayList(); 451 BooleanArrayList list = new BooleanArrayList();
449 for (boolean element : elements) { 452 for (boolean element : elements) {
450 list.addBoolean(element); 453 list.addBoolean(element);
451 } 454 }
452 list.makeImmutable(); 455 list.makeImmutable();
453 return list; 456 return list;
454 } 457 }
455 } 458 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698