Debian Codegraph @

test/uk/me/parabola/imgfmt/app/net/RoadDefTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13 /* Create date: 15-Feb-2009 */
14 package uk.me.parabola.imgfmt.app.net;
15
16 import java.io.ByteArrayOutputStream;
17 import java.util.Random;
18
19 import uk.me.parabola.imgfmt.app.BitWriter;
20
21 import static org.junit.Assert.*;
22 import org.junit.Test;
23
24
25 public class RoadDefTest {
26
27 /**
28 * Compares the result of the loop in writeNod2 with using a BitWriter.
29 *
30 * You have to copy the code to here for this test to mean anything.
31 */
32 @Test
33 public void testBitArray() {
34
35 Random r = new Random();
36
37 final int MAX_BITS = 26;
38 boolean[] bits = new boolean[MAX_BITS];
39 for (int i = 0; i < bits.length; i++)
40 bits[i] = r.nextBoolean();
41
42 ByteArrayOutputStream writer = new ByteArrayOutputStream();
43
44 // This is the loop taken from the code
45 for (int i = 0; i < bits.length; i += 8) {
46 int b = 0;
47 for (int j = 0; j < 8 && j < bits.length - i; j++)
48 if (bits[i+j])
49 b |= 1 << j;
50 writer.write((byte) b);
51 }
52 // End of loop
53
54 BitWriter bw = new BitWriter();
55 for (boolean b : bits)
56 bw.put1(b);
57
58 byte[] loopResult = writer.toByteArray();
59
60 // Get the bit writer result and trim the array to the correct size
61 byte[] bwResult = new byte[bw.getLength()];
62 System.arraycopy(bw.getBytes(), 0, bwResult, 0, bwResult.length);
63
64 assertArrayEquals("Loop against bitwriter", bwResult, loopResult);
65 }
66 }