1 /*
   2  * Copyright 2010 Google, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /*
  26  * @test
  27  * @bug 6946040
  28  * @summary Tests Character/Short.reverseBytes and their intrinsics implementation in the server compiler
  29  * @run main/othervm -Xbatch -XX:CompileOnly=.testChar,.testShort TestCharShortByteSwap
  30  */
  31 
  32 // This test must run without any command line arguments.
  33 
  34 public class TestCharShortByteSwap {
  35 
  36   private static short initShort(String[] args, short v) {
  37     if (args.length > 0) {
  38       try {
  39         return (short) Integer.valueOf(args[0]).intValue();
  40       } catch (NumberFormatException e) { }
  41     }
  42     return v;
  43   }
  44 
  45   private static char initChar(String[] args, char v) {
  46     if (args.length > 0) {
  47       try {
  48         return (char) Integer.valueOf(args[0]).intValue();
  49       } catch (NumberFormatException e) { }
  50     }
  51     return v;
  52   }
  53 
  54   private static void testChar(char a, char b) {
  55     if (a != Character.reverseBytes(b)) {
  56       throw new RuntimeException("FAIL: " + (int)a + " != Character.reverseBytes(" + (int)b + ")");
  57     }
  58     if (b != Character.reverseBytes(a)) {
  59       throw new RuntimeException("FAIL: " + (int)b + " != Character.reverseBytes(" + (int)a + ")");
  60     }
  61   }
  62 
  63   private static void testShort(short a, short b) {
  64     if (a != Short.reverseBytes(b)) {
  65       throw new RuntimeException("FAIL: " + (int)a + " != Short.reverseBytes(" + (int)b + ")");
  66     }
  67     if (b != Short.reverseBytes(a)) {
  68       throw new RuntimeException("FAIL: " + (int)b + " != Short.reverseBytes(" + (int)a + ")");
  69     }
  70   }
  71 
  72   public static void main(String[] args) {
  73     for (int i = 0; i < 100000; ++i) { // Trigger compilation
  74       char c1 = initChar(args, (char) 0x0123);
  75       char c2 = initChar(args, (char) 0x2301);
  76       char c3 = initChar(args, (char) 0xaabb);
  77       char c4 = initChar(args, (char) 0xbbaa);
  78       short s1 = initShort(args, (short) 0x0123);
  79       short s2 = initShort(args, (short) 0x2301);
  80       short s3 = initShort(args, (short) 0xaabb);
  81       short s4 = initShort(args, (short) 0xbbaa);
  82       testChar(c1, c2);
  83       testChar(c3, c4);
  84       testShort(s1, s2);
  85       testShort(s3, s4);
  86     }
  87   }
  88 }