|
| 1 | +// these tests check the asserts that are only present in debug configurations |
| 2 | +// so they won't pass in release mode |
| 3 | +#![cfg(debug_assertions)] |
| 4 | + |
| 5 | +use std::{convert::Infallible, io::Cursor, ops::Range}; |
| 6 | + |
| 7 | +use arithmetic_coding::{decoder, encoder, Decoder, Encoder}; |
| 8 | +use arithmetic_coding_core::one_shot; |
| 9 | +use bitstream_io::{BigEndian, BitReader, BitWriter}; |
| 10 | + |
| 11 | +#[derive(Copy, Clone)] |
| 12 | +struct SmallModel; |
| 13 | +impl one_shot::Model for SmallModel { |
| 14 | + type B = u64; |
| 15 | + type Symbol = u64; |
| 16 | + type ValueError = Infallible; |
| 17 | + |
| 18 | + fn probability(&self, &value: &Self::Symbol) -> Result<Range<Self::B>, Self::ValueError> { |
| 19 | + #[allow(clippy::range_plus_one)] |
| 20 | + Ok(value..value + 1) |
| 21 | + } |
| 22 | + |
| 23 | + fn max_denominator(&self) -> Self::B { |
| 24 | + 2 |
| 25 | + } |
| 26 | + |
| 27 | + fn symbol(&self, value: Self::B) -> Self::Symbol { |
| 28 | + value |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Copy, Clone)] |
| 33 | +struct BigModel; |
| 34 | +impl one_shot::Model for BigModel { |
| 35 | + type B = u64; |
| 36 | + type Symbol = u64; |
| 37 | + type ValueError = Infallible; |
| 38 | + |
| 39 | + fn probability(&self, &value: &Self::Symbol) -> Result<Range<Self::B>, Self::ValueError> { |
| 40 | + #[allow(clippy::range_plus_one)] |
| 41 | + Ok(value..value + 1) |
| 42 | + } |
| 43 | + |
| 44 | + fn max_denominator(&self) -> Self::B { |
| 45 | + u64::from(u32::MAX) / 2 |
| 46 | + } |
| 47 | + |
| 48 | + fn symbol(&self, value: Self::B) -> Self::Symbol { |
| 49 | + value |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// this is one bit short of what it must be |
| 54 | +const PRECISION: u32 = 32; |
| 55 | + |
| 56 | +// Encoder::new should select the correct precision automagically, so we don't |
| 57 | +// expect it to panic |
| 58 | +#[test] |
| 59 | +fn encoder_new_doesnt_panic() { |
| 60 | + Encoder::new( |
| 61 | + one_shot::Wrapper::new(BigModel), |
| 62 | + &mut BitWriter::endian(Vec::new(), BigEndian), |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +#[should_panic(expected = "not enough bits of precision to prevent overflow/underflow")] |
| 68 | +fn encoder_with_precision_panics() { |
| 69 | + Encoder::with_precision( |
| 70 | + one_shot::Wrapper::new(BigModel), |
| 71 | + &mut BitWriter::endian(Vec::new(), BigEndian), |
| 72 | + PRECISION, |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +#[should_panic(expected = "not enough bits of precision to prevent overflow/underflow")] |
| 78 | +fn encoder_with_state_panics() { |
| 79 | + Encoder::with_state( |
| 80 | + encoder::State::new(PRECISION, &mut BitWriter::endian(Vec::new(), BigEndian)), |
| 81 | + one_shot::Wrapper::new(BigModel), |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +#[should_panic(expected = "not enough bits of precision to prevent overflow/underflow")] |
| 87 | +fn encoder_chain_panics() { |
| 88 | + let mut writer = BitWriter::endian(Vec::new(), BigEndian); |
| 89 | + let encoder = |
| 90 | + Encoder::with_precision(one_shot::Wrapper::new(SmallModel), &mut writer, PRECISION); |
| 91 | + |
| 92 | + encoder.chain(one_shot::Wrapper::new(BigModel)); |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fn decoder_new_doesnt_panic() { |
| 97 | + Decoder::new( |
| 98 | + one_shot::Wrapper::new(BigModel), |
| 99 | + BitReader::endian(Cursor::new(&[]), BigEndian), |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +#[should_panic(expected = "not enough bits of precision to prevent overflow/underflow")] |
| 105 | +fn decoder_with_precision_panics() { |
| 106 | + Decoder::with_precision( |
| 107 | + one_shot::Wrapper::new(BigModel), |
| 108 | + BitReader::endian(Cursor::new(&[]), BigEndian), |
| 109 | + PRECISION, |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +#[test] |
| 114 | +#[should_panic(expected = "not enough bits of precision to prevent overflow/underflow")] |
| 115 | +fn decoder_with_state_panics() { |
| 116 | + Decoder::with_state( |
| 117 | + decoder::State::new(PRECISION, BitReader::endian(Cursor::new(&[]), BigEndian)), |
| 118 | + one_shot::Wrapper::new(BigModel), |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +#[should_panic(expected = "not enough bits of precision to prevent overflow/underflow")] |
| 124 | +fn decoder_chain_panics() { |
| 125 | + let decoder = Decoder::with_precision( |
| 126 | + one_shot::Wrapper::new(SmallModel), |
| 127 | + BitReader::endian(Cursor::new(&[]), BigEndian), |
| 128 | + PRECISION, |
| 129 | + ); |
| 130 | + |
| 131 | + decoder.chain(one_shot::Wrapper::new(BigModel)); |
| 132 | +} |
0 commit comments