From 53733bd93de7bde4705511a325cd7d0f11ea42e3 Mon Sep 17 00:00:00 2001 From: Hitalo Souza <63821277+enghitalo@users.noreply.github.com> Date: Fri, 16 Feb 2024 06:03:59 -0400 Subject: [PATCH] picohttpparser: add tests for u64toa (#20822) --- vlib/picohttpparser/misc_test.v | 65 ++++++++++++++++++++++++++++++++ vlib/picohttpparser/response.c.v | 4 ++ 2 files changed, 69 insertions(+) create mode 100644 vlib/picohttpparser/misc_test.v diff --git a/vlib/picohttpparser/misc_test.v b/vlib/picohttpparser/misc_test.v new file mode 100644 index 00000000000000..a3fb7a54b8d902 --- /dev/null +++ b/vlib/picohttpparser/misc_test.v @@ -0,0 +1,65 @@ +module picohttpparser + +// Test various values under 10,000 +pub fn test_u64toa_small_values() { + for v in [u64(0), 1, 10, 99, 100, 999, 1000, 9999] { + mut buf := [10]u8{} + len := unsafe { u64toa(&buf[0], v) or { 0 } } + + assert len == expected_len(v) + + // Check the actual string for accuracy + assert buf[0..len] == v.str().bytes() + } +} + +// Test various values above 10,000 and error handling +pub fn test_u64toa_large_values() { + for i, v in [u64(10000), 12345, 99999, 100000, 999999, 12345678, 99_999_999, 100_000_000] { + mut buf := [20]u8{} + + len := unsafe { + u64toa(&buf[0], v) or { assert err.msg() == 'Maximum size of 100MB exceeded!' } + } + + if v < 100_000_000 { + assert len == expected_len(v) + + assert buf[0..len] == v.str().bytes() + } else { + assert len == 0 + } + } +} + +// Test edge cases +pub fn test_u64toa_edge_cases() { + mut buf := [10]u8{} + + // Test zero value + len := unsafe { + u64toa(&buf[0], 0) or { assert false } + } + + assert len == 1 + assert buf[0] == `0` +} + +// Helper functions for expected values +fn expected_len(v u64) int { + if v == 0 { + return 1 + } + + // return int(math.ceil(math.log10(f64(v + 1)))) + + mut count := 0 + mut temp := v + + for temp > 0 { + temp /= 10 + count++ + } + + return count +} diff --git a/vlib/picohttpparser/response.c.v b/vlib/picohttpparser/response.c.v index 5f6f59c721eed8..05216f0f0cb02a 100644 --- a/vlib/picohttpparser/response.c.v +++ b/vlib/picohttpparser/response.c.v @@ -1,5 +1,7 @@ module picohttpparser +#include + pub struct Response { pub: fd int @@ -105,6 +107,8 @@ pub fn (mut r Response) raw(response string) { r.write_string(response) } +fn C.send(sockfd int, buf voidptr, len usize, flags int) int + @[inline] pub fn (mut r Response) end() int { n := int(i64(r.buf) - i64(r.buf_start))