-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x.json2.decoder2: decode bool and numbers (#22550)
- Loading branch information
Showing
3 changed files
with
217 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
vlib/x/json2/decoder2/tests/decode_number_and_boolean_test.v
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import x.json2.decoder2 as json | ||
|
||
fn test_number() { | ||
// Test u8 | ||
assert json.decode[u8]('0')! == 0 | ||
assert json.decode[u8]('1')! == 1 | ||
assert json.decode[u8]('201')! == 201 | ||
|
||
assert json.decode[u8]('-1')! == u8(-1) | ||
assert json.decode[u8]('-127')! == u8(-127) | ||
|
||
// Test u16 | ||
assert json.decode[u16]('0')! == 0 | ||
assert json.decode[u16]('1')! == 1 | ||
assert json.decode[u16]('201')! == 201 | ||
|
||
assert json.decode[u16]('-1')! == u16(-1) | ||
assert json.decode[u16]('-201')! == u16(-201) | ||
|
||
// Test u32 | ||
assert json.decode[u32]('0')! == 0 | ||
assert json.decode[u32]('1')! == 1 | ||
assert json.decode[u32]('201')! == 201 | ||
|
||
// Test u64 | ||
assert json.decode[u64]('0')! == 0 | ||
assert json.decode[u64]('1')! == 1 | ||
assert json.decode[u64]('201')! == 201 | ||
|
||
// Test i8 | ||
assert json.decode[i8]('0')! == 0 | ||
assert json.decode[i8]('1')! == 1 | ||
assert json.decode[i8]('127')! == 127 | ||
|
||
assert json.decode[i8]('-1')! == -1 | ||
assert json.decode[i8]('-127')! == -127 | ||
|
||
// Test i16 | ||
assert json.decode[i16]('0')! == 0 | ||
assert json.decode[i16]('1')! == 1 | ||
assert json.decode[i16]('201')! == 201 | ||
|
||
assert json.decode[i16]('-1')! == -1 | ||
assert json.decode[i16]('-201')! == -201 | ||
|
||
// Test int | ||
assert json.decode[int]('0')! == 0 | ||
assert json.decode[int]('1')! == 1 | ||
assert json.decode[int]('201')! == 201 | ||
|
||
assert json.decode[int]('-1')! == -1 | ||
assert json.decode[int]('-201')! == -201 | ||
|
||
assert json.decode[int]('1234567890')! == 1234567890 | ||
assert json.decode[int]('-1234567890')! == -1234567890 | ||
|
||
// Test i64 | ||
assert json.decode[i64]('0')! == 0 | ||
assert json.decode[i64]('1')! == 1 | ||
assert json.decode[i64]('201')! == 201 | ||
|
||
assert json.decode[i64]('-1')! == -1 | ||
assert json.decode[i64]('-201')! == -201 | ||
|
||
assert json.decode[i64]('1234567890')! == 1234567890 | ||
assert json.decode[i64]('-1234567890')! == -1234567890 | ||
|
||
// Test f32 | ||
assert json.decode[f32]('0')! == 0.0 | ||
assert json.decode[f32]('1')! == 1.0 | ||
assert json.decode[f32]('201')! == 201.0 | ||
|
||
assert json.decode[f32]('-1')! == -1.0 | ||
assert json.decode[f32]('-201')! == -201.0 | ||
|
||
assert json.decode[f32]('1234567890')! == 1234567890.0 | ||
assert json.decode[f32]('-1234567890')! == -1234567890.0 | ||
|
||
// Test f64 | ||
assert json.decode[f64]('0')! == 0.0 | ||
assert json.decode[f64]('1')! == 1.0 | ||
assert json.decode[f64]('201')! == 201.0 | ||
|
||
assert json.decode[f64]('-1')! == -1.0 | ||
assert json.decode[f64]('-201')! == -201.0 | ||
|
||
assert json.decode[f64]('1234567890')! == 1234567890.0 | ||
assert json.decode[f64]('-1234567890')! == -1234567890.0 | ||
} | ||
|
||
fn test_boolean() { | ||
assert json.decode[bool]('false')! == false | ||
assert json.decode[bool]('true')! == true | ||
} |