test/java/util/UUID/UUIDTest.java

Print this page
rev 9894 : 8006627: UUID to/from String performance should be improved by reducing object allocations


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

















  99     }
 100 
 101     private static void versionTest() throws Exception {
 102         UUID test = UUID.randomUUID();
 103         if (test.version() != 4)
 104             throw new Exception("randomUUID not type 4");
 105         Random byteSource = new Random();
 106         byte[] someBytes = new byte[12];
 107         byteSource.nextBytes(someBytes);
 108         test = UUID.nameUUIDFromBytes(someBytes);
 109         if (test.version() != 3)
 110             throw new Exception("nameUUIDFromBytes not type 3");
 111         test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda");
 112         if (test.version() != 1)
 113             throw new Exception("wrong version fromString 1");
 114         test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda");
 115         if (test.version() != 2)
 116             throw new Exception("wrong version fromString 2");
 117         test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda");
 118         if (test.version() != 3)




  79             UUID u1 = UUID.nameUUIDFromBytes(someBytes);
  80             if (3 != u1.version()) {
  81                 throw new Exception("bad version");
  82             }
  83             if (2 != u1.variant()) {
  84                 throw new Exception("bad variant");
  85             }
  86             if (list.contains(u1))
  87                 throw new Exception("byte UUID collision very unlikely");
  88             list.add(u1);
  89         }
  90     }
  91 
  92     private static void stringTest() throws Exception {
  93         for (int i=0; i<100; i++) {
  94             UUID u1 = UUID.randomUUID();
  95             UUID u2 = UUID.fromString(u1.toString());
  96             if (!u1.equals(u2))
  97                 throw new Exception("UUID -> string -> UUID failed");
  98         }
  99 
 100         testFromStringError("-0");
 101         testFromStringError("x");
 102         testFromStringError("----");
 103         testFromStringError("-0-0-0-0");
 104         testFromStringError("0-0-0-0-");
 105         testFromStringError("0-0-0-0-0-");
 106         testFromStringError("0-0-0-0-x");
 107     }
 108 
 109     private static void testFromStringError(String str) {
 110         try {
 111             UUID test = UUID.fromString(str);
 112             throw new RuntimeException("Should have thrown IAE");
 113         } catch (IllegalArgumentException iae) {
 114             // pass
 115         }
 116     }
 117 
 118     private static void versionTest() throws Exception {
 119         UUID test = UUID.randomUUID();
 120         if (test.version() != 4)
 121             throw new Exception("randomUUID not type 4");
 122         Random byteSource = new Random();
 123         byte[] someBytes = new byte[12];
 124         byteSource.nextBytes(someBytes);
 125         test = UUID.nameUUIDFromBytes(someBytes);
 126         if (test.version() != 3)
 127             throw new Exception("nameUUIDFromBytes not type 3");
 128         test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda");
 129         if (test.version() != 1)
 130             throw new Exception("wrong version fromString 1");
 131         test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda");
 132         if (test.version() != 2)
 133             throw new Exception("wrong version fromString 2");
 134         test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda");
 135         if (test.version() != 3)