Tests operations against the two provided [`i256`]
(il: i256, ir: i256)
| 1184 | |
| 1185 | /// Tests operations against the two provided [`i256`] |
| 1186 | fn test_ops(il: i256, ir: i256) { |
| 1187 | let bl = BigInt::from_signed_bytes_le(&il.to_le_bytes()); |
| 1188 | let br = BigInt::from_signed_bytes_le(&ir.to_le_bytes()); |
| 1189 | |
| 1190 | // Comparison |
| 1191 | assert_eq!(il.cmp(&ir), bl.cmp(&br), "{bl} cmp {br}"); |
| 1192 | |
| 1193 | // Conversions |
| 1194 | assert_eq!(i256::from_le_bytes(il.to_le_bytes()), il); |
| 1195 | assert_eq!(i256::from_be_bytes(il.to_be_bytes()), il); |
| 1196 | assert_eq!(i256::from_le_bytes(ir.to_le_bytes()), ir); |
| 1197 | assert_eq!(i256::from_be_bytes(ir.to_be_bytes()), ir); |
| 1198 | |
| 1199 | // To i128 |
| 1200 | assert_eq!(il.to_i128(), bl.to_i128(), "{bl}"); |
| 1201 | assert_eq!(ir.to_i128(), br.to_i128(), "{br}"); |
| 1202 | |
| 1203 | // Absolute value |
| 1204 | let (abs, overflow) = i256::from_bigint_with_overflow(bl.abs()); |
| 1205 | assert_eq!(il.wrapping_abs(), abs); |
| 1206 | assert_eq!(il.checked_abs().is_none(), overflow); |
| 1207 | |
| 1208 | let (abs, overflow) = i256::from_bigint_with_overflow(br.abs()); |
| 1209 | assert_eq!(ir.wrapping_abs(), abs); |
| 1210 | assert_eq!(ir.checked_abs().is_none(), overflow); |
| 1211 | |
| 1212 | // Negation |
| 1213 | let (neg, overflow) = i256::from_bigint_with_overflow(bl.clone().neg()); |
| 1214 | assert_eq!(il.wrapping_neg(), neg); |
| 1215 | assert_eq!(il.checked_neg().is_none(), overflow); |
| 1216 | |
| 1217 | // Negation |
| 1218 | let (neg, overflow) = i256::from_bigint_with_overflow(br.clone().neg()); |
| 1219 | assert_eq!(ir.wrapping_neg(), neg); |
| 1220 | assert_eq!(ir.checked_neg().is_none(), overflow); |
| 1221 | |
| 1222 | // Addition |
| 1223 | let actual = il.wrapping_add(ir); |
| 1224 | let (expected, overflow) = i256::from_bigint_with_overflow(bl.clone() + br.clone()); |
| 1225 | assert_eq!(actual, expected); |
| 1226 | |
| 1227 | let checked = il.checked_add(ir); |
| 1228 | match overflow { |
| 1229 | true => assert!(checked.is_none()), |
| 1230 | false => assert_eq!(checked, Some(actual)), |
| 1231 | } |
| 1232 | |
| 1233 | // Subtraction |
| 1234 | let actual = il.wrapping_sub(ir); |
| 1235 | let (expected, overflow) = i256::from_bigint_with_overflow(bl.clone() - br.clone()); |
| 1236 | assert_eq!(actual.to_string(), expected.to_string()); |
| 1237 | |
| 1238 | let checked = il.checked_sub(ir); |
| 1239 | match overflow { |
| 1240 | true => assert!(checked.is_none()), |
| 1241 | false => assert_eq!(checked, Some(actual), "{bl} - {br} = {expected}"), |
| 1242 | } |
| 1243 |
no test coverage detected