| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 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 |
|
| 53 |
|
| 54 |
BitWriter bw = new BitWriter(); |
| 55 |
for (boolean b : bits) |
| 56 |
bw.put1(b); |
| 57 |
|
| 58 |
byte[] loopResult = writer.toByteArray(); |
| 59 |
|
| 60 |
|
| 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 |
} |