|
1 |
| -use bson::{from_bson, to_bson, Bson}; |
| 1 | +use bson::{from_bson, to_bson, Bson, EncoderError, EncoderResult}; |
2 | 2 | use bson::oid::ObjectId;
|
3 | 3 | use std::collections::BTreeMap;
|
| 4 | +use std::{u8, u16, u32, u64}; |
4 | 5 |
|
5 | 6 | #[test]
|
6 | 7 | fn floating_point() {
|
@@ -53,11 +54,85 @@ fn int32() {
|
53 | 54 | assert_eq!(deser, obj);
|
54 | 55 | }
|
55 | 56 |
|
| 57 | +#[test] |
| 58 | +#[cfg_attr(feature = "u2i", ignore)] |
| 59 | +fn uint8() { |
| 60 | + let obj_min: EncoderResult<Bson> = to_bson(&u8::MIN); |
| 61 | + assert_matches!(obj_min, Err(EncoderError::UnsupportedUnsignedType)); |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +#[cfg(feature = "u2i")] |
| 66 | +fn uint8_u2i() { |
| 67 | + let obj: Bson = to_bson(&u8::MIN).unwrap(); |
| 68 | + let deser: u8 = from_bson(obj).unwrap(); |
| 69 | + assert_eq!(deser, u8::MIN); |
| 70 | + |
| 71 | + let obj_max: Bson = to_bson(&u8::MAX).unwrap(); |
| 72 | + let deser_max: u8 = from_bson(obj_max).unwrap(); |
| 73 | + assert_eq!(deser_max, u8::MAX); |
| 74 | +} |
| 75 | + |
| 76 | +#[test] |
| 77 | +#[cfg_attr(feature = "u2i", ignore)] |
| 78 | +fn uint16() { |
| 79 | + let obj_min: EncoderResult<Bson> = to_bson(&u16::MIN); |
| 80 | + assert_matches!(obj_min, Err(EncoderError::UnsupportedUnsignedType)); |
| 81 | +} |
| 82 | + |
| 83 | +#[test] |
| 84 | +#[cfg(feature = "u2i")] |
| 85 | +fn uint16_u2i() { |
| 86 | + let obj: Bson = to_bson(&u16::MIN).unwrap(); |
| 87 | + let deser: u16 = from_bson(obj).unwrap(); |
| 88 | + assert_eq!(deser, u16::MIN); |
| 89 | + |
| 90 | + let obj_max: Bson = to_bson(&u16::MAX).unwrap(); |
| 91 | + let deser_max: u16 = from_bson(obj_max).unwrap(); |
| 92 | + assert_eq!(deser_max, u16::MAX); |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +#[cfg_attr(feature = "u2i", ignore)] |
| 97 | +fn uint32() { |
| 98 | + let obj_min: EncoderResult<Bson> = to_bson(&u32::MIN); |
| 99 | + assert_matches!(obj_min, Err(EncoderError::UnsupportedUnsignedType)); |
| 100 | +} |
| 101 | + |
| 102 | +#[test] |
| 103 | +#[cfg(feature = "u2i")] |
| 104 | +fn uint32_u2i() { |
| 105 | + let obj_min: Bson = to_bson(&u32::MIN).unwrap(); |
| 106 | + let deser_min: u32 = from_bson(obj_min).unwrap(); |
| 107 | + assert_eq!(deser_min, u32::MIN); |
| 108 | + |
| 109 | + let obj_max: Bson = to_bson(&u32::MAX).unwrap(); |
| 110 | + let deser_max: u32 = from_bson(obj_max).unwrap(); |
| 111 | + assert_eq!(deser_max, u32::MAX); |
| 112 | +} |
| 113 | + |
| 114 | +#[test] |
| 115 | +#[cfg_attr(feature = "u2i", ignore)] |
| 116 | +fn uint64() { |
| 117 | + let obj_min: EncoderResult<Bson> = to_bson(&u64::MIN); |
| 118 | + assert_matches!(obj_min, Err(EncoderError::UnsupportedUnsignedType)); |
| 119 | +} |
| 120 | + |
| 121 | +#[test] |
| 122 | +#[cfg(feature = "u2i")] |
| 123 | +fn uint64_u2i() { |
| 124 | + let obj_min: Bson = to_bson(&u64::MIN).unwrap(); |
| 125 | + let deser_min: u64 = from_bson(obj_min).unwrap(); |
| 126 | + assert_eq!(deser_min, u64::MIN); |
| 127 | + |
| 128 | + let obj_max: EncoderResult<Bson> = to_bson(&u64::MAX); |
| 129 | + assert_matches!(obj_max, Err(EncoderError::UnsignedTypesValueExceedsRange(u64::MAX))); |
| 130 | +} |
| 131 | + |
56 | 132 | #[test]
|
57 | 133 | fn int64() {
|
58 | 134 | let obj = Bson::I64(101);
|
59 | 135 | let i: i64 = from_bson(obj.clone()).unwrap();
|
60 |
| - |
61 | 136 | assert_eq!(i, 101);
|
62 | 137 |
|
63 | 138 | let deser: Bson = to_bson(&i).unwrap();
|
|
0 commit comments