1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. 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 package org.graalvm.compiler.jtt.optimize;
  24 
  25 import org.junit.Test;
  26 
  27 import org.graalvm.compiler.jtt.JTTTest;
  28 
  29 /**
  30  * inspired by java.security.SecureRandom.longToByteArray(long).
  31  *
  32  */
  33 public class LongToSomethingArray01 extends JTTTest {
  34 
  35     public static byte[] longToByteArray(long arg) {
  36         long l = arg;
  37         byte[] ret = new byte[8];
  38         for (int i = 0; i < 8; i++) {
  39             ret[i] = (byte) (l & 0xff);
  40             l = l >> 8;
  41         }
  42         return ret;
  43     }
  44 
  45     @Test
  46     public void runB0() throws Throwable {
  47         runTest("longToByteArray", 0x1122_3344_5566_7788L);
  48     }
  49 
  50     public static short[] longToShortArray(long arg) {
  51         long l = arg;
  52         short[] ret = new short[4];
  53         for (int i = 0; i < 4; i++) {
  54             ret[i] = (short) (l & 0xffff);
  55             l = l >> 16;
  56         }
  57         return ret;
  58     }
  59 
  60     @Test
  61     public void runS0() throws Throwable {
  62         runTest("longToShortArray", 0x1122_3344_5566_7788L);
  63     }
  64 
  65     public static int[] longToIntArray(long arg) {
  66         long l = arg;
  67         int[] ret = new int[2];
  68         for (int i = 0; i < 2; i++) {
  69             ret[i] = (int) (l & 0xffff_ffff);
  70             l = l >> 32;
  71         }
  72         return ret;
  73     }
  74 
  75     @Test
  76     public void runI0() throws Throwable {
  77         runTest("longToIntArray", 0x1122_3344_5566_7788L);
  78     }
  79 
  80     public static long[] longToLongArray(long arg) {
  81         long l = arg;
  82         long[] ret = new long[1];
  83         for (int i = 0; i < 1; i++) {
  84             ret[i] = l & 0xffff_ffff_ffff_ffffL;
  85             l = l >> 64;
  86         }
  87         return ret;
  88     }
  89 
  90     @Test
  91     public void runL0() throws Throwable {
  92         runTest("longToLongArray", 0x1122_3344_5566_7788L);
  93     }
  94 }