| 1 |
use rmp::encode::*; |
| 2 |
use rmp::Marker; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn pass_pack_pfix() { |
| 6 |
let mut buf = [0x00]; |
| 7 |
|
| 8 |
write_pfix(&mut &mut buf[..], 127).ok().unwrap(); |
| 9 |
|
| 10 |
assert_eq!([0x7f], buf); |
| 11 |
} |
| 12 |
|
| 13 |
#[test] |
| 14 |
fn fail_pack_pfix_too_small_buffer() { |
| 15 |
let mut buf = []; |
| 16 |
|
| 17 |
write_pfix(&mut &mut buf[..], 127).err().unwrap(); |
| 18 |
} |
| 19 |
|
| 20 |
#[test] |
| 21 |
#[should_panic(expected = "assertion failed")] |
| 22 |
fn fail_pack_pfix_too_large() { |
| 23 |
let mut buf = [0x00]; |
| 24 |
|
| 25 |
write_pfix(&mut &mut buf[..], 128).ok().unwrap(); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn pass_pack_u8() { |
| 30 |
let mut buf = [0x00, 0x00]; |
| 31 |
|
| 32 |
write_u8(&mut &mut buf[..], 255).ok().unwrap(); |
| 33 |
|
| 34 |
assert_eq!([0xcc, 0xff], buf); |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn pass_pack_u16() { |
| 39 |
let mut buf = [0x00, 0x00, 0x00]; |
| 40 |
|
| 41 |
write_u16(&mut &mut buf[..], 65535).ok().unwrap(); |
| 42 |
|
| 43 |
assert_eq!([0xcd, 0xff, 0xff], buf); |
| 44 |
} |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn pass_pack_u32() { |
| 48 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00]; |
| 49 |
|
| 50 |
write_u32(&mut &mut buf[..], 4294967295).ok().unwrap(); |
| 51 |
|
| 52 |
assert_eq!([0xce, 0xff, 0xff, 0xff, 0xff], buf); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn pass_pack_u64() { |
| 57 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 58 |
|
| 59 |
write_u64(&mut &mut buf[..], 18446744073709551615).ok().unwrap(); |
| 60 |
|
| 61 |
assert_eq!([0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], buf); |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn pass_pack_nfix() { |
| 66 |
let mut buf = [0x00]; |
| 67 |
|
| 68 |
write_nfix(&mut &mut buf[..], -32).ok().unwrap(); |
| 69 |
|
| 70 |
assert_eq!([0xe0], buf); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
#[should_panic(expected = "assertion failed")] |
| 75 |
fn fail_pack_nfix_too_large() { |
| 76 |
let mut buf = [0x00]; |
| 77 |
|
| 78 |
write_nfix(&mut &mut buf[..], 0).ok().unwrap(); |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
#[should_panic(expected = "assertion failed")] |
| 83 |
fn fail_pack_nfix_too_small() { |
| 84 |
let mut buf = [0x00]; |
| 85 |
|
| 86 |
write_nfix(&mut &mut buf[..], -33).ok().unwrap(); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn pass_pack_i8() { |
| 91 |
let mut buf = [0x00, 0x00]; |
| 92 |
|
| 93 |
write_i8(&mut &mut buf[..], -128).ok().unwrap(); |
| 94 |
|
| 95 |
assert_eq!([0xd0, 0x80], buf); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn pass_pack_i16() { |
| 100 |
let mut buf = [0x00, 0x00, 0x00]; |
| 101 |
|
| 102 |
write_i16(&mut &mut buf[..], -32768).ok().unwrap(); |
| 103 |
|
| 104 |
assert_eq!([0xd1, 0x80, 0x00], buf); |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn pass_pack_i32() { |
| 109 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00]; |
| 110 |
|
| 111 |
write_i32(&mut &mut buf[..], -2147483648).ok().unwrap(); |
| 112 |
|
| 113 |
assert_eq!([0xd2, 0x80, 0x00, 0x00, 0x00], buf); |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn pass_pack_i64() { |
| 118 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 119 |
|
| 120 |
write_i64(&mut &mut buf[..], -9223372036854775808).ok().unwrap(); |
| 121 |
|
| 122 |
assert_eq!([0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn pass_pack_uint_fix() { |
| 127 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 128 |
|
| 129 |
assert_eq!(Marker::FixPos(127), write_uint(&mut &mut buf[..], 127).ok().unwrap()); |
| 130 |
|
| 131 |
assert_eq!([0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn pass_pack_uint_u8() { |
| 136 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 137 |
|
| 138 |
assert_eq!(Marker::U8, write_uint(&mut &mut buf[..], 255).ok().unwrap()); |
| 139 |
|
| 140 |
assert_eq!([0xcc, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn pass_pack_uint_u16() { |
| 145 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 146 |
|
| 147 |
assert_eq!(Marker::U16, write_uint(&mut &mut buf[..], 65535).ok().unwrap()); |
| 148 |
|
| 149 |
assert_eq!([0xcd, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn pass_pack_uint_u32() { |
| 154 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 155 |
|
| 156 |
assert_eq!(Marker::U32, write_uint(&mut &mut buf[..], 4294967295).ok().unwrap()); |
| 157 |
|
| 158 |
assert_eq!([0xce, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00], buf); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn pass_pack_uint_u64() { |
| 163 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 164 |
|
| 165 |
assert_eq!(Marker::U64, write_uint(&mut &mut buf[..], 18446744073709551615).ok().unwrap()); |
| 166 |
|
| 167 |
assert_eq!([0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], buf); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn pass_pack_sint_fix() { |
| 172 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 173 |
|
| 174 |
assert_eq!(Marker::FixNeg(-32), write_sint(&mut &mut buf[..], -32).ok().unwrap()); |
| 175 |
|
| 176 |
assert_eq!([0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn pass_pack_sint_i8_min() { |
| 181 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 182 |
|
| 183 |
assert_eq!(Marker::I8, write_sint(&mut &mut buf[..], -128).ok().unwrap()); |
| 184 |
|
| 185 |
assert_eq!([0xd0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 186 |
} |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn pass_pack_sint_i16_min() { |
| 190 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 191 |
|
| 192 |
assert_eq!(Marker::I16, write_sint(&mut &mut buf[..], -32768).ok().unwrap()); |
| 193 |
|
| 194 |
assert_eq!([0xd1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 195 |
} |
| 196 |
|
| 197 |
#[test] |
| 198 |
fn pass_pack_sint_i16_max() { |
| 199 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 200 |
|
| 201 |
assert_eq!(Marker::U16, write_sint(&mut &mut buf[..], 32767).ok().unwrap()); |
| 202 |
|
| 203 |
assert_eq!([0xcd, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn pass_pack_sint_i32_min() { |
| 208 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 209 |
|
| 210 |
assert_eq!(Marker::I32, write_sint(&mut &mut buf[..], -2147483648).ok().unwrap()); |
| 211 |
|
| 212 |
assert_eq!([0xd2, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn pass_pack_sint_i32_max() { |
| 217 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 218 |
|
| 219 |
assert_eq!(Marker::U32, write_sint(&mut &mut buf[..], 2147483647).ok().unwrap()); |
| 220 |
|
| 221 |
assert_eq!([0xce, 0x7f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00], buf); |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn pass_pack_sint_i64_min() { |
| 226 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 227 |
|
| 228 |
assert_eq!(Marker::I64, write_sint(&mut &mut buf[..], -9223372036854775808).ok().unwrap()); |
| 229 |
|
| 230 |
assert_eq!([0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], buf); |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn pass_pack_sint_i64_max() { |
| 235 |
let mut buf = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]; |
| 236 |
|
| 237 |
assert_eq!(Marker::U64, write_sint(&mut &mut buf[..], 9223372036854775807).ok().unwrap()); |
| 238 |
|
| 239 |
assert_eq!([0xcf, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], buf); |
| 240 |
} |