1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
31 package com.google.protobuf;
33 import junit.framework.TestCase;
35 import java.util.ArrayList;
36 import java.util.List;
39 * Tests for {@link LazyStringArrayList}.
41 * @author jonp@google.com (Jon Perlow)
43 public class LazyStringArrayListTest extends TestCase {
45 private static String STRING_A = "A";
46 private static String STRING_B = "B";
47 private static String STRING_C = "C";
49 private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A");
50 private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B");
51 private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C");
53 public void testJustStrings() {
54 LazyStringArrayList list = new LazyStringArrayList();
59 assertEquals(3, list.size());
60 assertSame(STRING_A, list.get(0));
61 assertSame(STRING_B, list.get(1));
62 assertSame(STRING_C, list.get(2));
64 list.set(1, STRING_C);
65 assertSame(STRING_C, list.get(1));
68 assertSame(STRING_A, list.get(0));
69 assertSame(STRING_C, list.get(1));
72 public void testJustByteString() {
73 LazyStringArrayList list = new LazyStringArrayList();
74 list.add(BYTE_STRING_A);
75 list.add(BYTE_STRING_B);
76 list.add(BYTE_STRING_C);
78 assertEquals(3, list.size());
79 assertSame(BYTE_STRING_A, list.getByteString(0));
80 assertSame(BYTE_STRING_B, list.getByteString(1));
81 assertSame(BYTE_STRING_C, list.getByteString(2));
84 assertSame(BYTE_STRING_A, list.getByteString(0));
85 assertSame(BYTE_STRING_C, list.getByteString(1));
88 public void testConversionBackAndForth() {
89 LazyStringArrayList list = new LazyStringArrayList();
91 list.add(BYTE_STRING_B);
92 list.add(BYTE_STRING_C);
94 // String a should be the same because it was originally a string
95 assertSame(STRING_A, list.get(0));
97 // String b and c should be different because the string has to be computed
98 // from the ByteString
99 String bPrime = list.get(1);
100 assertNotSame(STRING_B, bPrime);
101 assertEquals(STRING_B, bPrime);
102 String cPrime = list.get(2);
103 assertNotSame(STRING_C, cPrime);
104 assertEquals(STRING_C, cPrime);
106 // String c and c should stay the same once cached.
107 assertSame(bPrime, list.get(1));
108 assertSame(cPrime, list.get(2));
110 // ByteString needs to be computed from string for both a and b
111 ByteString aPrimeByteString = list.getByteString(0);
112 assertEquals(BYTE_STRING_A, aPrimeByteString);
113 ByteString bPrimeByteString = list.getByteString(1);
114 assertNotSame(BYTE_STRING_B, bPrimeByteString);
115 assertEquals(BYTE_STRING_B, list.getByteString(1));
117 // Once cached, ByteString should stay cached.
118 assertSame(aPrimeByteString, list.getByteString(0));
119 assertSame(bPrimeByteString, list.getByteString(1));
122 public void testCopyConstructorCopiesByReference() {
123 LazyStringArrayList list1 = new LazyStringArrayList();
125 list1.add(BYTE_STRING_B);
126 list1.add(BYTE_STRING_C);
128 LazyStringArrayList list2 = new LazyStringArrayList(list1);
129 assertEquals(3, list2.size());
130 assertSame(STRING_A, list2.get(0));
131 assertSame(BYTE_STRING_B, list2.getByteString(1));
132 assertSame(BYTE_STRING_C, list2.getByteString(2));
135 public void testListCopyConstructor() {
136 List<String> list1 = new ArrayList<String>();
141 LazyStringArrayList list2 = new LazyStringArrayList(list1);
142 assertEquals(3, list2.size());
143 assertSame(STRING_A, list2.get(0));
144 assertSame(STRING_B, list2.get(1));
145 assertSame(STRING_C, list2.get(2));
148 public void testAddAllCopiesByReferenceIfPossible() {
149 LazyStringArrayList list1 = new LazyStringArrayList();
151 list1.add(BYTE_STRING_B);
152 list1.add(BYTE_STRING_C);
154 LazyStringArrayList list2 = new LazyStringArrayList();
157 assertEquals(3, list2.size());
158 assertSame(STRING_A, list2.get(0));
159 assertSame(BYTE_STRING_B, list2.getByteString(1));
160 assertSame(BYTE_STRING_C, list2.getByteString(2));