cosmopolitan/test/libc/intrin/strlen_test.c

185 lines
7 KiB
C
Raw Normal View History

2020-06-15 14:18:57 +00:00
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
2020-12-28 01:18:44 +00:00
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
2020-06-15 14:18:57 +00:00
2020-12-28 01:18:44 +00:00
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
2020-06-15 14:18:57 +00:00
*/
2022-08-11 19:13:18 +00:00
#include "libc/intrin/bits.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/rand.h"
#include "libc/stdio/stdio.h"
2020-06-15 14:18:57 +00:00
#include "libc/str/str.h"
2020-10-27 10:39:46 +00:00
#include "libc/testlib/ezbench.h"
2020-06-15 14:18:57 +00:00
#include "libc/testlib/testlib.h"
char u8[] = "utf-8 ☻";
char16_t u16[] = u"utf16 ☻";
wchar_t u32[] = L"utf32 ☻";
Make numerous improvements - Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
2021-09-28 05:58:51 +00:00
size_t strlen_pure(const char *s) {
size_t n = 0;
while (*s++) ++n;
return n;
}
2020-06-15 14:18:57 +00:00
TEST(strlen, usageExample_c11) {
_Alignas(16) char ugh[] = "eeeeeeeeeeeeeee\017";
EXPECT_EQ(1, strlen_pure(ugh + 15));
2020-06-15 14:18:57 +00:00
EXPECT_EQ(6 + 3, strlen(u8));
EXPECT_EQ(7, strlen16(u16));
EXPECT_EQ(7, wcslen(u32));
2020-06-15 14:18:57 +00:00
}
TEST(strlen, usageExample_c99) {
EXPECT_EQ(6 + 3, strlen(u8));
EXPECT_EQ(7, strlen16(u16));
EXPECT_EQ(7, wcslen(u32));
}
TEST(strlen, whatMemoryLooksLike) {
EXPECT_BINEQ(u"utf-8 Γÿ╗ ", u8); /* ← thompson-pike encoded */
EXPECT_BINEQ(u"u t f 1 6   ;&  ", u16);
EXPECT_BINEQ(u"u   t   f   3   2       ;&      ", u32);
}
TEST(strlen, test_const) {
const char buf[] = "hellothere";
ASSERT_EQ(0, strlen(""));
ASSERT_EQ(0, strnlen("e", 0));
ASSERT_EQ(10, strlen(buf));
}
TEST(strlen, test_nonconst) {
char buf[256];
unsigned i;
for (i = 0; i < 255; ++i) buf[i] = i + 1;
buf[i] = '\0';
ASSERT_EQ(255, strlen(buf));
}
TEST(strnlen, testconst) {
ASSERT_EQ(0, strnlen_s(NULL, 3));
ASSERT_EQ(0, strnlen("", 3));
ASSERT_EQ(0, strnlen("a", 0));
ASSERT_EQ(3, strnlen("123", 3));
ASSERT_EQ(2, strnlen("123", 2));
ASSERT_EQ(3, strnlen("123", 4));
}
TEST(strlen, testnonconst) {
2020-12-24 07:42:56 +00:00
/* this test case is a great example of why we need:
"m"(*(char(*)[0x7fffffff])StR)
rather than:
"m"(*StR) */
2020-06-15 14:18:57 +00:00
char buf[256];
unsigned i;
2020-12-24 07:42:56 +00:00
for (i = 0; i < 250; ++i) buf[i] = i + 1;
2020-06-15 14:18:57 +00:00
buf[i] = '\0';
2020-12-24 07:42:56 +00:00
ASSERT_EQ(250, strlen(buf));
2020-06-15 14:18:57 +00:00
}
TEST(strnlen_s, null_ReturnsZero) {
ASSERT_EQ(0, strnlen_s(NULL, 3));
}
2023-05-09 05:27:03 +00:00
TEST(wcsnlen_s, null_ReturnsZero) {
ASSERT_EQ(0, wcsnlen_s(NULL, 3));
}
2020-06-15 14:18:57 +00:00
TEST(strnlen, nulNotFound_ReturnsSize) {
int sizes[] = {1, 2, 7, 8, 15, 16, 31, 32, 33};
for (unsigned i = 0; i < ARRAYLEN(sizes); ++i) {
char *buf = malloc(sizes[i]);
2020-06-15 14:18:57 +00:00
memset(buf, ' ', sizes[i]);
ASSERT_EQ(sizes[i], strnlen(buf, sizes[i]), "%d", sizes[i]);
free(buf);
2020-06-15 14:18:57 +00:00
}
}
2022-06-23 17:21:07 +00:00
TEST(strnlen_s, nulNotFound) {
2020-06-15 14:18:57 +00:00
char buf[3] = {1, 2, 3};
2022-06-23 17:21:07 +00:00
ASSERT_EQ(3, strnlen_s(buf, 3));
2020-06-15 14:18:57 +00:00
}
2023-05-09 05:27:03 +00:00
TEST(wcsnlen_s, nulNotFound) {
wchar_t buf[3] = {1, 2, 3};
ASSERT_EQ(3, wcsnlen_s(buf, 3));
}
TEST(strlen, fuzz) {
char *b;
size_t n, n1, n2;
for (n = 2; n < 1026; ++n) {
2022-10-10 11:12:06 +00:00
b = rngset(calloc(1, n), n - 1, _rand64, -1);
n1 = strlen(b);
n2 = strlen_pure(b);
ASSERT_EQ(n1, n2, "%#.*s", n, b);
n1 = strlen(b + 1);
n2 = strlen_pure(b + 1);
ASSERT_EQ(n1, n2);
free(b);
}
}
2020-10-27 10:39:46 +00:00
BENCH(strlen, bench) {
extern size_t strlen_(const char *) asm("strlen");
extern size_t strlen_pure_(const char *) asm("strlen_pure");
static char b[2048];
2021-05-17 01:22:39 +00:00
static char c[512];
static char d[256];
2020-10-27 10:39:46 +00:00
memset(b, -1, sizeof(b) - 1);
2021-05-17 01:22:39 +00:00
memset(c, -1, sizeof(c) - 1);
memset(d, -1, sizeof(d) - 1);
EZBENCH2("strlen 1", donothing, strlen_(""));
EZBENCH2("strlen_pure 1", donothing, strlen_pure_(""));
EZBENCH2("strlen 2", donothing, strlen_("1"));
EZBENCH2("strlen_pure 2", donothing, strlen_pure_("1"));
EZBENCH2("strlen 3", donothing, strlen_("11"));
EZBENCH2("strlen_pure 3", donothing, strlen_pure_("11"));
EZBENCH2("strlen 4", donothing, strlen_("113"));
EZBENCH2("strlen_pure 4", donothing, strlen_pure_("113"));
EZBENCH2("strlen 7", donothing, strlen_("123456"));
EZBENCH2("strlen_pure 7", donothing, strlen_pure_("123456"));
EZBENCH2("strlen 8", donothing, strlen_("1234567"));
EZBENCH2("strlen_pure 8", donothing, strlen_pure_("1234567"));
EZBENCH2("strlen 9", donothing, strlen_("12345678"));
EZBENCH2("strlen_pure 9", donothing, strlen_pure_("12345678"));
EZBENCH2("strlen 11", donothing, strlen_("12345678aa"));
EZBENCH2("strlen_pure 11", donothing, strlen_pure_("12345678aa"));
EZBENCH2("strlen 13", donothing, strlen_("12345678aabb"));
EZBENCH2("strlen_pure 13", donothing, strlen_pure_("12345678aabb"));
EZBENCH2("strlen 16", donothing, strlen_("123456781234567"));
EZBENCH2("strlen_pure 16", donothing, strlen_pure_("123456781234567"));
EZBENCH2("strlen 17", donothing, strlen_("123456781234567e"));
EZBENCH2("strlen_pure 17", donothing, strlen_pure_("123456781234567e"));
EZBENCH2("strlen 34", donothing,
2021-05-17 01:22:39 +00:00
strlen_("123456781234567e123456781234567ee"));
EZBENCH2("strlen_pure 34", donothing,
2021-05-17 01:22:39 +00:00
strlen_pure_("123456781234567e123456781234567ee"));
EZBENCH2("strlen 68", donothing,
2021-05-17 01:22:39 +00:00
strlen_("123456781234567e123456781234567ee123456781234567e1234567812"
"34567eee"));
EZBENCH2("strlen_pure 68", donothing,
2021-05-17 01:22:39 +00:00
strlen_pure_("123456781234567e123456781234567ee123456781234567e12345"
"6781234567eee"));
EZBENCH2("strlen 256", donothing, strlen_(d));
EZBENCH2("strlen_pure 256", donothing, strlen_pure_(d));
EZBENCH2("strlen 512", donothing, strlen_(c));
EZBENCH2("strlen_pure 512", donothing, strlen_pure_(c));
EZBENCH2("strlen 2048", donothing, strlen_(b));
EZBENCH2("strlen_pure 2048", donothing, strlen_pure_(b));
2020-10-27 10:39:46 +00:00
}