From fb0735eebe97a79c3db5bcbfeb271544d421ae3f Mon Sep 17 00:00:00 2001 From: mohanson Date: Mon, 16 Oct 2023 15:59:47 +0800 Subject: [PATCH 1/2] Fix atoi --- include/c-stdlib/src/printf_impl.c | 18 +++++++++++++++--- tests/basic/Makefile | 1 + tests/basic/test_float.js | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 tests/basic/test_float.js diff --git a/include/c-stdlib/src/printf_impl.c b/include/c-stdlib/src/printf_impl.c index 13b1fa3..727803e 100644 --- a/include/c-stdlib/src/printf_impl.c +++ b/include/c-stdlib/src/printf_impl.c @@ -246,7 +246,7 @@ static inline unsigned int _strnlen_s(const char *str, size_t maxsize) { static inline bool _is_digit(char ch) { return (ch >= '0') && (ch <= '9'); } // internal ASCII string to unsigned int conversion -unsigned int atoi(const char **str) { +unsigned int _atoi(const char **str) { unsigned int i = 0U; while (_is_digit(**str)) { i = i * 10U + (unsigned int)(*((*str)++) - '0'); @@ -254,6 +254,18 @@ unsigned int atoi(const char **str) { return i; } +int atoi(const char *str) { + if (str[0] == '+') { + const char *sub = str + 1; + return +_atoi(&sub); + } + if (str[0] == '-') { + const char *sub = str + 1; + return -_atoi(&sub); + } + return _atoi(&str); +} + // output the specified string in reverse, taking care of any zero-padding static size_t _out_rev(out_fct_type out, char *buffer, size_t idx, size_t maxlen, const char *buf, size_t len, unsigned int width, unsigned int flags) { @@ -685,7 +697,7 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const // evaluate width field width = 0U; if (_is_digit(*format)) { - width = atoi(&format); + width = _atoi(&format); } else if (*format == '*') { const int w = va_arg(va, int); if (w < 0) { @@ -703,7 +715,7 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const flags |= FLAGS_PRECISION; format++; if (_is_digit(*format)) { - precision = atoi(&format); + precision = _atoi(&format); } else if (*format == '*') { const int prec = (int)va_arg(va, int); precision = prec > 0 ? (unsigned int)prec : 0U; diff --git a/tests/basic/Makefile b/tests/basic/Makefile index 9386d0f..9492710 100644 --- a/tests/basic/Makefile +++ b/tests/basic/Makefile @@ -21,6 +21,7 @@ qjs-tests: $(call run,test_closure.js) $(call run,test_builtin.js) $(call run,test_bignum.js) + $(call run,test_float.js) log: $(CKB-DEBUGGER) --bin $(BIN_PATH) -- -e "console.log(scriptArgs[0], scriptArgs[1]);" hello world diff --git a/tests/basic/test_float.js b/tests/basic/test_float.js new file mode 100644 index 0000000..fe06042 --- /dev/null +++ b/tests/basic/test_float.js @@ -0,0 +1,21 @@ +"use strict"; + +function assert(actual, expected, message) { + if (arguments.length == 1) + expected = true; + + if (actual == expected) + return; + + throw Error("assertion failed: got |" + actual + "|" + + ", expected |" + expected + "|" + + (message ? " (" + message + ")" : "")); +} + +function test_to_string() { + assert(1.1.toString(), "1.1") + assert(-1.1.toString(), "-1.100000") + assert(3.14159265354.toString(), "3.1415926540000000") +} + +test_to_string() From c13dc13ac741c6dae2f7e9f029560aba8f6b6c9c Mon Sep 17 00:00:00 2001 From: mohanson Date: Mon, 16 Oct 2023 17:55:03 +0800 Subject: [PATCH 2/2] Fix skipped tests in test_bignum.js --- tests/basic/test_bignum.js | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/tests/basic/test_bignum.js b/tests/basic/test_bignum.js index ef2d6c0..f59a823 100644 --- a/tests/basic/test_bignum.js +++ b/tests/basic/test_bignum.js @@ -94,7 +94,7 @@ function test_bigint1() r = 1n << 31n; assert(r, 2147483648n, "1 << 31n === 2147483648n"); - + r = 1n << 32n; assert(r, 4294967296n, "1 << 32n === 4294967296n"); } @@ -150,7 +150,7 @@ function test_bigint_ext() function test_bigfloat() { var e, a, b, sqrt2; - + assert(typeof 1n === "bigint"); assert(typeof 1l === "bigfloat"); assert(1 == 1.0l); @@ -164,7 +164,7 @@ function test_bigfloat() test_less(2.1, 3l); // test_eq(Math.sqrt(9), 3l); - + test_less(2n, 3l); test_eq(3n, 3l); @@ -174,7 +174,7 @@ function test_bigfloat() // assert(a === BigFloat.parseFloat("0x1.6a09e667f3bcc908b2fb1366ea957d3e", 0, e)); // assert(e.inexact === true); // assert(BigFloat.fpRound(a) == 0x1.6a09e667f3bcc908b2fb1366ea95l); - + // b = BigFloatEnv.setPrec(BigFloat.sqrt.bind(null, 2), 128); // assert(a === b); @@ -188,7 +188,7 @@ function test_bigfloat() assert(BigFloat.exp(0.2l) === 1.2214027581601698339210719946396742l); assert(BigFloat.log(3l) === 1.0986122886681096913952452369225256l); assert(BigFloat.pow(2.1l, 1.6l) === 3.277561666451861947162828744873745l); - + assert(BigFloat.sin(-1l) === -0.841470984807896506652502321630299l); assert(BigFloat.cos(1l) === 0.5403023058681397174009366074429766l); assert(BigFloat.tan(0.1l) === 0.10033467208545054505808004578111154l); @@ -241,15 +241,15 @@ function test_bigdecimal() assert(1m !== 2m); test_less(1m, 2m); test_eq(2m, 2m); - + test_less(1, 2m); // test_less(1.1, 2m); // test_eq(Math.sqrt(4), 2m); - + test_less(2n, 3m); test_eq(3n, 3m); - + assert(BigDecimal("1234.1") === 1234.1m); assert(BigDecimal(" 1234.1") === 1234.1m); assert(BigDecimal(" 1234.1 ") === 1234.1m); @@ -271,7 +271,7 @@ function test_bigdecimal() assert(1234.5m ** 3m === 1881365963.625m); assertThrows(RangeError, () => { 2m ** 3.1m } ); assertThrows(RangeError, () => { 2m ** -3m } ); - + // assert(BigDecimal.sqrt(2m, // { roundingMode: "half-even", // maximumSignificantDigits: 4 }) === 1.414m); @@ -281,7 +281,7 @@ function test_bigdecimal() // assert(BigDecimal.sqrt(0.002m, // { roundingMode: "half-even", // maximumFractionDigits: 3 }) === 0.045m); - + assert(BigDecimal.round(3.14159m, { roundingMode: "half-even", maximumFractionDigits: 3 }) === 3.142m); @@ -308,15 +308,14 @@ function test_bigdecimal() /* string conversion */ assert((1234.125m).toString(), "1234.125"); - // TODO: - // assert((1234.125m).toFixed(2), "1234.13"); - // assert((1234.125m).toFixed(2, "down"), "1234.12"); - // assert((1234.125m).toExponential(), "1.234125e+3"); - // assert((1234.125m).toExponential(5), "1.23413e+3"); - // assert((1234.125m).toExponential(5, "down"), "1.23412e+3"); - // assert((1234.125m).toPrecision(6), "1234.13"); - // assert((1234.125m).toPrecision(6, "down"), "1234.12"); - // assert((-1234.125m).toPrecision(6, "floor"), "-1234.13"); + assert((1234.125m).toFixed(2), "1234.13"); + assert((1234.125m).toFixed(2, "down"), "1234.12"); + assert((1234.125m).toExponential(), "1.234125e+3"); + assert((1234.125m).toExponential(5), "1.23413e+3"); + assert((1234.125m).toExponential(5, "down"), "1.23412e+3"); + assert((1234.125m).toPrecision(6), "1234.13"); + assert((1234.125m).toPrecision(6, "down"), "1234.12"); + assert((-1234.125m).toPrecision(6, "floor"), "-1234.13"); } test_bigint1(); test_bigint2();