1 /* 2 * Copyright (C) 2011 The Android Open Source Project 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 17 package libcore.java.util; 18 19 import java.io.InvalidObjectException; 20 import java.util.EnumSet; 21 import junit.framework.TestCase; 22 import libcore.util.SerializationTester; 23 24 public final class EnumSetTest extends TestCase { 25 public void testSmallEnumSetSerialization() { 26 assertTrue(Roshambo.values().length <= 64); 27 String s = "aced0005737200246a6176612e7574696c2e456e756d5365742453657269616c" 28 + "697a6174696f6e50726f78790507d3db7654cad10200024c000b656c656d656e7" 29 + "4547970657400114c6a6176612f6c616e672f436c6173733b5b0008656c656d65" 30 + "6e74737400115b4c6a6176612f6c616e672f456e756d3b7870767200266c69626" 31 + "36f72652e6a6176612e7574696c2e456e756d5365745465737424526f7368616d" 32 + "626f00000000000000001200007872000e6a6176612e6c616e672e456e756d000" 33 + "00000000000001200007870757200115b4c6a6176612e6c616e672e456e756d3b" 34 + "a88dea2d33d22f980200007870000000027e71007e0004740004524f434b7e710" 35 + "07e000474000853434953534f5253"; 36 EnumSet<Roshambo> set = EnumSet.of(Roshambo.ROCK, Roshambo.SCISSORS); 37 new SerializationTester<EnumSet<Roshambo>>(set, s).test(); 38 } 39 40 public void testLargeEnumSetSerialization() { 41 assertTrue(Element.values().length > 64); 42 String s = "aced0005737200246a6176612e7574696c2e456e756d5365742453657269616c" 43 + "697a6174696f6e50726f78790507d3db7654cad10200024c000b656c656d656e7" 44 + "4547970657400114c6a6176612f6c616e672f436c6173733b5b0008656c656d65" 45 + "6e74737400115b4c6a6176612f6c616e672f456e756d3b7870767200256c69626" 46 + "36f72652e6a6176612e7574696c2e456e756d5365745465737424456c656d656e" 47 + "7400000000000000001200007872000e6a6176612e6c616e672e456e756d00000" 48 + "000000000001200007870757200115b4c6a6176612e6c616e672e456e756d3ba8" 49 + "8dea2d33d22f980200007870000000047e71007e0004740001487e71007e00047" 50 + "4000254427e71007e000474000244597e71007e000474000355554f"; 51 EnumSet<Element> set = EnumSet.of(Element.H, Element.TB, Element.DY, Element.UUO); 52 new SerializationTester<EnumSet<Element>>(set, s).test(); 53 } 54 55 public void testDeserializeRemovedValue() throws Exception { 56 /* 57 * enum Roshambo { 58 * ROCK, PAPER, SCISSORS, LIZARD, SPOCK 59 * } 60 * EnumSet<Roshambo> set = EnumSet.of(Roshambo.SPOCK); 61 */ 62 String s = "aced0005737200246a6176612e7574696c2e456e756d5365742453657269616c" 63 + "697a6174696f6e50726f78790507d3db7654cad10200024c000b656c656d656e7" 64 + "4547970657400114c6a6176612f6c616e672f436c6173733b5b0008656c656d65" 65 + "6e74737400115b4c6a6176612f6c616e672f456e756d3b7870767200266c69626" 66 + "36f72652e6a6176612e7574696c2e456e756d5365745465737424526f7368616d" 67 + "626f00000000000000001200007872000e6a6176612e6c616e672e456e756d000" 68 + "00000000000001200007870757200115b4c6a6176612e6c616e672e456e756d3b" 69 + "a88dea2d33d22f980200007870000000017e71007e000474000553504f434b"; 70 try { 71 SerializationTester.deserializeHex(s); 72 fail(); 73 } catch (InvalidObjectException expected) { 74 } 75 } 76 77 public void testDeserializeShuffledOrdinals() throws Exception { 78 /* 79 * enum Roshambo { 80 * SCISSORS, ROCK, PAPER 81 * } 82 * EnumSet<Roshambo> set = EnumSet.of(Roshambo.SCISSORS, Roshambo.ROCK) 83 */ 84 String s = "aced0005737200246a6176612e7574696c2e456e756d5365742453657269616c" 85 + "697a6174696f6e50726f78790507d3db7654cad10200024c000b656c656d656e7" 86 + "4547970657400114c6a6176612f6c616e672f436c6173733b5b0008656c656d65" 87 + "6e74737400115b4c6a6176612f6c616e672f456e756d3b7870767200266c69626" 88 + "36f72652e6a6176612e7574696c2e456e756d5365745465737424526f7368616d" 89 + "626f00000000000000001200007872000e6a6176612e6c616e672e456e756d000" 90 + "00000000000001200007870757200115b4c6a6176612e6c616e672e456e756d3b" 91 + "a88dea2d33d22f980200007870000000027e71007e000474000853434953534f5" 92 + "2537e71007e0004740004524f434b"; 93 assertEquals(EnumSet.of(Roshambo.ROCK, Roshambo.SCISSORS), 94 SerializationTester.deserializeHex(s)); 95 } 96 97 enum Roshambo { 98 ROCK, PAPER, SCISSORS 99 } 100 101 enum Element { 102 H, HE, LI, BE, B, C, N, O, F, NE, NA, MG, AL, SI, P, S, CL, AR, K, CA, SC, TI, V, CR, MN, 103 FE, CO, NI, CU, ZN, GA, GE, AS, SE, BR, KR, RB, SR, Y, ZR, NB, MO, TC, RU, RH, PD, AG, CD, 104 IN, SN, SB, TE, I, XE, CS, BA, LA, CE, PR, ND, PM, SM, EU, GD, TB, DY, HO, ER, TM, YB, LU, 105 HF, TA, W, RE, OS, IR, PT, AU, HG, TL, PB, BI, PO, AT, RN, FR, RA, AC, TH, PA, U, NP, PU, 106 AM, CM, BK, CF, ES, FM, MD, NO, LR, RF, DB, SG, BH, HS, MT, DS, RG, CN, UUT, UUQ, UUP, UUH, 107 UUS, UUO 108 } 109 } 110