| 1 | use crate::compact::{CompactQR, KEEP_LAST}; |
| 2 | use crate::encode; |
| 3 | use crate::encode::Mode; |
| 4 | use crate::hardcode::cci_bits; |
| 5 | |
| 6 | #[test] |
| 7 | fn best_encoding_numeric_0() { |
| 8 | let res = encode::best_encoding(b"589492"); |
| 9 | assert_eq!(Mode::Numeric, res); |
| 10 | } |
| 11 | |
| 12 | #[test] |
| 13 | fn best_encoding_numeric_1() { |
| 14 | let res = encode::best_encoding(b"95904409521090298052194059450950249521940"); |
| 15 | assert_eq!(Mode::Numeric, res); |
| 16 | } |
| 17 | |
| 18 | #[test] |
| 19 | fn best_encoding_alnum_0() { |
| 20 | let res = encode::best_encoding(b"HELLO WORLD"); |
| 21 | assert_eq!(Mode::Alphanumeric, res); |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn best_encoding_alnum_1() { |
| 26 | let res = encode::best_encoding(b"HELLO WORLD MY NAME IS ERWAN VIVIEN: THIS IS A TEST//////"); |
| 27 | assert_eq!(Mode::Alphanumeric, res); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn best_encoding_byte_0() { |
| 32 | let res = encode::best_encoding(b"589492h"); |
| 33 | assert_eq!(Mode::Byte, res); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn best_encoding_byte_1() { |
| 38 | let res = encode::best_encoding(b"HELLO WORLD!"); |
| 39 | assert_eq!(Mode::Byte, res); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn best_encoding_byte_2() { |
| 44 | let res = encode::best_encoding(b"HELLO WORLD MY NAME, IS ERWAN VIVIEN: THIS IS A TEST//////"); |
| 45 | assert_eq!(Mode::Byte, res); |
| 46 | } |
| 47 | |
| 48 | fn test_encode_header(compact: &CompactQR, input: &[u8], expected_mode: Mode) { |
| 49 | let res = compact.get_data(); |
| 50 | let mode = match res[0] >> 4 { |
| 51 | 0b0001 => Mode::Numeric, |
| 52 | 0b0010 => Mode::Alphanumeric, |
| 53 | 0b0100 => Mode::Byte, |
| 54 | _ => panic!("Invalid encoding mode"), |
| 55 | }; |
| 56 | |
| 57 | assert_eq!( |
| 58 | expected_mode, mode, |
| 59 | "Encoding mode should be {:?}", |
| 60 | expected_mode |
| 61 | ); |
| 62 | |
| 63 | let cci = cci_bits(crate::Version::V01, mode) as u16; |
| 64 | let character_count: u16 = { |
| 65 | let first_nb_bits = 4; |
| 66 | let second_nb_bits = cci - first_nb_bits; |
| 67 | |
| 68 | let first = ((res[0] & 0b0000_1111) as u16) << second_nb_bits; |
| 69 | let second_mask = u8::MAX << (8 - second_nb_bits); |
| 70 | let second = ((res[1] & second_mask) as u16) >> (8 - second_nb_bits); |
| 71 | |
| 72 | first | second |
| 73 | }; |
| 74 | |
| 75 | assert_eq!( |
| 76 | character_count, |
| 77 | input.len() as u16, |
| 78 | "Input length, should be {}", |
| 79 | input.len() |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn encode_byte_1() { |
| 85 | let mut compact = CompactQR::new(); |
| 86 | const INPUT: &[u8] = b"Hello WORLD!"; |
| 87 | encode::encode_byte(&mut compact, INPUT, 8); |
| 88 | |
| 89 | test_encode_header(&compact, INPUT, Mode::Byte); |
| 90 | |
| 91 | let res = compact.get_data(); |
| 92 | for (i, b) in INPUT.iter().enumerate() { |
| 93 | assert_eq!(res[1 + i] & 0b111, *b >> 4, "Left part at index {}", i); |
| 94 | assert_eq!(res[2 + i] >> 4, *b & 0b1111, "Right part at index {}", i); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn encode_alphanumeric_1() { |
| 100 | let mut compact = CompactQR::new(); |
| 101 | const INPUT: &[u8] = b"HELLO WORLD"; |
| 102 | encode::encode_alphanumeric(&mut compact, INPUT, 9); |
| 103 | |
| 104 | test_encode_header(&compact, INPUT, Mode::Alphanumeric); |
| 105 | |
| 106 | let keep_last = KEEP_LAST.map(|x| x as u16); |
| 107 | |
| 108 | let res = compact |
| 109 | .get_data() |
| 110 | .iter() |
| 111 | .map(|&x| u16::from(x)) |
| 112 | .collect::<Vec<_>>(); |
| 113 | |
| 114 | // 13, 'HE' |
| 115 | assert_eq!(res[1] & 0b0000_0111, (17 * 45 + 14) >> 8); |
| 116 | assert_eq!(res[2] & 0b1111_1111, (17 * 45 + 14) & keep_last[8]); |
| 117 | // 24, 'LL' |
| 118 | assert_eq!(res[3] & 0b1111_1111, (21 * 45 + 21) >> 3); |
| 119 | assert_eq!(res[4] & 0b1110_0000, (21 * 45 + 21) << 5 & keep_last[8]); |
| 120 | // 35, 'O ' |
| 121 | assert_eq!(res[4] & 0b0001_1111, (24 * 45 + 36) >> 6); |
| 122 | assert_eq!(res[5] & 0b1111_1100, (24 * 45 + 36) << 2 & keep_last[8]); |
| 123 | // 46, 'WO' |
| 124 | assert_eq!(res[5] & 0b0000_0011, (32 * 45 + 24) >> 9); |
| 125 | assert_eq!(res[6] & 0b1111_1111, (32 * 45 + 24) >> 1 & keep_last[8]); |
| 126 | assert_eq!(res[7] & 0b1000_0000, (32 * 45 + 24) << 8 & keep_last[8]); |
| 127 | // 57, 'RL' |
| 128 | assert_eq!(res[7] & 0b0111_1111, (27 * 45 + 21) >> 4); |
| 129 | assert_eq!(res[8] & 0b1111_0000, (27 * 45 + 21) << 4 & keep_last[8]); |
| 130 | // 68, 'D' |
| 131 | assert_eq!(res[8] & 0b0000_1111, (13) >> 2); |
| 132 | assert_eq!(res[9] & 0b1100_0000, (13) << 6 & keep_last[8]); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn encode_numeric_1() { |
| 137 | let mut compact = CompactQR::new(); |
| 138 | const INPUT: &[u8] = b"5894"; |
| 139 | encode::encode_numeric(&mut compact, INPUT, 10); |
| 140 | |
| 141 | test_encode_header(&compact, INPUT, Mode::Numeric); |
| 142 | |
| 143 | let keep_last = KEEP_LAST.map(|x| x as u16); |
| 144 | |
| 145 | let res = compact |
| 146 | .get_data() |
| 147 | .iter() |
| 148 | .map(|&x| u16::from(x)) |
| 149 | .collect::<Vec<_>>(); |
| 150 | |
| 151 | // 13, '589' |
| 152 | assert_eq!(res[1] & 0b0000_0011, 589 >> 8); |
| 153 | assert_eq!(res[2] & 0b1111_1111, (589 << 0) & keep_last[8]); |
| 154 | // 24, '4' |
| 155 | assert_eq!(res[3] & 0b1111_0000, (4 << 4) & keep_last[8]); |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn encode_numeric_2() { |
| 160 | let mut compact = CompactQR::new(); |
| 161 | const INPUT: &[u8] = b"58949"; |
| 162 | encode::encode_numeric(&mut compact, INPUT, 10); |
| 163 | |
| 164 | test_encode_header(&compact, INPUT, Mode::Numeric); |
| 165 | |
| 166 | let keep_last = KEEP_LAST.map(|x| x as u16); |
| 167 | |
| 168 | let res = compact |
| 169 | .get_data() |
| 170 | .iter() |
| 171 | .map(|&x| u16::from(x)) |
| 172 | .collect::<Vec<_>>(); |
| 173 | |
| 174 | // 13, '589' |
| 175 | assert_eq!(res[1] & 0b0000_0011, 589 >> 8); |
| 176 | assert_eq!(res[2] & 0b1111_1111, (589 << 0) & keep_last[8]); |
| 177 | // 24, '49' |
| 178 | assert_eq!(res[3] & 0b1111_1110, 49 << 1 & keep_last[8]); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn encode_numeric_3() { |
| 183 | let mut compact = CompactQR::new(); |
| 184 | const INPUT: &[u8] = b"589491"; |
| 185 | encode::encode_numeric(&mut compact, INPUT, 10); |
| 186 | |
| 187 | test_encode_header(&compact, INPUT, Mode::Numeric); |
| 188 | |
| 189 | let keep_last = KEEP_LAST.map(|x| x as u16); |
| 190 | |
| 191 | let res = compact |
| 192 | .get_data() |
| 193 | .iter() |
| 194 | .map(|&x| u16::from(x)) |
| 195 | .collect::<Vec<_>>(); |
| 196 | |
| 197 | // 13, '589' |
| 198 | assert_eq!(res[1] & 0b0000_0011, 589 >> 8); |
| 199 | assert_eq!(res[2] & 0b1111_1111, (589 << 0) & keep_last[8]); |
| 200 | // 24, '491' |
| 201 | assert_eq!(res[3] & 0b1111_1111, (491 >> 2) & keep_last[8]); |
| 202 | assert_eq!(res[4] & 0b1100_0000, (491 << 6) & keep_last[8]); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn encode_numeric_4() { |
| 207 | let mut compact = CompactQR::new(); |
| 208 | const INPUT: &[u8] = b"200505150001"; |
| 209 | encode::encode_numeric(&mut compact, INPUT, 10); |
| 210 | |
| 211 | test_encode_header(&compact, INPUT, Mode::Numeric); |
| 212 | |
| 213 | let keep_last = KEEP_LAST.map(|x| x as u16); |
| 214 | |
| 215 | let res = compact |
| 216 | .get_data() |
| 217 | .iter() |
| 218 | .map(|&x| u16::from(x)) |
| 219 | .collect::<Vec<_>>(); |
| 220 | |
| 221 | // 13, '200' |
| 222 | assert_eq!(res[1] & 0b0000_0011, 200 >> 8); |
| 223 | assert_eq!(res[2] & 0b1111_1111, (200 << 0) & keep_last[8]); |
| 224 | // 24, '505' |
| 225 | assert_eq!(res[3] & 0b1111_1111, (505 >> 2) & keep_last[8]); |
| 226 | assert_eq!(res[4] & 0b1100_0000, (505 << 6) & keep_last[8]); |
| 227 | // 35, '150' |
| 228 | assert_eq!(res[4] & 0b0011_1111, (150 >> 4) & keep_last[8]); |
| 229 | assert_eq!(res[5] & 0b1111_0000, (150 << 4) & keep_last[8]); |
| 230 | // 46, '001' |
| 231 | assert_eq!(res[5] & 0b0000_1111, (1) >> 6); |
| 232 | assert_eq!(res[6] & 0b1111_1100, (1) << 2 & keep_last[8]); |
| 233 | } |