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.test;
32 import com.google.protobuf.*;
34 import junit.framework.TestCase;
36 import java.util.ArrayList;
37 import java.util.List;
40 * Tests for {@link LazyStringArrayList}.
42 * @author jonp@google.com (Jon Perlow)
44 public class LazyStringArrayListTest extends TestCase {
46 private static String STRING_A = "A";
47 private static String STRING_B = "B";
48 private static String STRING_C = "C";
50 private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A");
51 private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B");
52 private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C");
54 public void testJustStrings() {
55 LazyStringArrayList list = new LazyStringArrayList();
60 assertEquals(3, list.size());
61 assertSame(STRING_A, list.get(0));
62 assertSame(STRING_B, list.get(1));
63 assertSame(STRING_C, list.get(2));
65 list.set(1, STRING_C);
66 assertSame(STRING_C, list.get(1));
69 assertSame(STRING_A, list.get(0));
70 assertSame(STRING_C, list.get(1));
73 public void testJustByteString() {
74 LazyStringArrayList list = new LazyStringArrayList();
75 list.add(BYTE_STRING_A);
76 list.add(BYTE_STRING_B);
77 list.add(BYTE_STRING_C);
79 assertEquals(3, list.size());
80 assertSame(BYTE_STRING_A, list.getByteString(0));
81 assertSame(BYTE_STRING_B, list.getByteString(1));
82 assertSame(BYTE_STRING_C, list.getByteString(2));
85 assertSame(BYTE_STRING_A, list.getByteString(0));
86 assertSame(BYTE_STRING_C, list.getByteString(1));
89 public void testConversionBackAndForth() {
90 LazyStringArrayList list = new LazyStringArrayList();
92 list.add(BYTE_STRING_B);
93 list.add(BYTE_STRING_C);
95 // String a should be the same because it was originally a string
96 assertSame(STRING_A, list.get(0));
98 // String b and c should be different because the string has to be computed
99 // from the ByteString
100 String bPrime = list.get(1);
101 assertNotSame(STRING_B, bPrime);
102 assertEquals(STRING_B, bPrime);
103 String cPrime = list.get(2);
104 assertNotSame(STRING_C, cPrime);
105 assertEquals(STRING_C, cPrime);
107 // String c and c should stay the same once cached.
108 assertSame(bPrime, list.get(1));
109 assertSame(cPrime, list.get(2));
111 // ByteString needs to be computed from string for both a and b
112 ByteString aPrimeByteString = list.getByteString(0);
113 assertEquals(BYTE_STRING_A, aPrimeByteString);
114 ByteString bPrimeByteString = list.getByteString(1);
115 assertNotSame(BYTE_STRING_B, bPrimeByteString);
116 assertEquals(BYTE_STRING_B, list.getByteString(1));
118 // Once cached, ByteString should stay cached.
119 assertSame(aPrimeByteString, list.getByteString(0));
120 assertSame(bPrimeByteString, list.getByteString(1));
123 public void testCopyConstructorCopiesByReference() {
124 LazyStringArrayList list1 = new LazyStringArrayList();
126 list1.add(BYTE_STRING_B);
127 list1.add(BYTE_STRING_C);
129 LazyStringArrayList list2 = new LazyStringArrayList(list1);
130 assertEquals(3, list2.size());
131 assertSame(STRING_A, list2.get(0));
132 assertSame(BYTE_STRING_B, list2.getByteString(1));
133 assertSame(BYTE_STRING_C, list2.getByteString(2));
136 public void testListCopyConstructor() {
137 List<String> list1 = new ArrayList<String>();
142 LazyStringArrayList list2 = new LazyStringArrayList(list1);
143 assertEquals(3, list2.size());
144 assertSame(STRING_A, list2.get(0));
145 assertSame(STRING_B, list2.get(1));
146 assertSame(STRING_C, list2.get(2));
149 public void testAddAllCopiesByReferenceIfPossible() {
150 LazyStringArrayList list1 = new LazyStringArrayList();
152 list1.add(BYTE_STRING_B);
153 list1.add(BYTE_STRING_C);
155 LazyStringArrayList list2 = new LazyStringArrayList();
158 assertEquals(3, list2.size());
159 assertSame(STRING_A, list2.get(0));
160 assertSame(BYTE_STRING_B, list2.getByteString(1));
161 assertSame(BYTE_STRING_C, list2.getByteString(2));