/** Test that single byte reads have the desired effect on counting */
 @Test
 public void testSingleByteReads() throws Exception {
   ByteArrayInputStream bais = new ByteArrayInputStream(TEST_ARRAY);
   CountingInputStream cis = new CountingInputStream(bais);
   assertEquals((long) 0, cis.getPos());
   for (long i = 0; i < TEST_ARRAY.length; i++) {
     assertEquals(i, cis.getPos());
     int b = cis.read();
     assertEquals((byte) b, TEST_ARRAY[(int) i]);
     assertEquals(i + 1, cis.getPos());
   }
   assertEquals(TEST_ARRAY.length, cis.getPos());
   assertEquals(-1, cis.read());
   assertEquals(TEST_ARRAY.length, cis.getPos());
 }
 /** Test that two byte reads have the desired effect on counting */
 @Test
 public void testTwoByteReads() throws Exception {
   ByteArrayInputStream bais = new ByteArrayInputStream(TEST_ARRAY);
   CountingInputStream cis = new CountingInputStream(bais);
   assertEquals((long) 0, cis.getPos());
   for (long i = 0; i < TEST_ARRAY.length / 2; i++) {
     assertEquals(i * 2, cis.getPos());
     final byte[] read = new byte[2];
     final int rtr = cis.read(read);
     assertEquals(rtr, read.length);
     assertEquals(read[0], TEST_ARRAY[(int) i * 2]);
     assertEquals(read[1], TEST_ARRAY[(int) (i * 2) + 1]);
     assertEquals("offset after read i=" + i + "; ", (i * 2) + read.length, cis.getPos());
   }
   assertEquals(TEST_ARRAY.length, cis.getPos());
   assertEquals(-1, cis.read());
   assertEquals(TEST_ARRAY.length, cis.getPos());
 }
Beispiel #3
0
  public static void compareMapFiles(MapLoader expectedSavegame, MapLoader actualSavegame)
      throws IOException, MapLoadException, ClassNotFoundException {
    System.out.println(
        "Comparing expected '"
            + expectedSavegame
            + "' with actual '"
            + actualSavegame
            + "' (uncompressed!)");

    try (InputStream expectedStream =
            RemakeMapLoader.getMapInputStream(expectedSavegame.getListedMap());
        CountingInputStream actualStream =
            new CountingInputStream(
                RemakeMapLoader.getMapInputStream(actualSavegame.getListedMap()))) {
      MapFileHeader expectedHeader = MapFileHeader.readFromStream(expectedStream);
      MatchConstants.init(new NetworkTimer(true), 0L);
      MatchConstants.deserialize(new ObjectInputStream(expectedStream));
      int expectedTime = MatchConstants.clock().getTime();
      ExtendedRandom expectedRandom = MatchConstants.random();
      MatchConstants.clearState();

      MapFileHeader actualHeader = MapFileHeader.readFromStream(actualStream);
      MatchConstants.init(new NetworkTimer(true), 1L);
      MatchConstants.deserialize(new ObjectInputStream(actualStream));
      int actualTime = MatchConstants.clock().getTime();
      ExtendedRandom actualRandom = MatchConstants.random();
      MatchConstants.clearState();

      assertEquals(expectedHeader.getBaseMapId(), actualHeader.getBaseMapId());
      assertEquals(expectedTime, actualTime);
      // Test the random behavior a bit to have a high probability of equality. An equals method
      // does not exist for Random.
      assertEquals(expectedRandom.nextInt(), actualRandom.nextInt());
      assertEquals(expectedRandom.nextInt(), actualRandom.nextInt());
      assertEquals(expectedRandom.nextInt(), actualRandom.nextInt());

      int e, a;
      while (((e = expectedStream.read()) != -1) & ((a = actualStream.read()) != -1)) {
        assertEquals("difference at (uncompressed) byte " + actualStream.getByteCounter(), e, a);
      }
      assertEquals("files have different lengths (uncompressed)", e, a);
    }
  }