PageRenderTime 16ms CodeModel.GetById 8ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/java/src/test/java/com/google/protobuf/LazyStringArrayListTest.java

http://github.com/tomahawk-player/tomahawk
Java | 118 lines | 60 code | 18 blank | 40 comment | 0 complexity | 19ac921d08def17165d79d518061be24 MD5 | raw file
  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
 31package com.google.protobuf;
 32
 33import junit.framework.TestCase;
 34
 35/**
 36 * Tests for {@link LazyStringArrayList}.
 37 *
 38 * @author jonp@google.com (Jon Perlow)
 39 */
 40public class LazyStringArrayListTest extends TestCase {
 41
 42  private static String STRING_A = "A";
 43  private static String STRING_B = "B";
 44  private static String STRING_C = "C";
 45
 46  private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A");
 47  private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B");
 48  private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C");
 49
 50  public void testJustStrings() {
 51    LazyStringArrayList list = new LazyStringArrayList();
 52    list.add(STRING_A);
 53    list.add(STRING_B);
 54    list.add(STRING_C);
 55
 56    assertEquals(3, list.size());
 57    assertSame(STRING_A, list.get(0));
 58    assertSame(STRING_B, list.get(1));
 59    assertSame(STRING_C, list.get(2));
 60
 61    list.set(1, STRING_C);
 62    assertSame(STRING_C, list.get(1));
 63
 64    list.remove(1);
 65    assertSame(STRING_A, list.get(0));
 66    assertSame(STRING_C, list.get(1));
 67  }
 68
 69  public void testJustByteString() {
 70    LazyStringArrayList list = new LazyStringArrayList();
 71    list.add(BYTE_STRING_A);
 72    list.add(BYTE_STRING_B);
 73    list.add(BYTE_STRING_C);
 74
 75    assertEquals(3, list.size());
 76    assertSame(BYTE_STRING_A, list.getByteString(0));
 77    assertSame(BYTE_STRING_B, list.getByteString(1));
 78    assertSame(BYTE_STRING_C, list.getByteString(2));
 79
 80    list.remove(1);
 81    assertSame(BYTE_STRING_A, list.getByteString(0));
 82    assertSame(BYTE_STRING_C, list.getByteString(1));
 83  }
 84
 85  public void testConversionBackAndForth() {
 86    LazyStringArrayList list = new LazyStringArrayList();
 87    list.add(STRING_A);
 88    list.add(BYTE_STRING_B);
 89    list.add(BYTE_STRING_C);
 90
 91    // String a should be the same because it was originally a string
 92    assertSame(STRING_A, list.get(0));
 93
 94    // String b and c should be different because the string has to be computed
 95    // from the ByteString
 96    String bPrime = list.get(1);
 97    assertNotSame(STRING_B, bPrime);
 98    assertEquals(STRING_B, bPrime);
 99    String cPrime = list.get(2);
100    assertNotSame(STRING_C, cPrime);
101    assertEquals(STRING_C, cPrime);
102
103    // String c and c should stay the same once cached.
104    assertSame(bPrime, list.get(1));
105    assertSame(cPrime, list.get(2));
106
107    // ByteString needs to be computed from string for both a and b
108    ByteString aPrimeByteString = list.getByteString(0);
109    assertEquals(BYTE_STRING_A, aPrimeByteString);
110    ByteString bPrimeByteString = list.getByteString(1);
111    assertNotSame(BYTE_STRING_B, bPrimeByteString);
112    assertEquals(BYTE_STRING_B, list.getByteString(1));
113
114    // Once cached, ByteString should stay cached.
115    assertSame(aPrimeByteString, list.getByteString(0));
116    assertSame(bPrimeByteString, list.getByteString(1));
117  }
118}