test/jdk/java/lang/Long/BitTwiddle.java
changeset 50211 5afedc9e4662
parent 47216 71c04702a3d5
equal deleted inserted replaced
50210:afb9bc5328a3 50211:5afedc9e4662
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    81             throw new RuntimeException("h");
    81             throw new RuntimeException("h");
    82         if (numberOfLeadingZeros(-1) != 0)
    82         if (numberOfLeadingZeros(-1) != 0)
    83             throw new RuntimeException("i");
    83             throw new RuntimeException("i");
    84         if (numberOfLeadingZeros(1) != (SIZE - 1))
    84         if (numberOfLeadingZeros(1) != (SIZE - 1))
    85             throw new RuntimeException("j");
    85             throw new RuntimeException("j");
       
    86         if (numberOfLeadingZeros(Long.MAX_VALUE) != 1)
       
    87             throw new RuntimeException("lzmax");
       
    88         if (numberOfLeadingZeros(Integer.MAX_VALUE + 1L) != (SIZE - 32))
       
    89             throw new RuntimeException("lz32");
    86 
    90 
    87         if (numberOfTrailingZeros(0) != SIZE)
    91         if (numberOfTrailingZeros(0) != SIZE)
    88             throw new RuntimeException("k");
    92             throw new RuntimeException("k");
    89         if (numberOfTrailingZeros(1) != 0)
    93         if (numberOfTrailingZeros(1) != 0)
    90             throw new RuntimeException("l");
    94             throw new RuntimeException("l");
    93 
    97 
    94         for (int i = 0; i < N; i++) {
    98         for (int i = 0; i < N; i++) {
    95             long x = rnd.nextLong();
    99             long x = rnd.nextLong();
    96             if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
   100             if (numberOfLeadingZeros(x) != numberOfTrailingZeros(reverse(x)))
    97                 throw new RuntimeException("n: " + toHexString(x));
   101                 throw new RuntimeException("n: " + toHexString(x));
       
   102         }
       
   103         for (long i = 1, r = SIZE - 1; i != 0; i <<= 1, r--) {
       
   104             if (numberOfLeadingZeros(i) != (int)r ||
       
   105                 numberOfTrailingZeros(i) != (SIZE - (int)r - 1) ||
       
   106                 numberOfLeadingZeros(i) != numberOfTrailingZeros(reverse(i))) {
       
   107                 throw new RuntimeException("lzx: " + toHexString(i));
       
   108             }
    98         }
   109         }
    99 
   110 
   100         if (bitCount(0) != 0)
   111         if (bitCount(0) != 0)
   101                 throw new RuntimeException("o");
   112                 throw new RuntimeException("o");
   102 
   113 
   150             if (bitCount(x) != bitCount(reverseBytes(x)))
   161             if (bitCount(x) != bitCount(reverseBytes(x)))
   151                 throw new RuntimeException("z: " + toHexString(x));
   162                 throw new RuntimeException("z: " + toHexString(x));
   152         }
   163         }
   153     }
   164     }
   154 
   165 
       
   166     private static final String ZEROS = "0".repeat(64);
   155     private static String leftpad(String s, int width) {
   167     private static String leftpad(String s, int width) {
   156         String r = s;
   168         return ZEROS.substring(0, width - s.length()) + s;
   157         for (int c = 0; c < width - s.length(); c++) {
       
   158             r = "0" + r;
       
   159         }
       
   160         return r;
       
   161     }
   169     }
   162 }
   170 }