OLD | NEW |
| (Empty) |
1 // Protocol Buffers - Google's data interchange format | |
2 // Copyright 2008 Google Inc. All rights reserved. | |
3 // http://code.google.com/p/protobuf/ | |
4 // | |
5 // Redistribution and use in source and binary forms, with or without | |
6 // modification, are permitted provided that the following conditions are | |
7 // met: | |
8 // | |
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 | |
14 // distribution. | |
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. | |
18 // | |
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. | |
30 | |
31 package com.google.protobuf.test; | |
32 import com.google.protobuf.*; | |
33 | |
34 import junit.framework.TestCase; | |
35 | |
36 import java.util.ArrayList; | |
37 import java.util.List; | |
38 | |
39 /** | |
40 * Tests for {@link LazyStringArrayList}. | |
41 * | |
42 * @author jonp@google.com (Jon Perlow) | |
43 */ | |
44 public class LazyStringArrayListTest extends TestCase { | |
45 | |
46 private static String STRING_A = "A"; | |
47 private static String STRING_B = "B"; | |
48 private static String STRING_C = "C"; | |
49 | |
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"); | |
53 | |
54 public void testJustStrings() { | |
55 LazyStringArrayList list = new LazyStringArrayList(); | |
56 list.add(STRING_A); | |
57 list.add(STRING_B); | |
58 list.add(STRING_C); | |
59 | |
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)); | |
64 | |
65 list.set(1, STRING_C); | |
66 assertSame(STRING_C, list.get(1)); | |
67 | |
68 list.remove(1); | |
69 assertSame(STRING_A, list.get(0)); | |
70 assertSame(STRING_C, list.get(1)); | |
71 } | |
72 | |
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); | |
78 | |
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)); | |
83 | |
84 list.remove(1); | |
85 assertSame(BYTE_STRING_A, list.getByteString(0)); | |
86 assertSame(BYTE_STRING_C, list.getByteString(1)); | |
87 } | |
88 | |
89 public void testConversionBackAndForth() { | |
90 LazyStringArrayList list = new LazyStringArrayList(); | |
91 list.add(STRING_A); | |
92 list.add(BYTE_STRING_B); | |
93 list.add(BYTE_STRING_C); | |
94 | |
95 // String a should be the same because it was originally a string | |
96 assertSame(STRING_A, list.get(0)); | |
97 | |
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); | |
106 | |
107 // String c and c should stay the same once cached. | |
108 assertSame(bPrime, list.get(1)); | |
109 assertSame(cPrime, list.get(2)); | |
110 | |
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)); | |
117 | |
118 // Once cached, ByteString should stay cached. | |
119 assertSame(aPrimeByteString, list.getByteString(0)); | |
120 assertSame(bPrimeByteString, list.getByteString(1)); | |
121 } | |
122 | |
123 public void testCopyConstructorCopiesByReference() { | |
124 LazyStringArrayList list1 = new LazyStringArrayList(); | |
125 list1.add(STRING_A); | |
126 list1.add(BYTE_STRING_B); | |
127 list1.add(BYTE_STRING_C); | |
128 | |
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)); | |
134 } | |
135 | |
136 public void testListCopyConstructor() { | |
137 List<String> list1 = new ArrayList<String>(); | |
138 list1.add(STRING_A); | |
139 list1.add(STRING_B); | |
140 list1.add(STRING_C); | |
141 | |
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)); | |
147 } | |
148 | |
149 public void testAddAllCopiesByReferenceIfPossible() { | |
150 LazyStringArrayList list1 = new LazyStringArrayList(); | |
151 list1.add(STRING_A); | |
152 list1.add(BYTE_STRING_B); | |
153 list1.add(BYTE_STRING_C); | |
154 | |
155 LazyStringArrayList list2 = new LazyStringArrayList(); | |
156 list2.addAll(list1); | |
157 | |
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)); | |
162 } | |
163 } | |
OLD | NEW |