|
| 1 | +/// Adopted from pyth::i64, adapted for i16 |
| 2 | + |
| 3 | +module lazer_example::i16; |
| 4 | + |
| 5 | +const MAX_POSITIVE_MAGNITUDE: u64 = (1 << 15) - 1; // 32767 |
| 6 | +const MAX_NEGATIVE_MAGNITUDE: u64 = (1 << 15); // 32768 |
| 7 | + |
| 8 | +/// To consume these values, first call `get_is_negative()` to determine if the I16 |
| 9 | +/// represents a negative or positive value. Then call `get_magnitude_if_positive()` or |
| 10 | +/// `get_magnitude_if_negative()` to get the magnitude of the number in unsigned u64 format. |
| 11 | +/// This API forces consumers to handle positive and negative numbers safely. |
| 12 | +public struct I16 has copy, drop, store { |
| 13 | + negative: bool, |
| 14 | + magnitude: u64, |
| 15 | +} |
| 16 | + |
| 17 | +public fun new(magnitude: u64, mut negative: bool): I16 { |
| 18 | + let mut max_magnitude = MAX_POSITIVE_MAGNITUDE; |
| 19 | + if (negative) { |
| 20 | + max_magnitude = MAX_NEGATIVE_MAGNITUDE; |
| 21 | + }; |
| 22 | + assert!(magnitude <= max_magnitude, 0); //error::magnitude_too_large() |
| 23 | + |
| 24 | + // Ensure we have a single zero representation: (0, false). |
| 25 | + // (0, true) is invalid. |
| 26 | + if (magnitude == 0) { |
| 27 | + negative = false; |
| 28 | + }; |
| 29 | + |
| 30 | + I16 { |
| 31 | + magnitude, |
| 32 | + negative, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +public fun get_is_negative(i: &I16): bool { |
| 37 | + i.negative |
| 38 | +} |
| 39 | + |
| 40 | +public fun get_magnitude_if_positive(in: &I16): u64 { |
| 41 | + assert!(!in.negative, 0); // error::negative_value() |
| 42 | + in.magnitude |
| 43 | +} |
| 44 | + |
| 45 | +public fun get_magnitude_if_negative(in: &I16): u64 { |
| 46 | + assert!(in.negative, 0); //error::positive_value() |
| 47 | + in.magnitude |
| 48 | +} |
| 49 | + |
| 50 | +public fun from_u16(from: u16): I16 { |
| 51 | + // Use the MSB to determine whether the number is negative or not. |
| 52 | + let from_u64 = (from as u64); |
| 53 | + let negative = (from_u64 >> 15) == 1; |
| 54 | + let magnitude = parse_magnitude(from_u64, negative); |
| 55 | + |
| 56 | + new(magnitude, negative) |
| 57 | +} |
| 58 | + |
| 59 | +fun parse_magnitude(from: u64, negative: bool): u64 { |
| 60 | + // If positive, then return the input verbatim |
| 61 | + if (!negative) { |
| 62 | + return from |
| 63 | + }; |
| 64 | + |
| 65 | + // Otherwise convert from two's complement by inverting and adding 1 |
| 66 | + // For 16-bit numbers, we only invert the lower 16 bits |
| 67 | + let inverted = from ^ 0xFFFF; |
| 68 | + inverted + 1 |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +fun test_max_positive_magnitude() { |
| 73 | + new(0x7FFF, false); // 32767 |
| 74 | + assert!(&new((1<<15) - 1, false) == &from_u16(((1<<15) - 1) as u16), 1); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +#[expected_failure] |
| 79 | +fun test_magnitude_too_large_positive() { |
| 80 | + new(0x8000, false); // 32768 |
| 81 | +} |
| 82 | + |
| 83 | +#[test] |
| 84 | +fun test_max_negative_magnitude() { |
| 85 | + new(0x8000, true); // 32768 |
| 86 | + assert!(&new(1<<15, true) == &from_u16((1<<15) as u16), 1); |
| 87 | +} |
| 88 | + |
| 89 | +#[test] |
| 90 | +#[expected_failure] |
| 91 | +fun test_magnitude_too_large_negative() { |
| 92 | + new(0x8001, true); // 32769 |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fun test_from_u16_positive() { |
| 97 | + assert!(from_u16(0x1234) == new(0x1234, false), 1); |
| 98 | +} |
| 99 | + |
| 100 | +#[test] |
| 101 | +fun test_from_u16_negative() { |
| 102 | + assert!(from_u16(0xEDCC) == new(0x1234, true), 1); |
| 103 | +} |
| 104 | + |
| 105 | +#[test] |
| 106 | +fun test_get_is_negative() { |
| 107 | + assert!(get_is_negative(&new(234, true)) == true, 1); |
| 108 | + assert!(get_is_negative(&new(767, false)) == false, 1); |
| 109 | +} |
| 110 | + |
| 111 | +#[test] |
| 112 | +fun test_get_magnitude_if_positive_positive() { |
| 113 | + assert!(get_magnitude_if_positive(&new(7686, false)) == 7686, 1); |
| 114 | +} |
| 115 | + |
| 116 | +#[test] |
| 117 | +#[expected_failure] |
| 118 | +fun test_get_magnitude_if_positive_negative() { |
| 119 | + assert!(get_magnitude_if_positive(&new(7686, true)) == 7686, 1); |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fun test_get_magnitude_if_negative_negative() { |
| 124 | + assert!(get_magnitude_if_negative(&new(7686, true)) == 7686, 1); |
| 125 | +} |
| 126 | + |
| 127 | +#[test] |
| 128 | +#[expected_failure] |
| 129 | +fun test_get_magnitude_if_negative_positive() { |
| 130 | + assert!(get_magnitude_if_negative(&new(7686, false)) == 7686, 1); |
| 131 | +} |
| 132 | + |
| 133 | +#[test] |
| 134 | +fun test_single_zero_representation() { |
| 135 | + assert!(&new(0, true) == &new(0, false), 1); |
| 136 | + assert!(&new(0, true) == &from_u16(0), 1); |
| 137 | + assert!(&new(0, false) == &from_u16(0), 1); |
| 138 | +} |
| 139 | + |
| 140 | +#[test] |
| 141 | +fun test_boundary_values() { |
| 142 | + // Test positive boundary |
| 143 | + assert!(from_u16(0x7FFF) == new(32767, false), 1); |
| 144 | + |
| 145 | + // Test negative boundary |
| 146 | + assert!(from_u16(0x8000) == new(32768, true), 1); |
| 147 | + |
| 148 | + // Test -1 |
| 149 | + assert!(from_u16(0xFFFF) == new(1, true), 1); |
| 150 | +} |
0 commit comments