Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / protobuf / java / src / test / java / com / google / protobuf / LazyStringArrayListTest.java
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;
32
33 import junit.framework.TestCase;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38 /**
39  * Tests for {@link LazyStringArrayList}.
40  *
41  * @author jonp@google.com (Jon Perlow)
42  */
43 public class LazyStringArrayListTest extends TestCase {
44
45   private static String STRING_A = "A";
46   private static String STRING_B = "B";
47   private static String STRING_C = "C";
48
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");
52
53   public void testJustStrings() {
54     LazyStringArrayList list = new LazyStringArrayList();
55     list.add(STRING_A);
56     list.add(STRING_B);
57     list.add(STRING_C);
58
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));
63
64     list.set(1, STRING_C);
65     assertSame(STRING_C, list.get(1));
66
67     list.remove(1);
68     assertSame(STRING_A, list.get(0));
69     assertSame(STRING_C, list.get(1));
70   }
71
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);
77
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));
82
83     list.remove(1);
84     assertSame(BYTE_STRING_A, list.getByteString(0));
85     assertSame(BYTE_STRING_C, list.getByteString(1));
86   }
87
88   public void testConversionBackAndForth() {
89     LazyStringArrayList list = new LazyStringArrayList();
90     list.add(STRING_A);
91     list.add(BYTE_STRING_B);
92     list.add(BYTE_STRING_C);
93
94     // String a should be the same because it was originally a string
95     assertSame(STRING_A, list.get(0));
96
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);
105
106     // String c and c should stay the same once cached.
107     assertSame(bPrime, list.get(1));
108     assertSame(cPrime, list.get(2));
109
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));
116
117     // Once cached, ByteString should stay cached.
118     assertSame(aPrimeByteString, list.getByteString(0));
119     assertSame(bPrimeByteString, list.getByteString(1));
120   }
121
122   public void testCopyConstructorCopiesByReference() {
123     LazyStringArrayList list1 = new LazyStringArrayList();
124     list1.add(STRING_A);
125     list1.add(BYTE_STRING_B);
126     list1.add(BYTE_STRING_C);
127
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));
133   }
134
135   public void testListCopyConstructor() {
136     List<String> list1 = new ArrayList<String>();
137     list1.add(STRING_A);
138     list1.add(STRING_B);
139     list1.add(STRING_C);
140
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));
146   }
147
148   public void testAddAllCopiesByReferenceIfPossible() {
149     LazyStringArrayList list1 = new LazyStringArrayList();
150     list1.add(STRING_A);
151     list1.add(BYTE_STRING_B);
152     list1.add(BYTE_STRING_C);
153
154     LazyStringArrayList list2 = new LazyStringArrayList();
155     list2.addAll(list1);
156
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));
161   }
162 }