< prev index next >

test/jdk/java/util/UUID/UUIDTest.java

Print this page
rev 58200 : 8196334: Optimize UUID#fromString
Reviewed-by: TBD
Contributed-by: plokhotnyuk@gmail.com, jon.chambers@gmail.com, claes.redestad@oracle.com


  78         byte[] someBytes = new byte[12];
  79         List list = new LinkedList();
  80         for (int i=0; i<100; i++) {
  81             byteSource.nextBytes(someBytes);
  82             UUID u1 = UUID.nameUUIDFromBytes(someBytes);
  83             if (3 != u1.version()) {
  84                 throw new Exception("bad version");
  85             }
  86             if (2 != u1.variant()) {
  87                 throw new Exception("bad variant");
  88             }
  89             if (list.contains(u1))
  90                 throw new Exception("byte UUID collision very unlikely");
  91             list.add(u1);
  92         }
  93     }
  94 
  95     private static void stringTest() throws Exception {
  96         for (int i=0; i<100; i++) {
  97             UUID u1 = UUID.randomUUID();
  98             UUID u2 = UUID.fromString(u1.toString());
  99             if (!u1.equals(u2))

 100                 throw new Exception("UUID -> string -> UUID failed");
 101         }
 102 
 103         testFromStringError("-0");
 104         testFromStringError("x");
 105         testFromStringError("----");
 106         testFromStringError("-0-0-0-0");
 107         testFromStringError("0-0-0-0-");
 108         testFromStringError("0-0-0-0-0-");
 109         testFromStringError("0-0-0-0-x");
 110     }
 111 
 112     private static void testFromStringError(String str) {
 113         try {
 114             UUID test = UUID.fromString(str);
 115             throw new RuntimeException("Should have thrown IAE");
 116         } catch (IllegalArgumentException iae) {
 117             // pass
 118         }
 119     }




  78         byte[] someBytes = new byte[12];
  79         List list = new LinkedList();
  80         for (int i=0; i<100; i++) {
  81             byteSource.nextBytes(someBytes);
  82             UUID u1 = UUID.nameUUIDFromBytes(someBytes);
  83             if (3 != u1.version()) {
  84                 throw new Exception("bad version");
  85             }
  86             if (2 != u1.variant()) {
  87                 throw new Exception("bad variant");
  88             }
  89             if (list.contains(u1))
  90                 throw new Exception("byte UUID collision very unlikely");
  91             list.add(u1);
  92         }
  93     }
  94 
  95     private static void stringTest() throws Exception {
  96         for (int i=0; i<100; i++) {
  97             UUID u1 = UUID.randomUUID();
  98             UUID u2 = UUID.fromString(u1.toString().toLowerCase());
  99             UUID u3 = UUID.fromString(u1.toString().toUpperCase());
 100             if (!u1.equals(u2) || !u1.equals(u3))
 101                 throw new Exception("UUID -> string -> UUID failed");
 102         }
 103 
 104         testFromStringError("-0");
 105         testFromStringError("x");
 106         testFromStringError("----");
 107         testFromStringError("-0-0-0-0");
 108         testFromStringError("0-0-0-0-");
 109         testFromStringError("0-0-0-0-0-");
 110         testFromStringError("0-0-0-0-x");
 111     }
 112 
 113     private static void testFromStringError(String str) {
 114         try {
 115             UUID test = UUID.fromString(str);
 116             throw new RuntimeException("Should have thrown IAE");
 117         } catch (IllegalArgumentException iae) {
 118             // pass
 119         }
 120     }


< prev index next >