mirror of
https://github.com/google/pebble.git
synced 2025-05-28 05:53:12 +00:00
Import of the watch repository from Pebble
This commit is contained in:
commit
3b92768480
10334 changed files with 2564465 additions and 0 deletions
43
tests/libc/math/test_floor.c
Normal file
43
tests/libc/math/test_floor.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* 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 <math.h>
|
||||
|
||||
#include "clar.h"
|
||||
|
||||
// "Define" libc functions we're testing
|
||||
#include "pblibc_private.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Tests
|
||||
|
||||
void test_floor__basic(void) {
|
||||
cl_assert(floor( 0.5) == 0.0);
|
||||
cl_assert(floor(-0.5) == -1.0);
|
||||
cl_assert(floor( 0.0) == 0.0);
|
||||
cl_assert(floor(-0.0) == -0.0);
|
||||
cl_assert(floor( 1.5) == 1.0);
|
||||
cl_assert(floor(-1.5) == -2.0);
|
||||
cl_assert(floor( 2.0) == 2.0);
|
||||
cl_assert(floor(-2.0) == -2.0);
|
||||
cl_assert(floor( 0.0001) == 0.0);
|
||||
cl_assert(floor(-0.0001) == -1.0);
|
||||
cl_assert(isnan(floor(NAN)));
|
||||
cl_assert(isinf(floor(INFINITY)));
|
||||
}
|
55
tests/libc/math/test_log.c
Normal file
55
tests/libc/math/test_log.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* 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 <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <fenv.h>
|
||||
|
||||
#include "clar.h"
|
||||
|
||||
double log_theirs(double x) {
|
||||
return log(x);
|
||||
}
|
||||
|
||||
// "Define" libc functions we're testing
|
||||
#include "pblibc_private.h"
|
||||
|
||||
void test_pow__initialize(void) {
|
||||
fesetround(FE_TONEAREST);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Tests
|
||||
|
||||
void test_log__basic(void) {
|
||||
for(int i = 1; i < 10000; i++) {
|
||||
double v = i * 0.001;
|
||||
double us = log(v);
|
||||
double them = log_theirs(v);
|
||||
|
||||
// 1 ulps is acceptable error
|
||||
// To actually check this, we need to do some sorta gross raw operations on the doubles
|
||||
int64_t diff = *(int64_t*)&us - *(int64_t*)&them;
|
||||
cl_assert(diff <= 1 && diff >= -1);
|
||||
}
|
||||
cl_assert(isnan(log(-1.0)));
|
||||
cl_assert(log(0) == -HUGE_VAL);
|
||||
cl_assert(isnan(log(NAN)));
|
||||
cl_assert(isinf(log(INFINITY)));
|
||||
}
|
51
tests/libc/math/test_pow.c
Normal file
51
tests/libc/math/test_pow.c
Normal 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 <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <fenv.h>
|
||||
|
||||
#include "clar.h"
|
||||
|
||||
double pow_theirs(double x, double y) {
|
||||
return pow(x,y);
|
||||
}
|
||||
|
||||
// "Define" libc functions we're testing
|
||||
#include "pblibc_private.h"
|
||||
|
||||
void test_pow__initialize(void) {
|
||||
fesetround(FE_TONEAREST);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Tests
|
||||
|
||||
void test_pow__basic(void) {
|
||||
for(int i = 0; i < 10000; i++) {
|
||||
double v = i * 0.001;
|
||||
double us = pow(2, v);
|
||||
double them = pow_theirs(2,v);
|
||||
|
||||
// 1 ulps is acceptable error
|
||||
// To actually check this, we need to do some sorta gross raw operations on the doubles
|
||||
int64_t diff = *(int64_t*)&us - *(int64_t*)&them;
|
||||
cl_assert(diff <= 1 && diff >= -1);
|
||||
}
|
||||
}
|
36
tests/libc/math/test_round.c
Normal file
36
tests/libc/math/test_round.c
Normal 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 <math.h>
|
||||
|
||||
#include "clar.h"
|
||||
|
||||
// "Define" libc functions we're testing
|
||||
#include "pblibc_private.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Tests
|
||||
|
||||
void test_round__round(void) {
|
||||
cl_assert_equal_d(round(0.0), 0.0);
|
||||
cl_assert_equal_d(round(0.4), 0.0);
|
||||
cl_assert_equal_d(round(-0.4), -0.0);
|
||||
cl_assert_equal_d(round(0.5), 1.0);
|
||||
cl_assert_equal_d(round(-0.5), -1.0);
|
||||
cl_assert_equal_d(round(-1.0), -1.0);
|
||||
cl_assert_equal_d(round(1.7976931348623157e+308), 1.7976931348623157e+308);
|
||||
cl_assert_equal_d(round(2.2250738585072014e-308), -0.0);
|
||||
}
|
28
tests/libc/math/wscript
Normal file
28
tests/libc/math/wscript
Normal file
|
@ -0,0 +1,28 @@
|
|||
from waftools.pebble_test import clar
|
||||
|
||||
def build(ctx):
|
||||
clar(ctx,
|
||||
sources_ant_glob = "src/libc/math/floor.c",
|
||||
test_sources_ant_glob = "test_floor.c",
|
||||
add_includes = ["src/libc"],
|
||||
test_libs=['m'])
|
||||
|
||||
clar(ctx,
|
||||
sources_ant_glob = "src/libc/math/log.c",
|
||||
test_sources_ant_glob = "test_log.c",
|
||||
add_includes = ["src/libc"],
|
||||
test_libs=['m'])
|
||||
|
||||
clar(ctx,
|
||||
sources_ant_glob = "src/libc/math/pow.c" \
|
||||
" src/libc/math/scalbn.c" \
|
||||
" src/libc/math/sqrt.c",
|
||||
test_sources_ant_glob = "test_pow.c",
|
||||
add_includes = ["src/libc"],
|
||||
test_libs=['m'])
|
||||
|
||||
clar(ctx,
|
||||
sources_ant_glob = "src/libc/math/round.c",
|
||||
test_sources_ant_glob = "test_round.c",
|
||||
add_includes = ["src/libc"],
|
||||
test_libs=['m'])
|
757
tests/libc/printf/test_sprintf.c
Normal file
757
tests/libc/printf/test_sprintf.c
Normal file
|
@ -0,0 +1,757 @@
|
|||
/*
|
||||
* 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 <math.h>
|
||||
|
||||
#include "clar.h"
|
||||
|
||||
// "Define" libc functions we're testing
|
||||
#include "pblibc_private.h"
|
||||
// I want real memset
|
||||
#undef memset
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Tests
|
||||
|
||||
void test_sprintf__basic(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
cl_assert_equal_i(snprintf(dstbuf, 256, "Hello!\nI am error"), 17);
|
||||
cl_assert_equal_s(dstbuf, "Hello!\nI am error");
|
||||
|
||||
snprintf(dstbuf, 256, "What is the %%d");
|
||||
cl_assert_equal_s(dstbuf, "What is the %d");
|
||||
|
||||
snprintf(dstbuf, 256, "What is the %%d");
|
||||
cl_assert_equal_s(dstbuf, "What is the %d");
|
||||
}
|
||||
|
||||
void test_sprintf__truncate(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
memset(dstbuf, 0xFF, 256);
|
||||
cl_assert_equal_i(snprintf(dstbuf, 17, "Hello!\nI am error"), 17);
|
||||
cl_assert_equal_m(dstbuf, "Hello!\nI am erro\0\xFF", 18);
|
||||
|
||||
memset(dstbuf, 0xFF, 256);
|
||||
cl_assert_equal_i(snprintf(dstbuf, 15, "Hello!\nI am error"), 17);
|
||||
cl_assert_equal_m(dstbuf, "Hello!\nI am er\0\xFF", 16);
|
||||
}
|
||||
|
||||
void test_sprintf__long_conversion(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
uint64_t val = 0xFFFFFFFFFFFFFFFFULL;
|
||||
snprintf(dstbuf, 256, "%#llo", val);
|
||||
cl_assert_equal_s(dstbuf, "01777777777777777777777");
|
||||
}
|
||||
|
||||
|
||||
void test_sprintf__null(void) {
|
||||
cl_assert_equal_i(snprintf(NULL, 0, "Hello!\nI am error"), 17);
|
||||
}
|
||||
|
||||
void test_sprintf__percent_d(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
// Simple %d
|
||||
snprintf(dstbuf, 256, "There are %d lights, %d", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, -4");
|
||||
|
||||
// Alternate form
|
||||
snprintf(dstbuf, 256, "There are %#d lights, %#d", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, -4");
|
||||
|
||||
// Zero padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %02d lights, %02d", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 04 lights, -4");
|
||||
|
||||
// Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %2d lights, %2d", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, -4");
|
||||
|
||||
// Left-align, Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %-2d lights, %-2d", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, -4");
|
||||
|
||||
// Space for positive signed
|
||||
snprintf(dstbuf, 256, "There are % d lights, the absolute value of % d", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, the absolute value of -4");
|
||||
|
||||
// Plus for positive signed
|
||||
snprintf(dstbuf, 256, "There are %+d lights, the absolute value of %+d", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are +4 lights, the absolute value of -4");
|
||||
|
||||
// Minimum digits output
|
||||
snprintf(dstbuf, 256, "There are %.2d lights, %.2d", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 04 lights, -04");
|
||||
|
||||
// Minimum digits output (zero digits)
|
||||
snprintf(dstbuf, 256, "%.0dzero%.0d", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (unspecified digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.dzero%.d", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.-3dzero%.-3d", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (non-zero digits outputting zero)
|
||||
snprintf(dstbuf, 256, "%.1dzero%.1d", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "0zero1");
|
||||
|
||||
// Variable length character output
|
||||
snprintf(dstbuf, 256, "There are %*d lights", 3, 4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights");
|
||||
|
||||
// Left-align, Variable length character output
|
||||
snprintf(dstbuf, 256, "There are %*d lights", -3, 4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights");
|
||||
|
||||
// Variable length digits output
|
||||
snprintf(dstbuf, 256, "There are %.*d lights", 3, 4);
|
||||
cl_assert_equal_s(dstbuf, "There are 004 lights");
|
||||
|
||||
// Variable length digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.*dzero%.*d", -3, 0, -3, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Length modifiers
|
||||
int64_t hurf = 0x123456789ABCDEF0ULL;
|
||||
snprintf(dstbuf, 256, "%hhd,%hd,%d,%ld", hurf, hurf, hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "-16,-8464,-1698898192,-1698898192");
|
||||
snprintf(dstbuf, 256, "%lld,%jd", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "1311768467463790320,1311768467463790320");
|
||||
snprintf(dstbuf, 256, "%zd,%td", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "-1698898192,-1698898192");
|
||||
}
|
||||
|
||||
// Literally copy-paste from %d
|
||||
void test_sprintf__percent_i(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
// Simple %i
|
||||
snprintf(dstbuf, 256, "There are %i lights, %i", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, -4");
|
||||
|
||||
// Alternate form
|
||||
snprintf(dstbuf, 256, "There are %#i lights, %#i", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, -4");
|
||||
|
||||
// Zero padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %02i lights, %02i", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 04 lights, -4");
|
||||
|
||||
// Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %2i lights, %2i", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, -4");
|
||||
|
||||
// Left-align, Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %-2i lights, %-2i", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, -4");
|
||||
|
||||
// Space for positive signed
|
||||
snprintf(dstbuf, 256, "There are % i lights, the absolute value of % i", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, the absolute value of -4");
|
||||
|
||||
// Plus for positive signed
|
||||
snprintf(dstbuf, 256, "There are %+i lights, the absolute value of %+i", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are +4 lights, the absolute value of -4");
|
||||
|
||||
// Minimum digits output
|
||||
snprintf(dstbuf, 256, "There are %.2i lights, %.2i", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 04 lights, -04");
|
||||
|
||||
// Minimum digits output (zero digits)
|
||||
snprintf(dstbuf, 256, "%.0izero%.0i", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (unspecified digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.izero%.i", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.-3izero%.-3i", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (non-zero digits outputting zero)
|
||||
snprintf(dstbuf, 256, "%.1izero%.1i", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "0zero1");
|
||||
|
||||
// Variable length character output
|
||||
snprintf(dstbuf, 256, "There are %*i lights", 3, 4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights");
|
||||
|
||||
// Left-align, Variable length character output
|
||||
snprintf(dstbuf, 256, "There are %*i lights", -3, 4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights");
|
||||
|
||||
// Variable length digits output
|
||||
snprintf(dstbuf, 256, "There are %.*i lights", 3, 4);
|
||||
cl_assert_equal_s(dstbuf, "There are 004 lights");
|
||||
|
||||
// Variable length digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.*izero%.*i", -3, 0, -3, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Length modifiers
|
||||
int64_t hurf = 0x123456789ABCDEF0ULL;
|
||||
snprintf(dstbuf, 256, "%hhi,%hi,%i,%li", hurf, hurf, hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "-16,-8464,-1698898192,-1698898192");
|
||||
snprintf(dstbuf, 256, "%lli,%ji", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "1311768467463790320,1311768467463790320");
|
||||
snprintf(dstbuf, 256, "%zi,%ti", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "-1698898192,-1698898192");
|
||||
}
|
||||
|
||||
void test_sprintf__percent_u(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
// Simple %u
|
||||
snprintf(dstbuf, 256, "There are %u lights, %u", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, 4294967292");
|
||||
|
||||
// Alternate form
|
||||
snprintf(dstbuf, 256, "There are %#u lights, %#u", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, 4294967292");
|
||||
|
||||
// Zero padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %02u lights, %02u", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 04 lights, 4294967292");
|
||||
|
||||
// Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %2u lights, %2u", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, 4294967292");
|
||||
|
||||
// Space for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "There are % u lights, the absolute value of % u", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, the absolute value of 4294967292");
|
||||
|
||||
// Plus for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "There are %+u lights, the absolute value of %+u", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights, the absolute value of 4294967292");
|
||||
|
||||
// Minimum digits output
|
||||
snprintf(dstbuf, 256, "There are %.2u lights, %.2u", 4, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 04 lights, 4294967292");
|
||||
|
||||
// Minimum digits output (zero digits)
|
||||
snprintf(dstbuf, 256, "%.0uzero%.0u", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (unspecified digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.uzero%.u", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.-3uzero%.-3u", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (non-zero digits outputting zero)
|
||||
snprintf(dstbuf, 256, "%.1uzero%.1u", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "0zero1");
|
||||
|
||||
// Variable length character output
|
||||
snprintf(dstbuf, 256, "There are %*u lights", 3, 4);
|
||||
cl_assert_equal_s(dstbuf, "There are 4 lights");
|
||||
|
||||
// Variable length digits output
|
||||
snprintf(dstbuf, 256, "There are %.*u lights", 3, 4);
|
||||
cl_assert_equal_s(dstbuf, "There are 004 lights");
|
||||
|
||||
// Variable length digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.*uzero%.*u", -3, 0, -3, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Length modifiers
|
||||
uint64_t hurf = 0x123456789ABCDEF0ULL;
|
||||
snprintf(dstbuf, 256, "%hhu,%hu,%u,%lu", hurf, hurf, hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "240,57072,2596069104,2596069104");
|
||||
snprintf(dstbuf, 256, "%llu,%ju", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "1311768467463790320,1311768467463790320");
|
||||
snprintf(dstbuf, 256, "%zu,%tu", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "2596069104,2596069104");
|
||||
}
|
||||
|
||||
void test_sprintf__percent_o(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
// Simple %o
|
||||
snprintf(dstbuf, 256, "There are %o lights, %o", 8, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, 37777777774");
|
||||
|
||||
// Alternate form (adds 0 prefix)
|
||||
snprintf(dstbuf, 256, "There are %#o lights, %#o", 8, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights, 037777777774");
|
||||
|
||||
// Zero padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %03o lights, %03o", 8, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights, 37777777774");
|
||||
|
||||
// Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %3o lights, %3o", 8, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, 37777777774");
|
||||
|
||||
// Space for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "There are % o lights, the absolute value of % o", 8, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, the absolute value of 37777777774");
|
||||
|
||||
// Plus for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "There are %+o lights, the absolute value of %+o", 8, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, the absolute value of 37777777774");
|
||||
|
||||
// Minimum digits output
|
||||
snprintf(dstbuf, 256, "There are %.3o lights, %.3o", 8, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights, 37777777774");
|
||||
|
||||
// Minimum digits output (zero digits)
|
||||
snprintf(dstbuf, 256, "%.0ozero%.0o", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (unspecified digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.ozero%.o", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.-3ozero%.-3o", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (non-zero digits outputting zero)
|
||||
snprintf(dstbuf, 256, "%.1ozero%.1o", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "0zero1");
|
||||
|
||||
// Variable length character output
|
||||
snprintf(dstbuf, 256, "There are %*o lights", 3, 8);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights");
|
||||
|
||||
// Variable length digits output
|
||||
snprintf(dstbuf, 256, "There are %.*o lights", 3, 8);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights");
|
||||
|
||||
// Variable length digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.*ozero%.*o", -3, 0, -3, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Horrible thing
|
||||
snprintf(dstbuf, 256, "%#.ozero", 0);
|
||||
cl_assert_equal_s(dstbuf, "0zero");
|
||||
|
||||
// Length modifiers
|
||||
uint64_t hurf = 0x123456789ABCDEF0ULL;
|
||||
snprintf(dstbuf, 256, "%hho,%ho,%o,%lo", hurf, hurf, hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "360,157360,23257157360,23257157360");
|
||||
snprintf(dstbuf, 256, "%llo,%jo", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "110642547423257157360,110642547423257157360");
|
||||
snprintf(dstbuf, 256, "%zo,%to", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "23257157360,23257157360");
|
||||
}
|
||||
|
||||
void test_sprintf__percent_x(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
// Simple %x
|
||||
snprintf(dstbuf, 256, "There are %x lights, %x", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, fffffffc");
|
||||
|
||||
// Alternate form (adds 0x prefix)
|
||||
snprintf(dstbuf, 256, "There are %#x lights, %#x", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 0x10 lights, 0xfffffffc");
|
||||
|
||||
// Zero padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %03x lights, %03x", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights, fffffffc");
|
||||
|
||||
// Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %3x lights, %3x", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, fffffffc");
|
||||
|
||||
// Space for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "There are % x lights, the absolute value of % x", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, the absolute value of fffffffc");
|
||||
|
||||
// Plus for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "There are %+x lights, the absolute value of %+x", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, the absolute value of fffffffc");
|
||||
|
||||
// Minimum digits output
|
||||
snprintf(dstbuf, 256, "There are %.3x lights, %.3x", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights, fffffffc");
|
||||
|
||||
// Minimum digits output (zero digits)
|
||||
snprintf(dstbuf, 256, "%.0xzero%.0x", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (unspecified digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.xzero%.x", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.-3xzero%.-3x", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (non-zero digits outputting zero)
|
||||
snprintf(dstbuf, 256, "%.1xzero%.1x", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "0zero1");
|
||||
|
||||
// Variable length character output
|
||||
snprintf(dstbuf, 256, "There are %*x lights", 3, 16);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights");
|
||||
|
||||
// Variable length digits output
|
||||
snprintf(dstbuf, 256, "There are %.*x lights", 3, 16);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights");
|
||||
|
||||
// Variable length digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.*xzero%.*x", -3, 0, -3, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Length modifiers
|
||||
uint64_t hurf = 0x123456789ABCDEF0ULL;
|
||||
snprintf(dstbuf, 256, "%hhx,%hx,%x,%lx", hurf, hurf, hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "f0,def0,9abcdef0,9abcdef0");
|
||||
snprintf(dstbuf, 256, "%llx,%jx", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "123456789abcdef0,123456789abcdef0");
|
||||
snprintf(dstbuf, 256, "%zx,%tx", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "9abcdef0,9abcdef0");
|
||||
}
|
||||
|
||||
void test_sprintf__percent_capitalx(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
// Simple %X
|
||||
snprintf(dstbuf, 256, "There are %X lights, %X", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, FFFFFFFC");
|
||||
|
||||
// Alternate form (adds 0X prefix)
|
||||
snprintf(dstbuf, 256, "There are %#X lights, %#X", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 0X10 lights, 0XFFFFFFFC");
|
||||
|
||||
// Zero padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %03X lights, %03X", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights, FFFFFFFC");
|
||||
|
||||
// Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "There are %3X lights, %3X", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, FFFFFFFC");
|
||||
|
||||
// Space for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "There are % X lights, the absolute value of % X", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, the absolute value of FFFFFFFC");
|
||||
|
||||
// Plus for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "There are %+X lights, the absolute value of %+X", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights, the absolute value of FFFFFFFC");
|
||||
|
||||
// Minimum digits output
|
||||
snprintf(dstbuf, 256, "There are %.3X lights, %.3X", 16, -4);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights, FFFFFFFC");
|
||||
|
||||
// Minimum digits output (zero digits)
|
||||
snprintf(dstbuf, 256, "%.0Xzero%.0X", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (unspecified digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.Xzero%.X", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.-3Xzero%.-3X", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Minimum digits output (non-zero digits outputting zero)
|
||||
snprintf(dstbuf, 256, "%.1Xzero%.1X", 0, 1);
|
||||
cl_assert_equal_s(dstbuf, "0zero1");
|
||||
|
||||
// Variable length character output
|
||||
snprintf(dstbuf, 256, "There are %*X lights", 3, 16);
|
||||
cl_assert_equal_s(dstbuf, "There are 10 lights");
|
||||
|
||||
// Variable length digits output
|
||||
snprintf(dstbuf, 256, "There are %.*X lights", 3, 16);
|
||||
cl_assert_equal_s(dstbuf, "There are 010 lights");
|
||||
|
||||
// Variable length digits output (negative digits, acts as zero)
|
||||
snprintf(dstbuf, 256, "%.*Xzero%.*X", -3, 0, -3, 1);
|
||||
cl_assert_equal_s(dstbuf, "zero1");
|
||||
|
||||
// Length modifiers
|
||||
uint64_t hurf = 0x123456789ABCDEF0ULL;
|
||||
snprintf(dstbuf, 256, "%hhX,%hX,%X,%lX", hurf, hurf, hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "F0,DEF0,9ABCDEF0,9ABCDEF0");
|
||||
snprintf(dstbuf, 256, "%llX,%jX", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "123456789ABCDEF0,123456789ABCDEF0");
|
||||
snprintf(dstbuf, 256, "%zX,%tX", hurf, hurf);
|
||||
cl_assert_equal_s(dstbuf, "9ABCDEF0,9ABCDEF0");
|
||||
}
|
||||
|
||||
void test_sprintf__percent_c(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
// Simple %c
|
||||
snprintf(dstbuf, 256, "Hur%c", 'f');
|
||||
cl_assert_equal_s(dstbuf, "Hurf");
|
||||
|
||||
// Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "Hur%2c", 'f');
|
||||
cl_assert_equal_s(dstbuf, "Hur f");
|
||||
|
||||
// Space for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "Hur% c", 'f');
|
||||
cl_assert_equal_s(dstbuf, "Hurf");
|
||||
|
||||
// Plus for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "Hur%+c", 'f');
|
||||
cl_assert_equal_s(dstbuf, "Hurf");
|
||||
|
||||
// Variable length character output
|
||||
snprintf(dstbuf, 256, "Hur%*c", 2, 'f');
|
||||
cl_assert_equal_s(dstbuf, "Hur f");
|
||||
}
|
||||
|
||||
void test_sprintf__percent_s(void) {
|
||||
char dstbuf[256];
|
||||
|
||||
// Simple %s
|
||||
snprintf(dstbuf, 256, "You know Bagu? %s", "Then I can let you cross");
|
||||
cl_assert_equal_s(dstbuf, "You know Bagu? Then I can let you cross");
|
||||
|
||||
// Space padded minimum character output
|
||||
snprintf(dstbuf, 256, "You know Bagu? %25s", "Then I can let you cross");
|
||||
cl_assert_equal_s(dstbuf, "You know Bagu? Then I can let you cross");
|
||||
|
||||
// Space for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "You know Bagu? % s", "Then I can let you cross");
|
||||
cl_assert_equal_s(dstbuf, "You know Bagu? Then I can let you cross");
|
||||
|
||||
// Plus for positive signed
|
||||
// No signed conversion occurs, so this is a no-op
|
||||
snprintf(dstbuf, 256, "You know Bagu? %+s", "Then I can let you cross");
|
||||
cl_assert_equal_s(dstbuf, "You know Bagu? Then I can let you cross");
|
||||
|
||||
// Variable length character output
|
||||
snprintf(dstbuf, 256, "You know Bagu? %*s", 25, "Then I can let you cross");
|
||||
cl_assert_equal_s(dstbuf, "You know Bagu? Then I can let you cross");
|
||||
|
||||
// Left align
|
||||
snprintf(dstbuf, 256, "You know Bagu? %-26s", "Then I can let you cross");
|
||||
cl_assert_equal_s(dstbuf, "You know Bagu? Then I can let you cross ");
|
||||
|
||||
// Maximum character output 123456789012345678901234
|
||||
snprintf(dstbuf, 256, "You know Bagu? %.19s", "Then I can let you cross");
|
||||
cl_assert_equal_s(dstbuf, "You know Bagu? Then I can let you ");
|
||||
|
||||
// Left-align, Space padded minimum + maximum character output
|
||||
snprintf(dstbuf, 256, "You know Bagu? %-25.19s", "Then I can let you cross");
|
||||
cl_assert_equal_s(dstbuf, "You know Bagu? Then I can let you ");
|
||||
}
|
||||
|
||||
void test_sprintf__percent_p(void) {
|
||||
// %p is almost entirely implementation defined.
|
||||
// Because of this, we are testing against newlib's output.
|
||||
// Newlib just treats it as %#x
|
||||
char dstbuf[256];
|
||||
|
||||
snprintf(dstbuf, 256, "What's a cool number? %p", (void*)(uintptr_t)0x02468ACE);
|
||||
cl_assert_equal_s(dstbuf, "What's a cool number? 0x2468ace");
|
||||
|
||||
snprintf(dstbuf, 256, "What's a cool number? %p", (void*)(uintptr_t)0);
|
||||
cl_assert_equal_s(dstbuf, "What's a cool number? 0");
|
||||
}
|
||||
|
||||
void test_sprintf__percent_n(void) {
|
||||
char dstbuf[512];
|
||||
int val, val2;
|
||||
int8_t hhntest[4];
|
||||
int16_t hntest[2];
|
||||
int32_t lntest[2];
|
||||
int64_t llntest[2];
|
||||
int64_t jntest[2];
|
||||
int32_t zntest[2];
|
||||
int32_t tntest[2];
|
||||
|
||||
snprintf(dstbuf, 256, "%n", &val);
|
||||
cl_assert_equal_i(val, 0);
|
||||
|
||||
snprintf(dstbuf, 256, "Incredible mechanical monster%n comming soon%n!!", &val, &val2);
|
||||
cl_assert_equal_i(val, 29);
|
||||
cl_assert_equal_i(val2, 42);
|
||||
|
||||
// Note: Newlib actually doesn't support %hhn, breaking standard.
|
||||
hhntest[0] = hhntest[1] = hhntest[2] = hhntest[3] = 0;
|
||||
snprintf(dstbuf, 512, "aaaa aaaa aaaa aaaa " // 0- 20
|
||||
"aaaa aaaa aaaa aaaa " // 20- 40
|
||||
"aaaa aaaa aaaa aaaa " // 40- 60
|
||||
"aaaa aaaa aaaa aaaa " // 60- 80
|
||||
"aaaa aaaa aaaa aaaa " // 80-100
|
||||
"aaaa aaaa aaaa aaaa " //100-120
|
||||
"aaaa aaaa aaaa aaaa " //120-140
|
||||
"aaaa aaaa aaaa aaaa " //140-160
|
||||
"aaaa aaaa aaaa aaaa " //160-180
|
||||
"aaaa aaaa aaaa aaaa " //180-200
|
||||
"aaaa aaaa aaaa aaaa " //200-220
|
||||
"aaaa aaaa aaaa aaaa " //220-240
|
||||
"aaaa aaaa aaaa aaaa " //240-260
|
||||
"aaaa aaaa aaaa aaaa " //260-280
|
||||
"%hhnaaaa aaaa aaaa aaaa " //280-300
|
||||
"aaaa aaaa aaaa aaaa " //300-320
|
||||
, hhntest);
|
||||
cl_assert_equal_i(hhntest[0], 280-256);
|
||||
cl_assert_equal_i(hhntest[1], 0);
|
||||
cl_assert_equal_i(hhntest[2], 0);
|
||||
cl_assert_equal_i(hhntest[3], 0);
|
||||
|
||||
hntest[0] = hntest[1] = 0;
|
||||
snprintf(dstbuf, 512, "aaaa aaaa aaaa aaaa " // 0- 20
|
||||
"aaaa aaaa aaaa aaaa " // 20- 40
|
||||
"aaaa aaaa aaaa aaaa " // 40- 60
|
||||
"aaaa aaaa aaaa aaaa " // 60- 80
|
||||
"aaaa aaaa aaaa aaaa " // 80-100
|
||||
"aaaa aaaa aaaa aaaa " //100-120
|
||||
"aaaa aaaa aaaa aaaa " //120-140
|
||||
"aaaa aaaa aaaa aaaa " //140-160
|
||||
"aaaa aaaa aaaa aaaa " //160-180
|
||||
"aaaa aaaa aaaa aaaa " //180-200
|
||||
"aaaa aaaa aaaa aaaa " //200-220
|
||||
"aaaa aaaa aaaa aaaa " //220-240
|
||||
"aaaa aaaa aaaa aaaa " //240-260
|
||||
"aaaa aaaa aaaa aaaa " //260-280
|
||||
"%hnaaaa aaaa aaaa aaaa " //280-300
|
||||
"aaaa aaaa aaaa aaaa " //300-320
|
||||
, hntest);
|
||||
cl_assert_equal_i(hntest[0], 280);
|
||||
cl_assert_equal_i(hntest[1], 0);
|
||||
|
||||
lntest[0] = lntest[1] = 0;
|
||||
snprintf(dstbuf, 512, "aaaa aaaa aaaa aaaa " // 0- 20
|
||||
"aaaa aaaa aaaa aaaa " // 20- 40
|
||||
"aaaa aaaa aaaa aaaa " // 40- 60
|
||||
"aaaa aaaa aaaa aaaa " // 60- 80
|
||||
"aaaa aaaa aaaa aaaa " // 80-100
|
||||
"aaaa aaaa aaaa aaaa " //100-120
|
||||
"aaaa aaaa aaaa aaaa " //120-140
|
||||
"aaaa aaaa aaaa aaaa " //140-160
|
||||
"aaaa aaaa aaaa aaaa " //160-180
|
||||
"aaaa aaaa aaaa aaaa " //180-200
|
||||
"aaaa aaaa aaaa aaaa " //200-220
|
||||
"aaaa aaaa aaaa aaaa " //220-240
|
||||
"aaaa aaaa aaaa aaaa " //240-260
|
||||
"aaaa aaaa aaaa aaaa " //260-280
|
||||
"%lnaaaa aaaa aaaa aaaa " //280-300
|
||||
"aaaa aaaa aaaa aaaa " //300-320
|
||||
, lntest);
|
||||
cl_assert_equal_i(lntest[0], 280);
|
||||
cl_assert_equal_i(lntest[1], 0);
|
||||
|
||||
llntest[0] = llntest[1] = 0;
|
||||
snprintf(dstbuf, 512, "aaaa aaaa aaaa aaaa " // 0- 20
|
||||
"aaaa aaaa aaaa aaaa " // 20- 40
|
||||
"aaaa aaaa aaaa aaaa " // 40- 60
|
||||
"aaaa aaaa aaaa aaaa " // 60- 80
|
||||
"aaaa aaaa aaaa aaaa " // 80-100
|
||||
"aaaa aaaa aaaa aaaa " //100-120
|
||||
"aaaa aaaa aaaa aaaa " //120-140
|
||||
"aaaa aaaa aaaa aaaa " //140-160
|
||||
"aaaa aaaa aaaa aaaa " //160-180
|
||||
"aaaa aaaa aaaa aaaa " //180-200
|
||||
"aaaa aaaa aaaa aaaa " //200-220
|
||||
"aaaa aaaa aaaa aaaa " //220-240
|
||||
"aaaa aaaa aaaa aaaa " //240-260
|
||||
"aaaa aaaa aaaa aaaa " //260-280
|
||||
"%llnaaaa aaaa aaaa aaaa " //280-300
|
||||
"aaaa aaaa aaaa aaaa " //300-320
|
||||
, llntest);
|
||||
cl_assert_equal_i(llntest[0], 280);
|
||||
cl_assert_equal_i(llntest[1], 0);
|
||||
|
||||
jntest[0] = jntest[1] = 0;
|
||||
snprintf(dstbuf, 512, "aaaa aaaa aaaa aaaa " // 0- 20
|
||||
"aaaa aaaa aaaa aaaa " // 20- 40
|
||||
"aaaa aaaa aaaa aaaa " // 40- 60
|
||||
"aaaa aaaa aaaa aaaa " // 60- 80
|
||||
"aaaa aaaa aaaa aaaa " // 80-100
|
||||
"aaaa aaaa aaaa aaaa " //100-120
|
||||
"aaaa aaaa aaaa aaaa " //120-140
|
||||
"aaaa aaaa aaaa aaaa " //140-160
|
||||
"aaaa aaaa aaaa aaaa " //160-180
|
||||
"aaaa aaaa aaaa aaaa " //180-200
|
||||
"aaaa aaaa aaaa aaaa " //200-220
|
||||
"aaaa aaaa aaaa aaaa " //220-240
|
||||
"aaaa aaaa aaaa aaaa " //240-260
|
||||
"aaaa aaaa aaaa aaaa " //260-280
|
||||
"%jnaaaa aaaa aaaa aaaa " //280-300
|
||||
"aaaa aaaa aaaa aaaa " //300-320
|
||||
, jntest);
|
||||
cl_assert_equal_i(jntest[0], 280);
|
||||
cl_assert_equal_i(jntest[1], 0);
|
||||
|
||||
zntest[0] = zntest[1] = 0;
|
||||
snprintf(dstbuf, 512, "aaaa aaaa aaaa aaaa " // 0- 20
|
||||
"aaaa aaaa aaaa aaaa " // 20- 40
|
||||
"aaaa aaaa aaaa aaaa " // 40- 60
|
||||
"aaaa aaaa aaaa aaaa " // 60- 80
|
||||
"aaaa aaaa aaaa aaaa " // 80-100
|
||||
"aaaa aaaa aaaa aaaa " //100-120
|
||||
"aaaa aaaa aaaa aaaa " //120-140
|
||||
"aaaa aaaa aaaa aaaa " //140-160
|
||||
"aaaa aaaa aaaa aaaa " //160-180
|
||||
"aaaa aaaa aaaa aaaa " //180-200
|
||||
"aaaa aaaa aaaa aaaa " //200-220
|
||||
"aaaa aaaa aaaa aaaa " //220-240
|
||||
"aaaa aaaa aaaa aaaa " //240-260
|
||||
"aaaa aaaa aaaa aaaa " //260-280
|
||||
"%znaaaa aaaa aaaa aaaa " //280-300
|
||||
"aaaa aaaa aaaa aaaa " //300-320
|
||||
, zntest);
|
||||
cl_assert_equal_i(zntest[0], 280);
|
||||
cl_assert_equal_i(zntest[1], 0);
|
||||
|
||||
tntest[0] = tntest[1] = 0;
|
||||
snprintf(dstbuf, 512, "aaaa aaaa aaaa aaaa " // 0- 20
|
||||
"aaaa aaaa aaaa aaaa " // 20- 40
|
||||
"aaaa aaaa aaaa aaaa " // 40- 60
|
||||
"aaaa aaaa aaaa aaaa " // 60- 80
|
||||
"aaaa aaaa aaaa aaaa " // 80-100
|
||||
"aaaa aaaa aaaa aaaa " //100-120
|
||||
"aaaa aaaa aaaa aaaa " //120-140
|
||||
"aaaa aaaa aaaa aaaa " //140-160
|
||||
"aaaa aaaa aaaa aaaa " //160-180
|
||||
"aaaa aaaa aaaa aaaa " //180-200
|
||||
"aaaa aaaa aaaa aaaa " //200-220
|
||||
"aaaa aaaa aaaa aaaa " //220-240
|
||||
"aaaa aaaa aaaa aaaa " //240-260
|
||||
"aaaa aaaa aaaa aaaa " //260-280
|
||||
"%tnaaaa aaaa aaaa aaaa " //280-300
|
||||
"aaaa aaaa aaaa aaaa " //300-320
|
||||
, tntest);
|
||||
cl_assert_equal_i(tntest[0], 280);
|
||||
cl_assert_equal_i(tntest[1], 0);
|
||||
}
|
7
tests/libc/printf/wscript
Normal file
7
tests/libc/printf/wscript
Normal file
|
@ -0,0 +1,7 @@
|
|||
from waftools.pebble_test import clar
|
||||
|
||||
def build(ctx):
|
||||
clar(ctx,
|
||||
sources_ant_glob = "src/libc/vsprintf.c",
|
||||
test_sources_ant_glob = "test_sprintf.c",
|
||||
add_includes = ["src/libc"])
|
106
tests/libc/string/test_atoi.c
Normal file
106
tests/libc/string/test_atoi.c
Normal 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);
|
||||
}
|
149
tests/libc/string/test_ctype.c
Normal file
149
tests/libc/string/test_ctype.c
Normal 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)
|
||||
}
|
51
tests/libc/string/test_memchr.c
Normal file
51
tests/libc/string/test_memchr.c
Normal 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);
|
||||
}
|
53
tests/libc/string/test_memcmp.c
Normal file
53
tests/libc/string/test_memcmp.c
Normal 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);
|
||||
}
|
78
tests/libc/string/test_memcpy.c
Normal file
78
tests/libc/string/test_memcpy.c
Normal 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);
|
||||
}
|
52
tests/libc/string/test_memset.c
Normal file
52
tests/libc/string/test_memset.c
Normal 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);
|
||||
}
|
73
tests/libc/string/test_strcat.c
Normal file
73
tests/libc/string/test_strcat.c
Normal 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);
|
||||
}
|
67
tests/libc/string/test_strchr.c
Normal file
67
tests/libc/string/test_strchr.c
Normal 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);
|
||||
}
|
||||
|
71
tests/libc/string/test_strcmp.c
Normal file
71
tests/libc/string/test_strcmp.c
Normal 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);
|
||||
}
|
79
tests/libc/string/test_strcpy.c
Normal file
79
tests/libc/string/test_strcpy.c
Normal 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);
|
||||
}
|
||||
|
36
tests/libc/string/test_strlen.c
Normal file
36
tests/libc/string/test_strlen.c
Normal 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);
|
||||
}
|
42
tests/libc/string/test_strspn.c
Normal file
42
tests/libc/string/test_strspn.c
Normal 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);
|
||||
}
|
38
tests/libc/string/test_strstr.c
Normal file
38
tests/libc/string/test_strstr.c
Normal 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);
|
||||
}
|
268
tests/libc/string/test_strtol.c
Normal file
268
tests/libc/string/test_strtol.c
Normal 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
87
tests/libc/string/wscript
Normal 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"])
|
1055
tests/libc/time/test_strftime.c
Normal file
1055
tests/libc/time/test_strftime.c
Normal file
File diff suppressed because it is too large
Load diff
15
tests/libc/time/wscript
Normal file
15
tests/libc/time/wscript
Normal file
|
@ -0,0 +1,15 @@
|
|||
from waftools.pebble_test import clar
|
||||
|
||||
def build(ctx):
|
||||
clar(ctx,
|
||||
sources_ant_glob = (
|
||||
" src/fw/flash_region/flash_region.c"
|
||||
" src/fw/applib/pbl_std/pbl_std.c"
|
||||
" src/fw/applib/pbl_std/strftime.c"
|
||||
" src/fw/applib/pbl_std/timelocal.c"
|
||||
" tests/fakes/fake_rtc.c"
|
||||
" tests/fakes/fake_spi_flash.c"
|
||||
),
|
||||
test_sources_ant_glob = "test_strftime.c",
|
||||
override_includes=['dummy_board'],
|
||||
add_includes = ["src/libc"])
|
Loading…
Add table
Add a link
Reference in a new issue