|
| 1 | +require "minitest/autorun" |
| 2 | +require "tempfile" |
| 3 | +require_relative "../lib/bitarray" |
| 4 | + |
| 5 | +class TestBitArrayFile < Minitest::Test |
| 6 | + def setup |
| 7 | + ba = BitArray.new(35) |
| 8 | + [1, 5, 6, 7, 10, 16, 33].each { |i| ba[i] = 1} |
| 9 | + @file = Tempfile.new("bit_array_file.dat") |
| 10 | + ba.dump(@file) |
| 11 | + @file.rewind |
| 12 | + end |
| 13 | + |
| 14 | + def teardown |
| 15 | + @file.close |
| 16 | + @file.unlink |
| 17 | + end |
| 18 | + |
| 19 | + def test_from_filename |
| 20 | + baf = BitArrayFile.new(filename: @file.path) |
| 21 | + for i in 0...35 |
| 22 | + expected = [1, 5, 6, 7, 10, 16, 33].include?(i) ? 1 : 0 |
| 23 | + assert_equal expected, baf[i] |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + def test_from_io |
| 28 | + baf = BitArrayFile.new(io: @file) |
| 29 | + for i in 0...35 |
| 30 | + expected = [1, 5, 6, 7, 10, 16, 33].include?(i) ? 1 : 0 |
| 31 | + assert_equal expected, baf[i] |
| 32 | + end |
| 33 | + end |
| 34 | +end |
| 35 | + |
| 36 | +class TestBitArrayFileWhenNonReversedByte < Minitest::Test |
| 37 | + def setup |
| 38 | + ba = BitArray.new(35, reverse_byte: false) |
| 39 | + [1, 5, 6, 7, 10, 16, 33].each { |i| ba[i] = 1} |
| 40 | + @file = Tempfile.new("bit_array_file.dat") |
| 41 | + ba.dump(@file) |
| 42 | + @file.rewind |
| 43 | + end |
| 44 | + |
| 45 | + def teardown |
| 46 | + @file.close |
| 47 | + @file.unlink |
| 48 | + end |
| 49 | + |
| 50 | + def test_from_filename |
| 51 | + baf = BitArrayFile.new(filename: @file.path) |
| 52 | + for i in 0...35 |
| 53 | + expected = [1, 5, 6, 7, 10, 16, 33].include?(i) ? 1 : 0 |
| 54 | + assert_equal expected, baf[i] |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + def test_from_io |
| 59 | + baf = BitArrayFile.new(io: @file) |
| 60 | + for i in 0...35 |
| 61 | + expected = [1, 5, 6, 7, 10, 16, 33].include?(i) ? 1 : 0 |
| 62 | + assert_equal expected, baf[i] |
| 63 | + end |
| 64 | + end |
| 65 | +end |
0 commit comments