1/* 2 * Copyright (C) 2011 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.util; 18 19import java.util.NavigableSet; 20import java.util.SortedSet; 21import java.util.TreeSet; 22import junit.framework.TestCase; 23import libcore.util.SerializationTester; 24 25public final class TreeSetTest extends TestCase { 26 27 public void testEmptySetSerialization() { 28 String s = "aced0005737200116a6176612e7574696c2e54726565536574dd98509395ed87" 29 + "5b03000078707077040000000078"; 30 TreeSet<String> set = new TreeSet<String>(); 31 new SerializationTester<TreeSet<String>>(set, s).test(); 32 } 33 34 public void testSerializationWithComparator() { 35 String s = "aced0005737200116a6176612e7574696c2e54726565536574dd98509395ed87" 36 + "5b03000078707372002a6a6176612e6c616e672e537472696e672443617365496" 37 + "e73656e736974697665436f6d70617261746f7277035c7d5c50e5ce0200007870" 38 + "770400000002740001617400016278"; 39 TreeSet<String> set = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); 40 set.add("a"); 41 set.add("b"); 42 new SerializationTester<NavigableSet<String>>(set, s) { 43 @Override protected void verify(NavigableSet<String> deserialized) { 44 assertEquals(0, deserialized.comparator().compare("X", "x")); 45 } 46 }.test(); 47 } 48 49 public void testSubSetSerialization() { 50 String s = "aced0005737200116a6176612e7574696c2e54726565536574dd98509395ed87" 51 + "5b03000078707372002a6a6176612e6c616e672e537472696e672443617365496" 52 + "e73656e736974697665436f6d70617261746f7277035c7d5c50e5ce0200007870" 53 + "770400000002740001617400016278"; 54 TreeSet<String> set = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); 55 set.add("a"); 56 set.add("b"); 57 set.add("c"); 58 set.add("d"); 59 final SortedSet<String> subSet = set.subSet("a", "c"); 60 new SerializationTester<SortedSet<String>>(subSet, s) { 61 @Override protected void verify(SortedSet<String> deserialized) { 62 assertBounded(deserialized, deserialized == subSet); 63 } 64 }.test(); 65 } 66 67 public void testNavigableSubSetSerialization() { 68 String s = "aced0005737200116a6176612e7574696c2e54726565536574dd98509395ed87" 69 + "5b03000078707372002a6a6176612e6c616e672e537472696e672443617365496" 70 + "e73656e736974697665436f6d70617261746f7277035c7d5c50e5ce0200007870" 71 + "770400000002740001627400016378"; 72 TreeSet<String> set = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); 73 set.add("a"); 74 set.add("b"); 75 set.add("c"); 76 set.add("d"); 77 final SortedSet<String> subSet = set.subSet("a", false, "c", true); 78 new SerializationTester<SortedSet<String>>(subSet, s) { 79 @Override protected void verify(SortedSet<String> deserialized) { 80 assertBounded(deserialized, deserialized == subSet); 81 } 82 }.test(); 83 } 84 85 /** 86 * Regrettably, serializing a TreeSet causes it to forget its bounds. This 87 * is unlike a serialized TreeMap which retains its bounds when serialized. 88 */ 89 private void assertBounded(SortedSet<String> deserialized, boolean bounded) { 90 if (bounded) { 91 try { 92 deserialized.add("e"); 93 fail(); 94 } catch (IllegalArgumentException expected) { 95 } 96 } else { 97 assertTrue(deserialized.add("e")); 98 assertTrue(deserialized.remove("e")); 99 } 100 } 101 102 /** 103 * Test that TreeSet never attempts to serialize a non-serializable 104 * comparator. http://b/5552608 105 */ 106 public void testDescendingSetSerialization() { 107 String s = "aced0005737200116a6176612e7574696c2e54726565536574dd98509395ed87" 108 + "5b0300007870737200276a6176612e7574696c2e436f6c6c656374696f6e73245" 109 + "2657665727365436f6d70617261746f7264048af0534e4ad00200007870770400" 110 + "000002740001627400016178"; 111 TreeSet<String> set = new TreeSet<String>(); 112 set.add("a"); 113 set.add("b"); 114 NavigableSet<String> descendingSet = set.descendingSet(); 115 new SerializationTester<NavigableSet<String>>(descendingSet, s) { 116 @Override protected void verify(NavigableSet<String> deserialized) { 117 assertEquals("b", deserialized.first()); 118 } 119 }.test(); 120 } 121 122 public void testJava5SerializationWithComparator() { 123 String s = "aced0005737200116a6176612e7574696c2e54726565536574dd98509395ed87" 124 + "5b03000078707372002a6a6176612e6c616e672e537472696e672443617365496" 125 + "e73656e736974697665436f6d70617261746f7277035c7d5c50e5ce0200007870" 126 + "770400000002740001617400016278"; 127 TreeSet<String> set = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); 128 set.add("a"); 129 set.add("b"); 130 new SerializationTester<TreeSet<String>>(set, s) { 131 @Override protected void verify(TreeSet<String> deserialized) { 132 assertEquals(0, deserialized.comparator().compare("X", "x")); 133 } 134 }.test(); 135 } 136} 137