Ejemplo n.º 1
1
  @Test
  public final void testTotalBoxedLongBoxedLongBoxedLongBoxedLongBoxedLongMoreBoxedLongs() {
    final Long[] numbers1 = convertToBoxed(NUMBERS1);
    final Long[] numbers2 = convertToBoxed(NUMBERS2);

    for (final Long n1 : numbers1) {
      for (final Long n2 : numbers2) {
        for (final Long n3 : numbers1) {
          for (final Long n4 : numbers2) {
            for (final Long n5 : numbers1) {
              @SuppressWarnings("boxing")
              final Long expected1 = (n1 + n2 + n3 + n4 + n5 + n1 + n2 + n3 + n4 + n5);
              @SuppressWarnings("boxing")
              final Long actual1 = total(n1, n2, n3, n4, n5, n1, n2, n3, n4, n5);
              // System.out.println("expected: " + expected1 + "\nactual:   " + actual1);
              assertThat(actual1, is(equalTo(expected1)));

              @SuppressWarnings("boxing")
              final Long expected2 = (n1 + n2 + n3 + n4 + n5 + n1 + n2 + n3 + n4 + n5);
              @SuppressWarnings("boxing")
              final Long actual2 = total(n1, n2, n3, n4, n5, n1, n2, n3, n4, n5);
              // System.out.println("expected: " + expected2 + "\nactual:   " + actual2);
              assertThat(actual2, is(equalTo(expected2)));
            }
          }
        }
      }
    }

    final Long[] numbers3 = new Long[NUMBERS3.length];
    System.arraycopy(convertToBoxed(NUMBERS3), 0, numbers3, 0, NUMBERS3.length);
    final Long[] numbers4 = new Long[NUMBERS4.length];
    System.arraycopy(convertToBoxed(NUMBERS4), 0, numbers4, 0, NUMBERS4.length);

    for (final Long n1 : numbers3) {
      for (final Long n2 : numbers4) {
        for (final Long n3 : numbers3) {
          for (final Long n4 : numbers4) {
            for (final Long n5 : numbers3) {
              @SuppressWarnings("boxing")
              final Long expected3 = (n1 + n2 + n3 + n4 + n5 + n1 + n2 + n3 + n4 + n5);
              @SuppressWarnings("boxing")
              final Long actual3 = total(n1, n2, n3, n4, n5, n1, n2, n3, n4, n5);
              // System.out.println("expected: " + expected3 + "\nactual:   " + actual3);
              assertThat(actual3, is(equalTo(expected3)));

              @SuppressWarnings("boxing")
              final Long expected4 = (n1 + n2 + n3 + n4 + n5 + n1 + n2 + n3 + n4 + n5);
              @SuppressWarnings("boxing")
              final Long actual4 = total(n1, n2, n3, n4, n5, n1, n2, n3, n4, n5);
              // System.out.println("expected: " + expected4 + "\nactual:   " + actual4);
              assertThat(actual4, is(equalTo(expected4)));
            }
          }
        }
      }
    }
  }
Ejemplo n.º 2
1
  @Test
  public final void testSumLongLongLongLongLongLongArray() {
    final long[] numbers1 = NUMBERS1;
    final long[] numbers2 = NUMBERS2;

    for (final long n1 : numbers1) {
      for (final long n2 : numbers2) {
        for (final long n3 : numbers1) {
          for (final long n4 : numbers2) {
            for (final long n5 : numbers1) {

              long total1 = n1 + n2 + n3 + n4 + n5;
              for (final long n : numbers1) total1 += n;

              @SuppressWarnings("boxing")
              final Long expected1 = total1;
              @SuppressWarnings("boxing")
              final Long actual1 = sum(n1, n2, n3, n4, n5, numbers1);
              // System.out.println("expected: " + expected1 + "\nactual:   " + actual1);
              assertThat(actual1, is(equalTo(expected1)));

              long total2 = n1 + n2 + n3 + n4 + n5;
              for (final long n : numbers2) total2 += n;

              @SuppressWarnings("boxing")
              final Long expected2 = total2;
              @SuppressWarnings("boxing")
              final Long actual2 = sum(n1, n2, n3, n4, n5, numbers2);
              // System.out.println("expected: " + expected2 + "\nactual:   " + actual2);
              assertThat(actual2, is(equalTo(expected2)));
            }
          }
        }
      }
    }

    final long[] numbers3 = new long[NUMBERS3.length];
    System.arraycopy(NUMBERS3, 0, numbers3, 0, NUMBERS3.length);
    final long[] numbers4 = new long[NUMBERS4.length];
    System.arraycopy(NUMBERS4, 0, numbers4, 0, NUMBERS4.length);

    for (final long n1 : numbers3) {
      for (final long n2 : numbers4) {
        for (final long n3 : numbers3) {
          for (final long n4 : numbers4) {
            for (final long n5 : numbers3) {
              long total3 = n1 + n2 + n3 + n4 + n5;
              for (final long n : numbers3) total3 += n;

              @SuppressWarnings("boxing")
              final Long expected3 = total3;
              @SuppressWarnings("boxing")
              final Long actual3 = sum(n1, n2, n3, n4, n5, numbers3);
              // System.out.println("expected: " + expected3 + "\nactual:   " + actual3);
              assertThat(actual3, is(equalTo(expected3)));

              long total4 = n1 + n2 + n3 + n4 + n5;
              for (final long n : numbers4) total4 += n;

              @SuppressWarnings("boxing")
              final Long expected4 = total4;
              @SuppressWarnings("boxing")
              final Long actual4 = sum(n1, n2, n3, n4, n5, numbers4);
              // System.out.println("expected: " + expected4 + "\nactual:   " + actual4);
              assertThat(actual4, is(equalTo(expected4)));
            }
          }
        }
      }
    }
  }
Ejemplo n.º 3
0
  @Test
  public void test_checkMove_Correct() throws GameMoveException {
    final Dictionary dictionary = createDictionary("abcd", "def", "fefgabcd");
    final TilesBank tilesBank = new TilesBank(editor.createTilesBankInfo());
    final ScribbleBoard board =
        new ScribbleBoard(
            settings, Arrays.asList(player1, player2, player3), tilesBank, dictionary);
    h1 = board.getPlayerHand(player1);
    h2 = board.getPlayerHand(player2);
    h3 = board.getPlayerHand(player3);

    h1.setTiles(tilesBank.getTiles(0, 3, 6, 9, 12, 15, 18)); // abcdefg
    h2.setTiles(tilesBank.getTiles(1, 4, 7, 10, 13, 16, 19)); // abcdefg
    h3.setTiles(tilesBank.getTiles(2, 5, 8, 11, 14, 17, 20)); // abcdefg

    Personality person = board.getPlayerTurn();
    ScribblePlayerHand hand = board.getPlayerHand(person);
    final Tile[] moveTiles1 = Arrays.copyOf(hand.getTiles(), 4);
    board.makeTurn(person, new Word(new Position(7, 7), Direction.HORIZONTAL, moveTiles1)); // abcd
    assertEquals(1, board.getGameMoves().size());
    assertEquals(9, tilesBank.getTilesLimit());
    assertEquals(7, hand.getTiles().length);
    assertEquals(10, hand.getPoints());

    person = board.getPlayerTurn();
    hand = board.getPlayerHand(person);
    final Tile[] moveTiles2 = new Tile[3];
    moveTiles2[0] = moveTiles1[3]; // last 'd' letter
    System.arraycopy(hand.getTiles(), 4, moveTiles2, 1, 2);
    board.makeTurn(person, new Word(new Position(7, 10), Direction.HORIZONTAL, moveTiles2)); // def
    assertEquals(2, board.getGameMoves().size());
    assertEquals(7, tilesBank.getTilesLimit());
    assertEquals(7, hand.getTiles().length);
    assertEquals(30, hand.getPoints());

    person = board.getPlayerTurn();
    hand = board.getPlayerHand(person);
    final Tile[] moveTiles3 = new Tile[8];
    moveTiles3[0] = moveTiles2[2]; // last 'f' letter
    System.arraycopy(hand.getTiles(), 4, moveTiles3, 1, 3);
    System.arraycopy(hand.getTiles(), 0, moveTiles3, 4, 4);
    board.makeTurn(
        person, new Word(new Position(7, 12), Direction.VERTICAL, moveTiles3)); // fefgabcd

    assertEquals(3, board.getGameMoves().size());
    assertEquals(0, tilesBank.getTilesLimit());
    assertEquals(7, hand.getTiles().length);
    assertEquals(74, hand.getPoints()); // 6 + 5 + 6 + 7*2 + 1 + 2 + 3*2 + 4 +(30-all person) = 74
  }
Ejemplo n.º 4
0
 private LogBuffer flipAndPut() {
   ensureArrayCapacityPlus(bufferForConversions.limit());
   System.arraycopy(
       bufferForConversions.flip().array(), 0, bytes, writeIndex, bufferForConversions.limit());
   writeIndex += bufferForConversions.limit();
   return this;
 }
Ejemplo n.º 5
0
 private void ensureArrayCapacityPlus(int plus) {
   while (writeIndex + plus > bytes.length) {
     byte[] tmp = bytes;
     bytes = new byte[bytes.length * 2];
     System.arraycopy(tmp, 0, bytes, 0, tmp.length);
   }
 }
Ejemplo n.º 6
0
  @Test
  // Test array sort property that sorted array will contain
  // all elements in original array
  public void testArraySameContents() {
    int[][] arrs = generateRandomArrays();

    // loop through all arrays
    for (int index = 0; index < arrs.length; index++) {
      int[] testArr = arrs[index];
      int[] preSortedArray = new int[testArr.length];

      // create copy for comparison
      System.arraycopy(testArr, 0, preSortedArray, 0, testArr.length);

      // pass array to Arrays.sort
      Arrays.sort(testArr);

      // check that all elements in sorted array are in original array
      for (int i = 0; i < preSortedArray.length; i++) {
        if (Arrays.binarySearch(testArr, preSortedArray[i]) < 0) {
          fail("testArraySameContents");
        }
      }
    }
  }
  @Test(timeout = TIMEOUT)
  public void testQuickSort() {
    DValue[] arrZero = new DValue[0];
    Sorting.quickSort(arrZero, new BasicComparator(), new Random());
    assertEquals(0, arrZero.length);
    DValue[] arrOne = new DValue[1];
    arrOne[0] = new DValue(4, 0);
    Sorting.quickSort(arrOne, new BasicComparator(), new Random());
    assertEquals(1, arrOne.length);
    assertEquals(4, arrOne[0].val.intValue());

    Random rand = new Random();
    HashMap<Integer, Integer> values = new HashMap<Integer, Integer>();
    for (int i = 0; i < 50; i++) {
      int arrlen = rand.nextInt(1000) + 2;
      DValue[] arrMany = new DValue[arrlen];
      DValue[] arrManySorted = new DValue[arrlen];
      for (int j = 0; j < arrlen; j++) {
        arrMany[j] = new DValue(rand.nextInt(200) - 90, 0);
        if (values.containsKey(arrMany[j].val)) {
          arrMany[j].count = values.get(arrMany[j].val) + 1;
          values.put(arrMany[j].val, arrMany[j].count);
        } else {
          values.put(arrMany[j].val, 0);
        }
      }
      System.arraycopy(arrMany, 0, arrManySorted, 0, arrlen);
      Arrays.sort(arrManySorted);
      Sorting.quickSort(arrMany, new BasicComparator(), new Random());
      assertArrayEquals(arrManySorted, arrMany);
    }
  }
  @Test(timeout = TIMEOUT)
  public void testRadixSort() {
    int[] arrZero = new int[0];
    arrZero = Sorting.radixSort(arrZero);
    assertEquals(0, arrZero.length);
    int[] arrOne = new int[1];
    arrOne[0] = 4;
    arrOne = Sorting.radixSort(arrOne);
    assertEquals(1, arrOne.length);
    assertEquals(4, arrOne[0]);

    Random rand = new Random();
    for (int i = 0; i < 50; i++) {
      int arrlen = rand.nextInt(1000) + 2;
      int[] arrMany = new int[arrlen];
      int[] arrManySorted = new int[arrlen];
      for (int j = 0; j < arrlen; j++) {
        arrMany[j] = rand.nextInt(200) - 90;
      }
      System.arraycopy(arrMany, 0, arrManySorted, 0, arrlen);
      Arrays.sort(arrManySorted);
      arrMany = Sorting.radixSort(arrMany);
      assertArrayEquals(arrManySorted, arrMany);
    }
  }
Ejemplo n.º 9
0
  @Before
  public void setUp() throws Exception {
    arr = new int[] {11, 14, 18, 13, 21, 4};
    expected = new int[arr.length];

    System.arraycopy(arr, 0, expected, 0, arr.length);

    Arrays.sort(expected);
  }
Ejemplo n.º 10
0
  @Override
  protected void doClientReader() throws Exception {

    // blocks here
    final byte[] arraySent = clientQueue.take();

    final byte[] arrayReceived = new byte[CAPACITY];

    // blocks here
    final int size = client.receive(arrayReceived, POSITION, LIMIT);
    assertEquals(size, SIZE);

    final byte[] dataSent = new byte[SIZE];
    final byte[] dataReceived = new byte[SIZE];

    System.arraycopy( //
        arraySent, POSITION, dataSent, 0, SIZE);
    System.arraycopy( //
        arrayReceived, POSITION, dataReceived, 0, SIZE);

    assertTrue(Arrays.equals(dataSent, dataReceived));
  }
Ejemplo n.º 11
0
  public byte[] decryptCards(final String password, final byte[] ciphertext) throws Exception {
    final MessageDigest shaDigest = MessageDigest.getInstance("SHA-1");
    byte[] pw1 = password.getBytes("UTF-8");

    byte[] keyHash1 = shaDigest.digest(pw1);
    keyHash1 = shaDigest.digest(keyHash1);
    byte[] pw2 = new byte[keyHash1.length + pw1.length];
    System.arraycopy(keyHash1, 0, pw2, 0, keyHash1.length);
    System.arraycopy(pw1, 0, pw2, keyHash1.length, pw1.length);
    byte[] keyHash2 = shaDigest.digest(pw2);
    keyHash2 = shaDigest.digest(keyHash2);

    byte[] key = new byte[16];
    System.arraycopy(keyHash1, 0, key, 0, key.length);
    byte[] iv = new byte[16];
    System.arraycopy(keyHash1, 16, iv, 0, 4);
    System.arraycopy(keyHash2, 0, iv, 4, 12);

    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));

    return cipher.doFinal(ciphertext);
  }
 @Test
 public void test_string_t_length() throws IOException {
   int string_lenght = this.input.length - 1; // ignore the \0 at the end.
   byte[] foo = new byte[string_lenght + 4];
   // Insert the string length at first 4 positions. Is LittleEndian!
   foo[0] = (byte) (string_lenght);
   foo[1] = (byte) (string_lenght >>> 8);
   foo[2] = (byte) (string_lenght >>> 16);
   foo[3] = (byte) (string_lenght >>> 24);
   System.arraycopy(this.input, 0, foo, 4, string_lenght); // ignore the \0 at the end
   this.binFileReader = new BinFileReader(new BufferedInputStream(new ByteArrayInputStream(foo)));
   for (String exp : expected_string) {
     String result = binFileReader.string_t(true);
     assertEquals("string_t(length=true)", exp, result);
     assertEquals("string_t(length=true) toString()", exp.toString(), result.toString());
   }
 }
Ejemplo n.º 13
0
  @Test
  // Test array sort property that array will be the same
  // size after being passed to the sort method
  public void testArraySameSize() {
    int[][] arrs = generateRandomArrays();

    // loop through all arrays
    for (int index = 0; index < arrs.length; index++) {
      int[] testArr = arrs[index];
      int[] preSortedArray = new int[testArr.length];

      // create copy for comparison
      System.arraycopy(testArr, 0, preSortedArray, 0, testArr.length);

      // pass array to Arrays.sort
      Arrays.sort(testArr);

      // check to make sure length has not changed
      assertEquals(testArr.length, preSortedArray.length);
    }
  }
Ejemplo n.º 14
0
  //
  // verify that the data written to the full blocks are sane
  //
  private void checkFile(FileSystem fileSys, Path name, int repl) throws IOException {
    boolean done = false;

    // wait till all full blocks are confirmed by the datanodes.
    while (!done) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {;
      }
      done = true;
      BlockLocation[] locations =
          fileSys.getFileBlockLocations(fileSys.getFileStatus(name), 0, AppendTestUtil.FILE_SIZE);
      if (locations.length < AppendTestUtil.NUM_BLOCKS) {
        System.out.println("Number of blocks found " + locations.length);
        done = false;
        continue;
      }
      for (int idx = 0; idx < AppendTestUtil.NUM_BLOCKS; idx++) {
        if (locations[idx].getHosts().length < repl) {
          System.out.println("Block index " + idx + " not yet replciated.");
          done = false;
          break;
        }
      }
    }
    byte[] expected = new byte[AppendTestUtil.NUM_BLOCKS * AppendTestUtil.BLOCK_SIZE];
    if (simulatedStorage) {
      for (int i = 0; i < expected.length; i++) {
        expected[i] = SimulatedFSDataset.DEFAULT_DATABYTE;
      }
    } else {
      System.arraycopy(fileContents, 0, expected, 0, expected.length);
    }
    // do a sanity check. Read the file
    AppendTestUtil.checkFullFile(
        fileSys, name, AppendTestUtil.NUM_BLOCKS * AppendTestUtil.BLOCK_SIZE, expected, "Read 1");
  }
Ejemplo n.º 15
0
  @Test
  // Test array sort property running it twice on
  // same input array should always result in same output array
  public void testArrayPure() {
    int[][] arrs = generateRandomArrays();

    // loop through all arrays
    for (int index = 0; index < arrs.length; index++) {
      int[] testArr = arrs[index];
      int[] preSortedArray = new int[testArr.length];

      // create copy for comparison
      System.arraycopy(testArr, 0, preSortedArray, 0, testArr.length);

      // pass array to Arrays.sort
      Arrays.sort(testArr);

      // pass copy of array to Arrays.sort
      Arrays.sort(preSortedArray);

      // check that output of both sorted arrays are equal
      assertArrayEquals(preSortedArray, testArr);
    }
  }
Ejemplo n.º 16
0
  @Test
  // Test array sort property that sorted array will not
  // change after being passed to sort method again
  public void testArrayIdempotent() {
    int[][] arrs = generateRandomArrays();

    // loop through all arrays
    for (int index = 0; index < arrs.length; index++) {
      int[] testArr = arrs[index];
      int[] preSortedArray = new int[testArr.length];

      // pass array to Arrays.sort
      Arrays.sort(testArr);

      // create copy for comparison
      System.arraycopy(testArr, 0, preSortedArray, 0, testArr.length);

      // pass sorted array to Arrays.sort again
      Arrays.sort(testArr);

      // check to make sure contents have not changed
      assertArrayEquals(preSortedArray, testArr);
    }
  }
Ejemplo n.º 17
0
 public LogBuffer put(byte[] bytes) throws IOException {
   ensureArrayCapacityPlus(bytes.length);
   System.arraycopy(bytes, 0, this.bytes, writeIndex, bytes.length);
   writeIndex += bytes.length;
   return this;
 }