From f2bab4ab9aa6e70a2847b9349a0f09c76984a8b5 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 25 Dec 2019 15:42:00 +0300 Subject: [PATCH] atoi: add tests --- vlib/strconv/atoi_test.v | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 vlib/strconv/atoi_test.v diff --git a/vlib/strconv/atoi_test.v b/vlib/strconv/atoi_test.v new file mode 100644 index 0000000000..6c34fd6462 --- /dev/null +++ b/vlib/strconv/atoi_test.v @@ -0,0 +1,28 @@ +import strconv + +fn test_atoi() { + assert strconv.atoi('16') == 16 + assert strconv.atoi('+16') == 16 + assert strconv.atoi('-16') == -16 + assert strconv.atoi('str') == 0 + assert strconv.atoi('') == 0 +} + +fn test_parse_int() { + // Different bases + assert strconv.parse_int('16', 16, 0) == 0x16 + assert strconv.parse_int('16', 8, 0) == 016 + assert strconv.parse_int('11', 2, 0) == 3 + // Different bit sizes + assert strconv.parse_int('127', 10, 8) == 127 + assert strconv.parse_int('128', 10, 8) == 127 + assert strconv.parse_int('32767', 10, 16) == 32767 + assert strconv.parse_int('32768', 10, 16) == 32767 + assert strconv.parse_int('2147483647', 10, 32) == 2147483647 + assert strconv.parse_int('2147483648', 10, 32) == 2147483647 + assert strconv.parse_int('9223372036854775807', 10, 64) == 9223372036854775807 + assert strconv.parse_int('9223372036854775808', 10, 64) == 9223372036854775807 + // Invalid bit sizes + assert strconv.parse_int('123', 10, 65) == 0 + assert strconv.parse_int('123', 10, -1) == 0 +}