| 1 | import x.json2 as json |
| 2 | |
| 3 | fn test_number() { |
| 4 | // Test u8 |
| 5 | assert json.decode[u8]('0')! == 0 |
| 6 | assert json.decode[u8]('1')! == 1 |
| 7 | assert json.decode[u8]('201')! == 201 |
| 8 | |
| 9 | // Test u16 |
| 10 | assert json.decode[u16]('0')! == 0 |
| 11 | assert json.decode[u16]('1')! == 1 |
| 12 | assert json.decode[u16]('201')! == 201 |
| 13 | |
| 14 | // Test u32 |
| 15 | assert json.decode[u32]('0')! == 0 |
| 16 | assert json.decode[u32]('1')! == 1 |
| 17 | assert json.decode[u32]('201')! == 201 |
| 18 | |
| 19 | // Test u64 |
| 20 | assert json.decode[u64]('0')! == 0 |
| 21 | assert json.decode[u64]('1')! == 1 |
| 22 | assert json.decode[u64]('201')! == 201 |
| 23 | |
| 24 | // Test i8 |
| 25 | assert json.decode[i8]('0')! == 0 |
| 26 | assert json.decode[i8]('1')! == 1 |
| 27 | assert json.decode[i8]('127')! == 127 |
| 28 | |
| 29 | assert json.decode[i8]('-1')! == -1 |
| 30 | assert json.decode[i8]('-127')! == -127 |
| 31 | |
| 32 | // Test i16 |
| 33 | assert json.decode[i16]('0')! == 0 |
| 34 | assert json.decode[i16]('1')! == 1 |
| 35 | assert json.decode[i16]('201')! == 201 |
| 36 | |
| 37 | assert json.decode[i16]('-1')! == -1 |
| 38 | assert json.decode[i16]('-201')! == -201 |
| 39 | |
| 40 | // Test int |
| 41 | assert json.decode[int]('0')! == 0 |
| 42 | assert json.decode[int]('1')! == 1 |
| 43 | assert json.decode[int]('201')! == 201 |
| 44 | |
| 45 | assert json.decode[int]('-1')! == -1 |
| 46 | assert json.decode[int]('-201')! == -201 |
| 47 | |
| 48 | assert json.decode[int]('1234567890')! == 1234567890 |
| 49 | assert json.decode[int]('-1234567890')! == -1234567890 |
| 50 | |
| 51 | // Test i64 |
| 52 | assert json.decode[i64]('0')! == 0 |
| 53 | assert json.decode[i64]('1')! == 1 |
| 54 | assert json.decode[i64]('201')! == 201 |
| 55 | |
| 56 | assert json.decode[i64]('-1')! == -1 |
| 57 | assert json.decode[i64]('-201')! == -201 |
| 58 | |
| 59 | assert json.decode[i64]('1234567890')! == 1234567890 |
| 60 | assert json.decode[i64]('-1234567890')! == -1234567890 |
| 61 | |
| 62 | // Test f32 |
| 63 | assert json.decode[f32]('0')! == 0.0 |
| 64 | assert json.decode[f32]('1')! == 1.0 |
| 65 | assert json.decode[f32]('1.2')! == 1.2 |
| 66 | assert json.decode[f32]('-1.2')! == -1.2 |
| 67 | assert json.decode[f32]('201')! == 201.0 |
| 68 | |
| 69 | assert json.decode[f32]('-1')! == -1.0 |
| 70 | assert json.decode[f32]('-201')! == -201.0 |
| 71 | |
| 72 | assert json.decode[f32]('1234567890')! == 1234567890.0 |
| 73 | assert json.decode[f32]('-1234567890')! == -1234567890.0 |
| 74 | |
| 75 | // Test f64 |
| 76 | assert json.decode[f64]('0')! == 0.0 |
| 77 | assert json.decode[f64]('1')! == 1.0 |
| 78 | assert json.decode[f64]('1.2')! == 1.2 |
| 79 | assert json.decode[f64]('201')! == 201.0 |
| 80 | |
| 81 | assert json.decode[f64]('-1')! == -1.0 |
| 82 | assert json.decode[f64]('-1.2')! == -1.2 |
| 83 | assert json.decode[f64]('-201')! == -201.0 |
| 84 | |
| 85 | assert json.decode[f64]('1234567890')! == 1234567890.0 |
| 86 | assert json.decode[f64]('-1234567890')! == -1234567890.0 |
| 87 | |
| 88 | assert json.decode[f64]('1e10')! == 10000000000 |
| 89 | assert json.decode[f64]('1E10')! == 10000000000 |
| 90 | assert json.decode[f64]('1e+10')! == 10000000000 |
| 91 | assert json.decode[f64]('1e-10')! == 0.0000000001 |
| 92 | assert json.decode[f64]('-1e10')! == -10000000000 |
| 93 | assert json.decode[f64]('-1E-10')! == -0.0000000001 |
| 94 | assert json.decode[f64]('0.123e3')! - 123 < 0.0000001 |
| 95 | assert json.decode[f64]('10.5E+3')! == 10500 |
| 96 | |
| 97 | // Test Over/Underflow |
| 98 | assert json.decode[i8]('127')! == 127 |
| 99 | assert json.decode[i8]('-128')! == -128 |
| 100 | |
| 101 | if x := json.decode[i8]('128') { |
| 102 | assert false |
| 103 | } |
| 104 | if x := json.decode[i8]('130') { |
| 105 | assert false |
| 106 | } |
| 107 | if x := json.decode[i8]('1000') { |
| 108 | assert false |
| 109 | } |
| 110 | if x := json.decode[i8]('-129') { |
| 111 | assert false |
| 112 | } |
| 113 | if x := json.decode[i8]('-130') { |
| 114 | assert false |
| 115 | } |
| 116 | if x := json.decode[i8]('-1000') { |
| 117 | assert false |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fn test_boolean() { |
| 122 | assert json.decode[bool]('false')! == false |
| 123 | assert json.decode[bool]('true')! == true |
| 124 | } |
| 125 | |