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