Import of the watch repository from Pebble

This commit is contained in:
Matthieu Jeanson 2024-12-12 16:43:03 -08:00 committed by Katharine Berry
commit 3b92768480
10334 changed files with 2564465 additions and 0 deletions

View file

@ -0,0 +1,106 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_atoi__basic(void) {
cl_assert_equal_i(atoi("500"), 500);
cl_assert_equal_i(atoi("765"), 765);
cl_assert_equal_i(atoi("573888"), 573888);
cl_assert_equal_i(atoi("713713"), 713713);
}
void test_atoi__whitespace_pfx(void) {
cl_assert_equal_i(atoi(" 500"), 500);
cl_assert_equal_i(atoi(" 765"), 765);
cl_assert_equal_i(atoi(" 573888"), 573888);
cl_assert_equal_i(atoi(" 713713"), 713713);
}
void test_atoi__suffix(void) {
cl_assert_equal_i(atoi("500hurf"), 500);
cl_assert_equal_i(atoi("765berserker"), 765);
cl_assert_equal_i(atoi("573888 redmage"), 573888);
cl_assert_equal_i(atoi("713713 4 job fiesta111"), 713713);
}
void test_atoi__sign(void) {
cl_assert_equal_i(atoi("+500"), 500);
cl_assert_equal_i(atoi("-765"), -765);
cl_assert_equal_i(atoi(" -573888"), -573888);
cl_assert_equal_i(atoi(" +713713"), +713713);
}
void test_atoi__error(void) {
cl_assert_equal_i(atoi("2147483647"), 2147483647); // last valid value
cl_assert_equal_i(atoi("4294967287"), (int)4294967287); // signed integer overflow
// ((2147483648 * 10) + 1) & 0xFFFFFFFF
cl_assert_equal_i(atoi("21474836481"), 1);
// ((2147483647 * 10) + 1) & 0xFFFFFFFF
cl_assert_equal_i(atoi("21474836471"), (int)4294967287);
// -1 * (((2147483647 * 10) + 1) & 0xFFFFFFFF)
cl_assert_equal_i(atoi("-21474836471"), -(int)4294967287);
}
// atol will actually behave _exactly_ the same as atoi, so these tests are just copy-pasted
// Would probably be good to make these more DRY
void test_atol__basic(void) {
cl_assert_equal_i(atol("500"), 500);
cl_assert_equal_i(atol("765"), 765);
cl_assert_equal_i(atol("573888"), 573888);
cl_assert_equal_i(atol("713713"), 713713);
}
void test_atol__whitespace_pfx(void) {
cl_assert_equal_i(atol(" 500"), 500);
cl_assert_equal_i(atol(" 765"), 765);
cl_assert_equal_i(atol(" 573888"), 573888);
cl_assert_equal_i(atol(" 713713"), 713713);
}
void test_atol__suffix(void) {
cl_assert_equal_i(atol("500hurf"), 500);
cl_assert_equal_i(atol("765berserker"), 765);
cl_assert_equal_i(atol("573888 redmage"), 573888);
cl_assert_equal_i(atol("713713 4 job fiesta111"), 713713);
}
void test_atol__sign(void) {
cl_assert_equal_i(atol("+500"), 500);
cl_assert_equal_i(atol("-765"), -765);
cl_assert_equal_i(atol(" -573888"), -573888);
cl_assert_equal_i(atol(" +713713"), +713713);
}
void test_atol__error(void) {
cl_assert_equal_i(atol("2147483647"), 2147483647); // last valid value
cl_assert_equal_i(atol("4294967287"), (int)4294967287); // signed integer overflow
// ((2147483648 * 10) + 1) & 0xFFFFFFFF
cl_assert_equal_i(atol("21474836481"), 1);
// ((2147483647 * 10) + 1) & 0xFFFFFFFF
cl_assert_equal_i(atol("21474836471"), (int)4294967287);
// -1 * (((2147483647 * 10) + 1) & 0xFFFFFFFF)
cl_assert_equal_i(atol("-21474836471"), -(int)4294967287);
}

View file

@ -0,0 +1,149 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Horrible hack to get glibc's ctype.h to define isascii et al.
// Some of the weird options that are set in the unit test build environment
// force certain files to be included into every compilation unit before the
// first line of the source file. One of these implicitly-included headers
// includes stdint.h, which causes the feature macros to be interpreted before
// the compilation unit has a chance to set any feature macros. To work around
// this, the features.h header guard needs to be undefined so that the feature
// macros get reexamined for the next libc include. Ugh.
#undef _FEATURES_H
#define _DEFAULT_SOURCE // Required for glibc 2.20+
#define _BSD_SOURCE
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "clar.h"
#define CTYPE_THEIRS(CHK) \
int CHK##_theirs(int c) { \
return CHK(c); \
}
CTYPE_THEIRS(isalpha)
CTYPE_THEIRS(isupper)
CTYPE_THEIRS(islower)
CTYPE_THEIRS(isdigit)
CTYPE_THEIRS(isxdigit)
CTYPE_THEIRS(isspace)
CTYPE_THEIRS(ispunct)
CTYPE_THEIRS(isalnum)
CTYPE_THEIRS(isprint)
CTYPE_THEIRS(isgraph)
CTYPE_THEIRS(iscntrl)
CTYPE_THEIRS(isascii)
CTYPE_THEIRS(toascii)
// We have to do this because glibc throws specification to the wind.
// Quoting from the C99 spec:
// If the argument is a character for which isupper is true and there are one or more
// corresponding characters, as specified by the current locale, for which islower is true,
// the tolower function returns one of the corresponding characters (always the same one
// for any given locale); otherwise, the argument is returned unchanged.
// Notably, glibc will mangle inputs that are not hitting isupper/islower. Thanks Obama.
int toupper_theirs(int c) {
if (islower(c)) {
return toupper(c);
} else {
return c;
}
}
int tolower_theirs(int c) {
if (isupper(c)) {
return tolower(c);
} else {
return c;
}
}
// "Define" libc functions we're testing
#include "pblibc_private.h"
#include "include/ctype.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
#define ISCTYPE_TEST(CHK) \
for(int i = -128; i < 256; i++) { \
cl_assert_equal_b(CHK##_theirs(i), CHK(i)); \
} \
/* validate double-evaluation behavior */ \
for(int i = -128; i < 256;) { \
int them = CHK##_theirs(i); \
int us = CHK(i++); \
cl_assert_equal_b(them, us); \
}
void test_ctype__isalpha(void) {
ISCTYPE_TEST(isalpha)
}
void test_ctype__isupper(void) {
ISCTYPE_TEST(isupper)
}
void test_ctype__islower(void) {
ISCTYPE_TEST(islower)
}
void test_ctype__isdigit(void) {
ISCTYPE_TEST(isdigit)
}
void test_ctype__isxdigit(void) {
ISCTYPE_TEST(isxdigit)
}
void test_ctype__isspace(void) {
ISCTYPE_TEST(isspace)
}
void test_ctype__ispunct(void) {
ISCTYPE_TEST(ispunct)
}
void test_ctype__isalnum(void) {
ISCTYPE_TEST(isalnum)
}
void test_ctype__isprint(void) {
ISCTYPE_TEST(isprint)
}
void test_ctype__isgraph(void) {
ISCTYPE_TEST(isgraph)
}
void test_ctype__iscntrl(void) {
ISCTYPE_TEST(iscntrl)
}
void test_ctype__isascii(void) {
ISCTYPE_TEST(isascii)
}
#define TOCTYPE_TEST(CHK) \
for(int i = -128; i < 256; i++) { \
cl_assert_equal_i(CHK##_theirs(i), CHK(i)); \
} \
/* validate double-evaluation behavior */ \
for(int i = -128; i < 256;) { \
int them = CHK##_theirs(i); \
int us = CHK(i++); \
cl_assert_equal_i(them, us); \
}
void test_ctype__toascii(void) {
TOCTYPE_TEST(toascii)
}
void test_ctype__toupper(void) {
TOCTYPE_TEST(toupper)
}
void test_ctype__tolower(void) {
TOCTYPE_TEST(tolower)
}

View file

@ -0,0 +1,51 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_memchr__basic(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x12, 0x78, 0xDE, 0xF0, };
cl_assert_equal_p(memchr(testbuf, 0x78, 8), testbuf+3);
}
void test_memchr__unfound(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
cl_assert_equal_p(memchr(testbuf, 0xFF, 8), NULL);
}
void test_memchr__short_found(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0x78, 0xDE, 0xF0, };
cl_assert_equal_p(memchr(testbuf, 0x78, 4), testbuf+3);
}
void test_memchr__short_unfound(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
cl_assert_equal_p(memchr(testbuf, 0x9A, 4), NULL);
}
void test_memchr__big_value(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0x78, 0xDE, 0xF0, };
cl_assert_equal_p(memchr(testbuf, 0xF78, 8), testbuf+3);
}

View file

@ -0,0 +1,53 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
// Asserting !memcmp as a bool:
// True = equal
// False = non-equal
void test_memcmp__same_buffer(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
cl_assert_equal_b(!memcmp(testbuf, testbuf, 8), true);
}
void test_memcmp__same_content(void) {
uint8_t testbuf1[8] = { 0x12, 0x00, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t testbuf2[8] = { 0x12, 0x00, 0x56, 0x78, 0x00, 0xBC, 0xDE, 0xF0, };
testbuf2[4] = 0x9A;
cl_assert_equal_b(!memcmp(testbuf1, testbuf2, 8), true);
}
void test_memcmp__different_content(void) {
uint8_t testbuf1[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t testbuf2[8] = { 0x12, 0x34, 0x56, 0x78, 0x00, 0xBC, 0xDE, 0xF0, };
cl_assert_equal_b(!memcmp(testbuf1, testbuf2, 8), false);
}
void test_memcmp__partial(void) {
uint8_t testbuf1[9] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x0D, };
uint8_t testbuf2[9] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0xBA, };
cl_assert_equal_b(!memcmp(testbuf1, testbuf2, 8), true);
}

View file

@ -0,0 +1,78 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_memcpy__basic(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t destbuf[8];
memcpy(destbuf, testbuf, 8);
cl_assert_equal_m(testbuf, destbuf, 8);
}
void test_memcpy__partial(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t destbuf[8] = { 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, };
uint8_t expectbuf[8] = { 0x9A, 0xBC, 0xDE, 0xF0, 0x78, 0x78, 0x78, 0x78, };
memcpy(destbuf, testbuf+4, 4);
cl_assert_equal_m(expectbuf, destbuf, 8);
}
void test_memcpy__return_value(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t destbuf[8];
cl_assert_equal_p(memcpy(destbuf, testbuf, 8), destbuf);
}
void test_memcpy__move_basic(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t destbuf[8];
memmove(destbuf, testbuf, 8);
cl_assert_equal_m(testbuf, destbuf, 8);
}
void test_memcpy__move_return_value(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t destbuf[8];
cl_assert_equal_p(memmove(destbuf, testbuf, 8), destbuf);
}
void test_memcpy__move_overwrite_backwards(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t destbuf[8];
memcpy(destbuf+4, testbuf, 4);
memmove(destbuf+2, destbuf+4, 4);
cl_assert_equal_m(testbuf, destbuf+2, 4);
cl_assert_equal_m(testbuf+2, destbuf+6, 2);
}
void test_memcpy__move_overwrite_forwards(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t destbuf[8];
memcpy(destbuf+0, testbuf, 4);
memmove(destbuf+2, destbuf+0, 4);
cl_assert_equal_m(testbuf, destbuf+2, 4);
cl_assert_equal_m(testbuf, destbuf+0, 2);
}

View file

@ -0,0 +1,52 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_memset__basic(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t expectbuf[8] = { 5, 5, 5, 5, 5, 5, 5, 5, };
memset(testbuf, 5, 8);
cl_assert_equal_m(testbuf, expectbuf, 8);
}
void test_memset__return(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
cl_assert_equal_p(memset(testbuf, 5, 8), testbuf);
}
void test_memset__partial(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t expectbuf[8] = { 0x12, 0x34, 0x56, 0x78, 5, 5, 5, 5, };
memset(testbuf+4, 5, 4);
cl_assert_equal_m(testbuf, expectbuf, 8);
}
void test_memset__big_value(void) {
uint8_t testbuf[8] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, };
uint8_t expectbuf[8] = { 5, 5, 5, 5, 5, 5, 5, 5, };
memset(testbuf, 0xF05, 8);
cl_assert_equal_m(testbuf, expectbuf, 8);
}

View file

@ -0,0 +1,73 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_strcat__basic(void) {
char destbuf[9] = "hi";
char expectbuf[9] = "hilarity";
strcat(destbuf, "larity");
cl_assert_equal_m(expectbuf, destbuf, 9);
}
void test_strcat__weird(void) {
char destbuf[9] = "hi\0five";
char expectbuf[9] = "hilarity";
strcat(destbuf, "larity");
cl_assert_equal_m(expectbuf, destbuf, 9);
}
void test_strcat__return(void) {
char destbuf[9] = "hi";
char expectbuf[9] = "hilarity";
cl_assert_equal_p(strcat(destbuf, "larity"), destbuf);
}
void test_strcat__n_basic(void) {
char destbuf[9] = "hi";
char expectbuf[9] = "hilarity";
strncat(destbuf, "larity", 6);
cl_assert_equal_m(expectbuf, destbuf, 9);
}
void test_strcat__n_overlarge(void) {
char destbuf[9] = "hi";
char expectbuf[9] = "hilariou";
strncat(destbuf, "lariousness", 6);
cl_assert_equal_m(expectbuf, destbuf, 9);
}
void test_strcat__n_weird(void) {
char destbuf[9] = "hi\0five";
char expectbuf[9] = "hilariou";
strncat(destbuf, "lariousness", 6);
cl_assert_equal_m(expectbuf, destbuf, 9);
}
void test_strcat__n_return(void) {
char destbuf[9] = "hi";
char expectbuf[9] = "hilarity";
cl_assert_equal_p(strncat(destbuf, "larity", 6), destbuf);
}

View file

@ -0,0 +1,67 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_strchr__basic(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_p(strchr(testbuf, 'l'), testbuf+2);
}
void test_strchr__unfound(void) {
char testbuf[10] = "Hello!B\0Z";
cl_assert_equal_p(strchr(testbuf, 'Z'), NULL);
}
void test_strchr__big_value(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_p(strchr(testbuf, 0xF00 + 'l'), testbuf+2);
}
void test_strchr__end_of_string(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_p(strchr(testbuf, '\0'), testbuf+7);
}
void test_strchr__r_basic(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_p(strrchr(testbuf, 'B'), testbuf+6);
}
void test_strchr__r_unfound(void) {
char testbuf[10] = "Hello!B\0Z";
cl_assert_equal_p(strrchr(testbuf, 'Z'), NULL);
}
void test_strchr__r_big_value(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_p(strrchr(testbuf, 0xF00 + 'B'), testbuf+6);
}
void test_strchr__r_end_of_string(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_p(strrchr(testbuf, '\0'), testbuf+7);
}

View file

@ -0,0 +1,71 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
// Asserting !strcmp as a bool:
// True = equal
// False = non-equal
void test_strcmp__same_buffer(void) {
char testbuf[8] = "Hello!\0\0";
cl_assert_equal_b(!strcmp(testbuf, testbuf), true);
}
void test_strcmp__same_content(void) {
char testbuf1[8] = "Hello!\0\0";
char testbuf2[8] = "Hello\0\0\0";
testbuf2[5] = '!';
cl_assert_equal_b(!strcmp(testbuf1, testbuf2), true);
}
void test_strcmp__different_content(void) {
char testbuf1[8] = "Hello!\0\0";
char testbuf2[8] = "Hello\0\0\0";
cl_assert_equal_b(!strcmp(testbuf1, testbuf2), false);
}
void test_strcmp__n_same_buffer(void) {
char testbuf[8] = "Hello!\0\0";
cl_assert_equal_b(!strncmp(testbuf, testbuf, 8), true);
}
void test_strcmp__n_same_content(void) {
char testbuf1[8] = "Hello!\0\0";
char testbuf2[8] = "Hello\0\0\0";
testbuf2[5] = '!';
cl_assert_equal_b(!strncmp(testbuf1, testbuf2, 8), true);
}
void test_strcmp__n_different_content(void) {
char testbuf1[8] = "Hello!\0\0";
char testbuf2[8] = "Hello\0\0\0";
cl_assert_equal_b(!strncmp(testbuf1, testbuf2, 8), false);
}
void test_strcmp__n_short(void) {
char testbuf1[8] = "Hello!G\0";
char testbuf2[8] = "HelloAB\0";
cl_assert_equal_b(!strncmp(testbuf1, testbuf2, 5), true);
}

View file

@ -0,0 +1,79 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_strcpy__basic(void) {
char testbuf[8] = "Hello!";
char destbuf[8] = "AAAAAAAA";
char expectbuf[8] = "Hello!\0A";
strcpy(destbuf, testbuf);
cl_assert_equal_m(expectbuf, destbuf, 8);
}
void test_strcpy__weird(void) {
char testbuf[8] = "He\0llo!";
char destbuf[8] = "AAAAAAAA";
char expectbuf[8] = "He\0AAAAA";
strcpy(destbuf, testbuf);
cl_assert_equal_m(expectbuf, destbuf, 8);
}
void test_strcpy__return(void) {
char testbuf[8] = "Hello!";
char destbuf[8] = "AAAAAAAA";
cl_assert_equal_p(strcpy(destbuf, testbuf), destbuf);
}
void test_strcpy__n_basic(void) {
char testbuf[8] = "Hello!";
char destbuf[8] = "AAAAAAAA";
char expectbuf[8] = "Hello!\0\0";
strncpy(destbuf, testbuf, 8);
cl_assert_equal_m(expectbuf, destbuf, 8);
}
void test_strcpy__n_weird(void) {
char testbuf[8] = "He\0llo!";
char destbuf[8] = "AAAAAAAA";
char expectbuf[8] = "He\0\0\0\0\0\0";
strncpy(destbuf, testbuf, 8);
cl_assert_equal_m(expectbuf, destbuf, 8);
}
void test_strcpy__n_big_string(void) {
char testbuf[16] = "Hello, I'm huge";
char destbuf[8] = "AAAAAAAA";
char expectbuf[8] = "Hello, I";
strncpy(destbuf, testbuf, 8);
cl_assert_equal_m(expectbuf, destbuf, 8);
}
void test_strcpy__n_return(void) {
char testbuf[8] = "Hello!";
char destbuf[8] = "AAAAAAAA";
cl_assert_equal_p(strncpy(destbuf, testbuf, 8), destbuf);
}

View file

@ -0,0 +1,36 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_strlen__basic(void) {
char testbuf[9] = "hi";
cl_assert_equal_i(strlen(testbuf), 2);
}
void test_strlen__weird(void) {
char testbuf[9] = "hi\0five";
cl_assert_equal_i(strlen(testbuf), 2);
}

View file

@ -0,0 +1,42 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_strspn__basic(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_i(strspn(testbuf, "Hel"), 4);
cl_assert_equal_i(strspn(testbuf, "Heo"), 2);
cl_assert_equal_i(strspn(testbuf, "B"), 0);
cl_assert_equal_i(strspn(testbuf, "Helo!B"), 7);
}
void test_strspn__c_basic(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_i(strcspn(testbuf, "o!B"), 4);
cl_assert_equal_i(strcspn(testbuf, "o"), 4);
cl_assert_equal_i(strcspn(testbuf, "!"), 5);
cl_assert_equal_i(strcspn(testbuf, "H"), 0);
}

View file

@ -0,0 +1,38 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_strstr__basic(void) {
char testbuf[8] = "Hello!B";
cl_assert_equal_p(strstr(testbuf, "lo!"), testbuf+3);
cl_assert_equal_p(strstr(testbuf, "log"), NULL);
}
void test_strstr__weird(void) {
char testbuf[8] = "He\0llo!B";
cl_assert_equal_p(strstr(testbuf, "l"), NULL);
cl_assert_equal_p(strstr(testbuf, "He"), testbuf);
}

View file

@ -0,0 +1,268 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include "clar.h"
// "Define" libc functions we're testing
#include "pblibc_private.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
//! Tests
void test_strtol__basic(void) {
cl_assert_equal_i(strtol("500", NULL, 10), 500);
cl_assert_equal_i(strtol("765", NULL, 10), 765);
cl_assert_equal_i(strtol("573888", NULL, 10), 573888);
cl_assert_equal_i(strtol("713713", NULL, 10), 713713);
cl_assert_equal_i(strtol("2147483646", NULL, 10), 2147483646);
cl_assert_equal_i(strtol("-2147483647", NULL, 10), -2147483647);
}
void test_strtol__whitespace_pfx(void) {
cl_assert_equal_i(strtol(" 500", NULL, 10), 500);
cl_assert_equal_i(strtol(" 765", NULL, 10), 765);
cl_assert_equal_i(strtol(" 573888", NULL, 10), 573888);
cl_assert_equal_i(strtol(" 713713", NULL, 10), 713713);
}
void test_strtol__suffix(void) {
cl_assert_equal_i(strtol("500hurf", NULL, 10), 500);
cl_assert_equal_i(strtol("765berserker", NULL, 10), 765);
cl_assert_equal_i(strtol("573888 redmage", NULL, 10), 573888);
cl_assert_equal_i(strtol("713713 4 job fiesta111", NULL, 10), 713713);
}
void test_strtol__sign(void) {
cl_assert_equal_i(strtol("+500", NULL, 10), 500);
cl_assert_equal_i(strtol("-765", NULL, 10), -765);
cl_assert_equal_i(strtol(" -573888", NULL, 10), -573888);
cl_assert_equal_i(strtol(" +713713", NULL, 10), +713713);
}
void test_strtol__error(void) {
cl_assert_equal_i(strtol("2147483647", NULL, 10), 2147483647); // last valid value
cl_assert_equal_i(strtol("-2147483648", NULL, 10), -2147483648); // last valid value
cl_assert_equal_i(strtol("3294967287", NULL, 10), INT_MAX); // signed integer overflow
cl_assert_equal_i(strtol("2147483648", NULL, 10), INT_MAX);
cl_assert_equal_i(strtol("-2147483649", NULL, 10), INT_MIN);
}
static const struct {
intmax_t value;
int base;
const char *str;
} s_base_test_data[] = {
{ 2147483646LL, 2, "1111111111111111111111111111110", },
{ 2147483646LL, 2, "1111111111111111111111111111110", },
{ -2147483647LL, 2, "-1111111111111111111111111111111", },
{ -2147483647LL, 2, "-1111111111111111111111111111111", },
{ 2147483646LL, 3, "12112122212110202100", },
{ 2147483646LL, 3, "12112122212110202100", },
{ -2147483647LL, 3, "-12112122212110202101", },
{ -2147483647LL, 3, "-12112122212110202101", },
{ 2147483646LL, 4, "1333333333333332", },
{ 2147483646LL, 4, "1333333333333332", },
{ -2147483647LL, 4, "-1333333333333333", },
{ -2147483647LL, 4, "-1333333333333333", },
{ 2147483646LL, 5, "13344223434041", },
{ 2147483646LL, 5, "13344223434041", },
{ -2147483647LL, 5, "-13344223434042", },
{ -2147483647LL, 5, "-13344223434042", },
{ 2147483646LL, 6, "553032005530", },
{ 2147483646LL, 6, "553032005530", },
{ -2147483647LL, 6, "-553032005531", },
{ -2147483647LL, 6, "-553032005531", },
{ 2147483646LL, 7, "104134211160", },
{ 2147483646LL, 7, "104134211160", },
{ -2147483647LL, 7, "-104134211161", },
{ -2147483647LL, 7, "-104134211161", },
{ 2147483646LL, 8, "17777777776", },
{ 2147483646LL, 8, "17777777776", },
{ -2147483647LL, 8, "-17777777777", },
{ -2147483647LL, 8, "-17777777777", },
{ 2147483646LL, 9, "5478773670", },
{ 2147483646LL, 9, "5478773670", },
{ -2147483647LL, 9, "-5478773671", },
{ -2147483647LL, 9, "-5478773671", },
{ 2147483646LL, 10, "2147483646", },
{ 2147483646LL, 10, "2147483646", },
{ -2147483647LL, 10, "-2147483647", },
{ -2147483647LL, 10, "-2147483647", },
{ 2147483646LL, 11, "A02220280", },
{ 2147483646LL, 11, "a02220280", },
{ -2147483647LL, 11, "-A02220281", },
{ -2147483647LL, 11, "-a02220281", },
{ 2147483646LL, 12, "4BB2308A6", },
{ 2147483646LL, 12, "4bb2308a6", },
{ -2147483647LL, 12, "-4BB2308A7", },
{ -2147483647LL, 12, "-4bb2308a7", },
{ 2147483646LL, 13, "282BA4AA9", },
{ 2147483646LL, 13, "282ba4aa9", },
{ -2147483647LL, 13, "-282BA4AAA", },
{ -2147483647LL, 13, "-282ba4aaa", },
{ 2147483646LL, 14, "1652CA930", },
{ 2147483646LL, 14, "1652ca930", },
{ -2147483647LL, 14, "-1652CA931", },
{ -2147483647LL, 14, "-1652ca931", },
{ 2147483646LL, 15, "C87E66B6", },
{ 2147483646LL, 15, "c87e66b6", },
{ -2147483647LL, 15, "-C87E66B7", },
{ -2147483647LL, 15, "-c87e66b7", },
{ 2147483646LL, 16, "7FFFFFFE", },
{ 2147483646LL, 16, "7ffffffe", },
{ -2147483647LL, 16, "-7FFFFFFF", },
{ -2147483647LL, 16, "-7fffffff", },
{ 2147483646LL, 17, "53G7F547", },
{ 2147483646LL, 17, "53g7f547", },
{ -2147483647LL, 17, "-53G7F548", },
{ -2147483647LL, 17, "-53g7f548", },
{ 2147483646LL, 18, "3928G3H0", },
{ 2147483646LL, 18, "3928g3h0", },
{ -2147483647LL, 18, "-3928G3H1", },
{ -2147483647LL, 18, "-3928g3h1", },
{ 2147483646LL, 19, "27C57H31", },
{ 2147483646LL, 19, "27c57h31", },
{ -2147483647LL, 19, "-27C57H32", },
{ -2147483647LL, 19, "-27c57h32", },
{ 2147483646LL, 20, "1DB1F926", },
{ 2147483646LL, 20, "1db1f926", },
{ -2147483647LL, 20, "-1DB1F927", },
{ -2147483647LL, 20, "-1db1f927", },
{ 2147483646LL, 21, "140H2D90", },
{ 2147483646LL, 21, "140h2d90", },
{ -2147483647LL, 21, "-140H2D91", },
{ -2147483647LL, 21, "-140h2d91", },
{ 2147483646LL, 22, "IKF5BF0", },
{ 2147483646LL, 22, "ikf5bf0", },
{ -2147483647LL, 22, "-IKF5BF1", },
{ -2147483647LL, 22, "-ikf5bf1", },
{ 2147483646LL, 23, "EBELF94", },
{ 2147483646LL, 23, "ebelf94", },
{ -2147483647LL, 23, "-EBELF95", },
{ -2147483647LL, 23, "-ebelf95", },
{ 2147483646LL, 24, "B5GGE56", },
{ 2147483646LL, 24, "b5gge56", },
{ -2147483647LL, 24, "-B5GGE57", },
{ -2147483647LL, 24, "-b5gge57", },
{ 2147483646LL, 25, "8JMDNKL", },
{ 2147483646LL, 25, "8jmdnkl", },
{ -2147483647LL, 25, "-8JMDNKM", },
{ -2147483647LL, 25, "-8jmdnkm", },
{ 2147483646LL, 26, "6OJ8IOM", },
{ 2147483646LL, 26, "6oj8iom", },
{ -2147483647LL, 26, "-6OJ8ION", },
{ -2147483647LL, 26, "-6oj8ion", },
{ 2147483646LL, 27, "5EHNCK9", },
{ 2147483646LL, 27, "5ehnck9", },
{ -2147483647LL, 27, "-5EHNCKA", },
{ -2147483647LL, 27, "-5ehncka", },
{ 2147483646LL, 28, "4CLM98E", },
{ 2147483646LL, 28, "4clm98e", },
{ -2147483647LL, 28, "-4CLM98F", },
{ -2147483647LL, 28, "-4clm98f", },
{ 2147483646LL, 29, "3HK7986", },
{ 2147483646LL, 29, "3hk7986", },
{ -2147483647LL, 29, "-3HK7987", },
{ -2147483647LL, 29, "-3hk7987", },
{ 2147483646LL, 30, "2SB6CS6", },
{ 2147483646LL, 30, "2sb6cs6", },
{ -2147483647LL, 30, "-2SB6CS7", },
{ -2147483647LL, 30, "-2sb6cs7", },
{ 2147483646LL, 31, "2D09UC0", },
{ 2147483646LL, 31, "2d09uc0", },
{ -2147483647LL, 31, "-2D09UC1", },
{ -2147483647LL, 31, "-2d09uc1", },
{ 2147483646LL, 32, "1VVVVVU", },
{ 2147483646LL, 32, "1vvvvvu", },
{ -2147483647LL, 32, "-1VVVVVV", },
{ -2147483647LL, 32, "-1vvvvvv", },
{ 2147483646LL, 33, "1LSQTL0", },
{ 2147483646LL, 33, "1lsqtl0", },
{ -2147483647LL, 33, "-1LSQTL1", },
{ -2147483647LL, 33, "-1lsqtl1", },
{ 2147483646LL, 34, "1D8XQRO", },
{ 2147483646LL, 34, "1d8xqro", },
{ -2147483647LL, 34, "-1D8XQRP", },
{ -2147483647LL, 34, "-1d8xqrp", },
{ 2147483646LL, 35, "15V22UL", },
{ 2147483646LL, 35, "15v22ul", },
{ -2147483647LL, 35, "-15V22UM", },
{ -2147483647LL, 35, "-15v22um", },
{ 2147483646LL, 36, "ZIK0ZI", },
{ 2147483646LL, 36, "zik0zi", },
{ -2147483647LL, 36, "-ZIK0ZJ", },
{ -2147483647LL, 36, "-zik0zj", },
{ 0,0,"" },
};
void test_strtol__altbase(void) {
for(int i = 0; s_base_test_data[i].base != 0; i++) {
cl_assert_equal_i(strtol(s_base_test_data[i].str, NULL, s_base_test_data[i].base),
s_base_test_data[i].value);
}
}
void test_strtol__zerobase(void) {
cl_assert_equal_i(strtol("573bb", NULL, 0), 573);
cl_assert_equal_i(strtol("0x573", NULL, 0), 0x573);
cl_assert_equal_i(strtol("0573", NULL, 0), 0573);
cl_assert_equal_i(strtol(" +573bb", NULL, 0), 573);
cl_assert_equal_i(strtol(" +0x573ghghghgh", NULL, 0), 0x573);
cl_assert_equal_i(strtol(" +0573faf", NULL, 0), 0573);
cl_assert_equal_i(strtol(" -573bb", NULL, 0), -573);
cl_assert_equal_i(strtol(" -0x573ghghghgh", NULL, 0), -0x573);
cl_assert_equal_i(strtol(" -0573faf", NULL, 0), -0573);
}
void test_strtol__bogus(void) {
cl_assert_equal_i(strtol(" ", NULL, 10), 0);
cl_assert_equal_i(strtol(" -", NULL, 10), 0);
cl_assert_equal_i(strtol("-", NULL, 10), 0);
cl_assert_equal_i(strtol(" +", NULL, 10), 0);
cl_assert_equal_i(strtol("+", NULL, 10), 0);
cl_assert_equal_i(strtol(" -+123", NULL, 10), 0);
cl_assert_equal_i(strtol("+-123", NULL, 10), 0);
}
void test_strtol__end(void) {
char *end;
char *s;
s = "";
strtol(s, &end, 10);
cl_assert_equal_p(s, end);
cl_assert_equal_i('\0', *end);
s = "123";
strtol(s, &end, 10);
cl_assert_equal_p(s+3, end);
cl_assert_equal_i('\0', *end);
s = "123a";
strtol(s, &end, 10);
cl_assert_equal_p(s+3, end);
cl_assert_equal_i('a', *end);
s = "a123";
strtol(s, &end, 10);
cl_assert_equal_p(s, end);
cl_assert_equal_i('a', *end);
}

87
tests/libc/string/wscript Normal file
View file

@ -0,0 +1,87 @@
from waftools.pebble_test import clar
def build(ctx):
clar(ctx,
sources_ant_glob = "src/libc/string/memcmp.c",
test_sources_ant_glob = "test_memcmp.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/memcpy.c",
test_sources_ant_glob = "test_memcpy.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/memset.c",
test_sources_ant_glob = "test_memset.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/memchr.c",
test_sources_ant_glob = "test_memchr.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/atoi.c" \
" src/libc/string/strtol.c",
test_sources_ant_glob = "test_atoi.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/strtol.c",
test_sources_ant_glob = "test_strtol.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/strcat.c" \
" src/libc/string/strlen.c" \
" src/libc/string/memcpy.c" \
" src/libc/string/memset.c" \
" src/libc/string/strcpy.c",
test_sources_ant_glob = "test_strcat.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/strlen.c",
test_sources_ant_glob = "test_strlen.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/strlen.c" \
" src/libc/string/memcpy.c" \
" src/libc/string/memset.c" \
" src/libc/string/strcpy.c",
test_sources_ant_glob = "test_strcpy.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/strlen.c" \
" src/libc/string/memcmp.c" \
" src/libc/string/strcmp.c",
test_sources_ant_glob = "test_strcmp.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/strlen.c" \
" src/libc/string/memchr.c" \
" src/libc/string/strchr.c",
test_sources_ant_glob = "test_strchr.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/strspn.c",
test_sources_ant_glob = "test_strspn.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/string/strlen.c" \
" src/libc/string/memcmp.c" \
" src/libc/string/strstr.c" \
" src/libc/string/strcmp.c",
test_sources_ant_glob = "test_strstr.c",
add_includes = ["src/libc"])
clar(ctx,
sources_ant_glob = "src/libc/ctype_ptr.c",
test_sources_ant_glob = "test_ctype.c",
add_includes = ["src/libc"])