mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
* Move to PROGMEM aware libc, allow PSTR in printf() A Newlib (libc) patch is in progress to move the _P functions from inside Arduino into first-class citizens in libc. This Arduino patch cleans up code that's been migrated there. Binaries for the new libs are included because it seems they're part of the Arduino git tree, and should be replaced with @igrr built ones when/if the Newlib changes are accepted. Notable changes/additions for Arduino: Allow for use of PROGMEM based format and parameter strings in all *printf functions. No need for copying PSTR()s into RAM before printing them out (transparently saves heap space when using _P functions) and makes it easier to print out constant strings for applications. Add "%S" (capital-S) format that I've been told, but cannot verify, is used in Arduino to specify a PROGMEM string parameter in printfs, as an alias for "%s" since plain "%s" can now handle PROGMEM. Optimized the memcpy_P, strnlen_P, and strncpy_P functions to use 32-bit direct reads whenver possible (source and dest alignment mediated), but there is still room for improvement in others. Finally, move several constant arrays from RODATA into PROGMEM and update their accessors. Among these are the ctype array, ~260 bytes, mprec* arrays, ~300 bytes, and strings/daycounts in the time formatting functions, ~200 bytes. All told, sketches will see from 300 to 800 additional RAM heap free on startup (depending on their use of these routines). * Fix merge error in #ifdef/#endif * Fix host test using the newlib generic pgmspace.h Host tests now use the sys/pgmspace.h for compiles instead of the ESP8266-specific version. * Update with rebuilt libraries using latest newlib * Include binaries built directly from @igrr repo Rebuild the binaries using a git clone of https://github.com/igrr/newlib-xtensa Build commands for posterity: ```` rm -rf ./xtensa-lx106-elf/ ./configure --prefix=<DIR>/esp8266/tools/sdk/libc --with-newlib \ --enable-multilib --disable-newlib-io-c99-formats \ --disable-newlib-supplied-syscalls \ --enable-newlib-nano-formatted-io --enable-newlib-reent-small \ --enable-target-optspace \ --program-transform-name="s&^&xtensa-lx106-elf-&" \ --disable-option-checking --with-target-subdir=xtensa-lx106-elf \ --target=xtensa-lx106-elf rm -f etc/config.cache CROSS_CFLAGS="-fno-omit-frame-pointer -DSIGNAL_PROVIDED -DABORT_PROVIDED"\ " -DMALLOC_PROVIDED" \ PATH=<DIR>/esp8266/tools/xtensa-lx106-elf/bin/:$PATH \ make all install ```` * Fix merge define conflict in c_types.h * Fix strlen_P misaligned source error Include fix from newlib-xtensa/fix-strlen branch cleaning up misaligned access on a non-aligned source string. * Fix strlen_P and strcpy_P edge cases Ran the included test suite on ESP8266 tstring.c with the following defines: #define MAX_1 50 #define memcmp memcmp_P #define memcpy memcpy_P #define memmem memmem_P #define memchr memchr_P #define strcat strcat_P #define strncat strncat_P #define strcpy strcpy_P #define strlen strlen_P #define strnlen strnlen_P #define strcmp strcmp_P #define strncmp strncmp_P Uncovered edge case and return value problems in the optimized versions of the strnlen_P and strncpy_P functions. Corrected. * Fix memcpy_P return value memcpy-1.c test suite showed error in return value of memcpy_P. Correct it. * Fix strnlen_P/strlen_P off-by-4 error Random crashes, often on String constructors using a PSTR, would occur due to the accelerated strnlen_P going past the end of the string. Would make debug builds fail, too (ESP.getVersionString() failure). Fix to fall through to normal copy on a word that's got a 0 byte anywhere in it. * Add device tests for libc functional verification Add test suite used to debug libc optimized _P functions to the device tests. * Rebuild from igrr's repo (same source as prior) Rebuild .a from igrr's repo at 347260af117b4177389e69fd4d04169b11d87a97 * WIP - add exceptions * Fix exception to have 0-terminator * Move some exception constants to TEXT from RODATA * Remove throw stubs * Move more exception stuff to ROM * Enable exceptions in platform.io * Remove atexit, is duplicated in rebuilt lib Need to look at the quick-toolchain options, there seems to be a definition for atexit defined there (libgcc?) that needs to be excised. For now, remove our local do-nothing copy. * Update libgcc to remove soft-fp functions The esp-quick-toolchain generated libgcc.a needed to have the soft-FP routines that are in ROM removed from it. Remove them in the new esp-quick-toolchain and update. * Fix merge typos in Makefile * Add unhandled exception handler to postmortem * Return our atexit() handler * Latest stdc++, minimize exception emercengy area * Remove atexit from newlib atexit was defined in newlib strongly, but we also define a noop atexit in core. Since we never exit, use the core's noop and delete the atexit from libc.a Updated in esp-quick-toolchain as well. * Move __FUNCTION__ static strings to PROGMEM __FUNCTION__ is unlikely to be a timing sensitive variable, so move it to PROGMEM and not RODATA (RAM) using linker magic. asserts() now should take no RAM for any strings. * Clean up linker file, update to latest stdc++ * Update to latest stdc++ which doesn't call strerror * Update to GCC5.1 exception emergency allocator Using GCC 5.1's emergency memory allocator for exceptions, much less space is required in programs which do not use exceptions and when space is allocated it is managed more efficiently. * Initial try with new compiler toolchain * Include newlib built from esp-quick-toolchain * Update JSON with all new esp-quick-toolchain builds * Use 64bit Windows compiler on 64bit Windows * Dump std::exception.what() when possible When doing the panic on unhandled exceptions, try and grab the .what() pointer and dump it as part of the termination info. Makes it easy to see mem errors (std::bad_alloc) or std::runtime_error strings. * Use scripted install from esp-quick-toolchain Makes sure proper libraries and includes are present by using a scripted installation from esp-quick-install instead of a manual one. * Update eqk to remove atexit, fix packaging diff
576 lines
19 KiB
C
576 lines
19 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define memcmp memcmp_P
|
|
#define memcpy memcpy_P
|
|
#define memmem memmem_P
|
|
#define memchr memchr_P
|
|
#define strcat strcat_P
|
|
#define strncat strncat_P
|
|
#define strcpy strcpy_P
|
|
#define strncpy strncpy_P
|
|
#define strlen strlen_P
|
|
#define strnlen strnlen_P
|
|
#define strcmp strcmp_P
|
|
#define strncmp strncmp_P
|
|
|
|
|
|
_CONST char *it = "<UNSET>"; /* Routine name for message routines. */
|
|
static int errors = 0;
|
|
|
|
/* Complain if condition is not true. */
|
|
#define check(thing) checkit(thing, __LINE__)
|
|
|
|
static void
|
|
_DEFUN(checkit,(ok,l),
|
|
int ok _AND
|
|
int l )
|
|
|
|
{
|
|
// newfunc(it);
|
|
// line(l);
|
|
|
|
if (!ok)
|
|
{
|
|
printf("string.c:%d %s\n", l, it);
|
|
++errors;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* Complain if first two args don't strcmp as equal. */
|
|
#define equal(a, b) funcqual(a,b,__LINE__);
|
|
|
|
static void
|
|
_DEFUN(funcqual,(a,b,l),
|
|
char *a _AND
|
|
char *b _AND
|
|
int l)
|
|
{
|
|
// newfunc(it);
|
|
|
|
// line(l);
|
|
if (a == NULL && b == NULL) return;
|
|
if (strcmp(a,b)) {
|
|
printf("string.c:%d (%s)\n", l, it);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static char one[50];
|
|
static char two[50];
|
|
|
|
|
|
void libm_test_string()
|
|
{
|
|
/* Test strcmp first because we use it to test other things. */
|
|
it = "strcmp";
|
|
check(strcmp("", "") == 0); /* Trivial case. */
|
|
check(strcmp("a", "a") == 0); /* Identity. */
|
|
check(strcmp("abc", "abc") == 0); /* Multicharacter. */
|
|
check(strcmp("abc", "abcd") < 0); /* Length mismatches. */
|
|
check(strcmp("abcd", "abc") > 0);
|
|
check(strcmp("abcd", "abce") < 0); /* Honest miscompares. */
|
|
check(strcmp("abce", "abcd") > 0);
|
|
check(strcmp("a\103", "a") > 0); /* Tricky if char signed. */
|
|
check(strcmp("a\103", "a\003") > 0);
|
|
|
|
/* Test strcpy next because we need it to set up other tests. */
|
|
it = "strcpy";
|
|
check(strcpy(one, "abcd") == one); /* Returned value. */
|
|
equal(one, "abcd"); /* Basic test. */
|
|
|
|
(void) strcpy(one, "x");
|
|
equal(one, "x"); /* Writeover. */
|
|
equal(one+2, "cd"); /* Wrote too much? */
|
|
|
|
(void) strcpy(two, "hi there");
|
|
(void) strcpy(one, two);
|
|
equal(one, "hi there"); /* Basic test encore. */
|
|
equal(two, "hi there"); /* Stomped on source? */
|
|
|
|
(void) strcpy(one, "");
|
|
equal(one, ""); /* Boundary condition. */
|
|
|
|
/* strcat. */
|
|
it = "strcat";
|
|
(void) strcpy(one, "ijk");
|
|
check(strcat(one, "lmn") == one); /* Returned value. */
|
|
equal(one, "ijklmn"); /* Basic test. */
|
|
|
|
(void) strcpy(one, "x");
|
|
(void) strcat(one, "yz");
|
|
equal(one, "xyz"); /* Writeover. */
|
|
equal(one+4, "mn"); /* Wrote too much? */
|
|
|
|
(void) strcpy(one, "gh");
|
|
(void) strcpy(two, "ef");
|
|
(void) strcat(one, two);
|
|
equal(one, "ghef"); /* Basic test encore. */
|
|
equal(two, "ef"); /* Stomped on source? */
|
|
|
|
(void) strcpy(one, "");
|
|
(void) strcat(one, "");
|
|
equal(one, ""); /* Boundary conditions. */
|
|
(void) strcpy(one, "ab");
|
|
(void) strcat(one, "");
|
|
equal(one, "ab");
|
|
(void) strcpy(one, "");
|
|
(void) strcat(one, "cd");
|
|
equal(one, "cd");
|
|
|
|
/* strncat - first test it as strcat, with big counts,
|
|
then test the count mechanism. */
|
|
it = "strncat";
|
|
(void) strcpy(one, "ijk");
|
|
check(strncat(one, "lmn", 99) == one); /* Returned value. */
|
|
equal(one, "ijklmn"); /* Basic test. */
|
|
|
|
(void) strcpy(one, "x");
|
|
(void) strncat(one, "yz", 99);
|
|
equal(one, "xyz"); /* Writeover. */
|
|
equal(one+4, "mn"); /* Wrote too much? */
|
|
|
|
(void) strcpy(one, "gh");
|
|
(void) strcpy(two, "ef");
|
|
(void) strncat(one, two, 99);
|
|
equal(one, "ghef"); /* Basic test encore. */
|
|
equal(two, "ef"); /* Stomped on source? */
|
|
|
|
(void) strcpy(one, "");
|
|
(void) strncat(one, "", 99);
|
|
equal(one, ""); /* Boundary conditions. */
|
|
(void) strcpy(one, "ab");
|
|
(void) strncat(one, "", 99);
|
|
equal(one, "ab");
|
|
(void) strcpy(one, "");
|
|
(void) strncat(one, "cd", 99);
|
|
equal(one, "cd");
|
|
|
|
(void) strcpy(one, "ab");
|
|
(void) strncat(one, "cdef", 2);
|
|
equal(one, "abcd"); /* Count-limited. */
|
|
|
|
(void) strncat(one, "gh", 0);
|
|
equal(one, "abcd"); /* Zero count. */
|
|
|
|
(void) strncat(one, "gh", 2);
|
|
equal(one, "abcdgh"); /* Count _AND length equal. */
|
|
it = "strncmp";
|
|
/* strncmp - first test as strcmp with big counts";*/
|
|
check(strncmp("", "", 99) == 0); /* Trivial case. */
|
|
check(strncmp("a", "a", 99) == 0); /* Identity. */
|
|
check(strncmp("abc", "abc", 99) == 0); /* Multicharacter. */
|
|
check(strncmp("abc", "abcd", 99) < 0); /* Length unequal. */
|
|
check(strncmp("abcd", "abc",99) > 0);
|
|
check(strncmp("abcd", "abce", 99) < 0); /* Honestly unequal. */
|
|
check(strncmp("abce", "abcd",99)>0);
|
|
check(strncmp("abce", "abcd", 3) == 0); /* Count limited. */
|
|
check(strncmp("abce", "abc", 3) == 0); /* Count == length. */
|
|
check(strncmp("abcd", "abce", 4) < 0); /* Nudging limit. */
|
|
check(strncmp("abc", "def", 0) == 0); /* Zero count. */
|
|
|
|
/* strncpy - testing is a bit different because of odd semantics. */
|
|
it = "strncpy";
|
|
check(strncpy(one, "abc", 4) == one); /* Returned value. */
|
|
equal(one, "abc"); /* Did the copy go right? */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) strncpy(one, "xyz", 2);
|
|
equal(one, "xycdefgh"); /* Copy cut by count. */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) strncpy(one, "xyz", 3); /* Copy cut just before NUL. */
|
|
equal(one, "xyzdefgh");
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) strncpy(one, "xyz", 4); /* Copy just includes NUL. */
|
|
equal(one, "xyz");
|
|
equal(one+4, "efgh"); /* Wrote too much? */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) strncpy(one, "xyz", 5); /* Copy includes padding. */
|
|
equal(one, "xyz");
|
|
equal(one+4, "");
|
|
equal(one+5, "fgh");
|
|
|
|
(void) strcpy(one, "abc");
|
|
(void) strncpy(one, "xyz", 0); /* Zero-length copy. */
|
|
equal(one, "abc");
|
|
|
|
(void) strncpy(one, "", 2); /* Zero-length source. */
|
|
equal(one, "");
|
|
equal(one+1, "");
|
|
equal(one+2, "c");
|
|
|
|
(void) strcpy(one, "hi there");
|
|
(void) strncpy(two, one, 9);
|
|
equal(two, "hi there"); /* Just paranoia. */
|
|
equal(one, "hi there"); /* Stomped on source? */
|
|
|
|
/* strlen. */
|
|
it = "strlen";
|
|
check(strlen("") == 0); /* Empty. */
|
|
check(strlen("a") == 1); /* Single char. */
|
|
check(strlen("abcd") == 4); /* Multiple chars. */
|
|
|
|
/* strchr. */
|
|
it = "strchr";
|
|
check(strchr("abcd", 'z') == NULL); /* Not found. */
|
|
(void) strcpy(one, "abcd");
|
|
check(strchr(one, 'c') == one+2); /* Basic test. */
|
|
check(strchr(one, 'd') == one+3); /* End of string. */
|
|
check(strchr(one, 'a') == one); /* Beginning. */
|
|
check(strchr(one, '\0') == one+4); /* Finding NUL. */
|
|
(void) strcpy(one, "ababa");
|
|
check(strchr(one, 'b') == one+1); /* Finding first. */
|
|
(void) strcpy(one, "");
|
|
check(strchr(one, 'b') == NULL); /* Empty string. */
|
|
check(strchr(one, '\0') == one); /* NUL in empty string. */
|
|
|
|
/* index - just like strchr. */
|
|
it = "index";
|
|
check(index("abcd", 'z') == NULL); /* Not found. */
|
|
(void) strcpy(one, "abcd");
|
|
check(index(one, 'c') == one+2); /* Basic test. */
|
|
check(index(one, 'd') == one+3); /* End of string. */
|
|
check(index(one, 'a') == one); /* Beginning. */
|
|
check(index(one, '\0') == one+4); /* Finding NUL. */
|
|
(void) strcpy(one, "ababa");
|
|
check(index(one, 'b') == one+1); /* Finding first. */
|
|
(void) strcpy(one, "");
|
|
check(index(one, 'b') == NULL); /* Empty string. */
|
|
check(index(one, '\0') == one); /* NUL in empty string. */
|
|
|
|
/* strrchr. */
|
|
it = "strrchr";
|
|
check(strrchr("abcd", 'z') == NULL); /* Not found. */
|
|
(void) strcpy(one, "abcd");
|
|
check(strrchr(one, 'c') == one+2); /* Basic test. */
|
|
check(strrchr(one, 'd') == one+3); /* End of string. */
|
|
check(strrchr(one, 'a') == one); /* Beginning. */
|
|
check(strrchr(one, '\0') == one+4); /* Finding NUL. */
|
|
(void) strcpy(one, "ababa");
|
|
check(strrchr(one, 'b') == one+3); /* Finding last. */
|
|
(void) strcpy(one, "");
|
|
check(strrchr(one, 'b') == NULL); /* Empty string. */
|
|
check(strrchr(one, '\0') == one); /* NUL in empty string. */
|
|
|
|
/* rindex - just like strrchr. */
|
|
it = "rindex";
|
|
check(rindex("abcd", 'z') == NULL); /* Not found. */
|
|
(void) strcpy(one, "abcd");
|
|
check(rindex(one, 'c') == one+2); /* Basic test. */
|
|
check(rindex(one, 'd') == one+3); /* End of string. */
|
|
check(rindex(one, 'a') == one); /* Beginning. */
|
|
check(rindex(one, '\0') == one+4); /* Finding NUL. */
|
|
(void) strcpy(one, "ababa");
|
|
check(rindex(one, 'b') == one+3); /* Finding last. */
|
|
(void) strcpy(one, "");
|
|
check(rindex(one, 'b') == NULL); /* Empty string. */
|
|
check(rindex(one, '\0') == one); /* NUL in empty string. */
|
|
|
|
/* strpbrk - somewhat like strchr. */
|
|
it = "strpbrk";
|
|
check(strpbrk("abcd", "z") == NULL); /* Not found. */
|
|
(void) strcpy(one, "abcd");
|
|
check(strpbrk(one, "c") == one+2); /* Basic test. */
|
|
check(strpbrk(one, "d") == one+3); /* End of string. */
|
|
check(strpbrk(one, "a") == one); /* Beginning. */
|
|
check(strpbrk(one, "") == NULL); /* Empty search list. */
|
|
check(strpbrk(one, "cb") == one+1); /* Multiple search. */
|
|
(void) strcpy(one, "abcabdea");
|
|
check(strpbrk(one, "b") == one+1); /* Finding first. */
|
|
check(strpbrk(one, "cb") == one+1); /* With multiple search. */
|
|
check(strpbrk(one, "db") == one+1); /* Another variant. */
|
|
(void) strcpy(one, "");
|
|
check(strpbrk(one, "bc") == NULL); /* Empty string. */
|
|
check(strpbrk(one, "") == NULL); /* Both strings empty. */
|
|
|
|
/* strstr - somewhat like strchr. */
|
|
it = "strstr";
|
|
check(strstr("z", "abcd") == NULL); /* Not found. */
|
|
check(strstr("abx", "abcd") == NULL); /* Dead end. */
|
|
(void) strcpy(one, "abcd");
|
|
check(strstr(one,"c") == one+2); /* Basic test. */
|
|
check(strstr(one, "bc") == one+1); /* Multichar. */
|
|
check(strstr(one,"d") == one+3); /* End of string. */
|
|
check(strstr(one,"cd") == one+2); /* Tail of string. */
|
|
check(strstr(one,"abc") == one); /* Beginning. */
|
|
check(strstr(one,"abcd") == one); /* Exact match. */
|
|
check(strstr(one,"de") == NULL); /* Past end. */
|
|
check(strstr(one,"") == one); /* Finding empty. */
|
|
(void) strcpy(one, "ababa");
|
|
check(strstr(one,"ba") == one+1); /* Finding first. */
|
|
(void) strcpy(one, "");
|
|
check(strstr(one, "b") == NULL); /* Empty string. */
|
|
check(strstr(one,"") == one); /* Empty in empty string. */
|
|
(void) strcpy(one, "bcbca");
|
|
check(strstr(one,"bca") == one+2); /* False start. */
|
|
(void) strcpy(one, "bbbcabbca");
|
|
check(strstr(one,"bbca") == one+1); /* With overlap. */
|
|
|
|
/* strspn. */
|
|
it = "strspn";
|
|
check(strspn("abcba", "abc") == 5); /* Whole string. */
|
|
check(strspn("abcba", "ab") == 2); /* Partial. */
|
|
check(strspn("abc", "qx") == 0); /* None. */
|
|
check(strspn("", "ab") == 0); /* Null string. */
|
|
check(strspn("abc", "") == 0); /* Null search list. */
|
|
|
|
/* strcspn. */
|
|
it = "strcspn";
|
|
check(strcspn("abcba", "qx") == 5); /* Whole string. */
|
|
check(strcspn("abcba", "cx") == 2); /* Partial. */
|
|
check(strcspn("abc", "abc") == 0); /* None. */
|
|
check(strcspn("", "ab") == 0); /* Null string. */
|
|
check(strcspn("abc", "") == 3); /* Null search list. */
|
|
|
|
/* strtok - the hard one. */
|
|
it = "strtok";
|
|
(void) strcpy(one, "first, second, third");
|
|
equal(strtok(one, ", "), "first"); /* Basic test. */
|
|
equal(one, "first");
|
|
equal(strtok((char *)NULL, ", "), "second");
|
|
equal(strtok((char *)NULL, ", "), "third");
|
|
check(strtok((char *)NULL, ", ") == NULL);
|
|
(void) strcpy(one, ", first, ");
|
|
equal(strtok(one, ", "), "first"); /* Extra delims, 1 tok. */
|
|
check(strtok((char *)NULL, ", ") == NULL);
|
|
(void) strcpy(one, "1a, 1b; 2a, 2b");
|
|
equal(strtok(one, ", "), "1a"); /* Changing delim lists. */
|
|
equal(strtok((char *)NULL, "; "), "1b");
|
|
equal(strtok((char *)NULL, ", "), "2a");
|
|
(void) strcpy(two, "x-y");
|
|
equal(strtok(two, "-"), "x"); /* New string before done. */
|
|
equal(strtok((char *)NULL, "-"), "y");
|
|
check(strtok((char *)NULL, "-") == NULL);
|
|
(void) strcpy(one, "a,b, c,, ,d");
|
|
equal(strtok(one, ", "), "a"); /* Different separators. */
|
|
equal(strtok((char *)NULL, ", "), "b");
|
|
equal(strtok((char *)NULL, " ,"), "c"); /* Permute list too. */
|
|
equal(strtok((char *)NULL, " ,"), "d");
|
|
check(strtok((char *)NULL, ", ") == NULL);
|
|
check(strtok((char *)NULL, ", ") == NULL); /* Persistence. */
|
|
(void) strcpy(one, ", ");
|
|
check(strtok(one, ", ") == NULL); /* No tokens. */
|
|
(void) strcpy(one, "");
|
|
check(strtok(one, ", ") == NULL); /* Empty string. */
|
|
(void) strcpy(one, "abc");
|
|
equal(strtok(one, ", "), "abc"); /* No delimiters. */
|
|
check(strtok((char *)NULL, ", ") == NULL);
|
|
(void) strcpy(one, "abc");
|
|
equal(strtok(one, ""), "abc"); /* Empty delimiter list. */
|
|
check(strtok((char *)NULL, "") == NULL);
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) strcpy(one, "a,b,c");
|
|
equal(strtok(one, ","), "a"); /* Basics again... */
|
|
equal(strtok((char *)NULL, ","), "b");
|
|
equal(strtok((char *)NULL, ","), "c");
|
|
check(strtok((char *)NULL, ",") == NULL);
|
|
equal(one+6, "gh"); /* Stomped past end? */
|
|
equal(one, "a"); /* Stomped old tokens? */
|
|
equal(one+2, "b");
|
|
equal(one+4, "c");
|
|
|
|
/* memcmp. */
|
|
it = "memcmp";
|
|
check(memcmp("a", "a", 1) == 0); /* Identity. */
|
|
check(memcmp("abc", "abc", 3) == 0); /* Multicharacter. */
|
|
check(memcmp("abcd", "abce", 4) < 0); /* Honestly unequal. */
|
|
check(memcmp("abce", "abcd",4));
|
|
check(memcmp("alph", "beta", 4) < 0);
|
|
check(memcmp("abce", "abcd", 3) == 0); /* Count limited. */
|
|
check(memcmp("abc", "def", 0) == 0); /* Zero count. */
|
|
|
|
/* memcmp should test strings as unsigned */
|
|
one[0] = 0xfe;
|
|
two[0] = 0x03;
|
|
check(memcmp(one, two,1) > 0);
|
|
|
|
|
|
/* memchr. */
|
|
it = "memchr";
|
|
check(memchr("abcd", 'z', 4) == NULL); /* Not found. */
|
|
(void) strcpy(one, "abcd");
|
|
check(memchr(one, 'c', 4) == one+2); /* Basic test. */
|
|
check(memchr(one, 'd', 4) == one+3); /* End of string. */
|
|
check(memchr(one, 'a', 4) == one); /* Beginning. */
|
|
check(memchr(one, '\0', 5) == one+4); /* Finding NUL. */
|
|
(void) strcpy(one, "ababa");
|
|
check(memchr(one, 'b', 5) == one+1); /* Finding first. */
|
|
check(memchr(one, 'b', 0) == NULL); /* Zero count. */
|
|
check(memchr(one, 'a', 1) == one); /* Singleton case. */
|
|
(void) strcpy(one, "a\203b");
|
|
check(memchr(one, 0203, 3) == one+1); /* Unsignedness. */
|
|
|
|
/* memcpy - need not work for overlap. */
|
|
it = "memcpy";
|
|
check(memcpy(one, "abc", 4) == one); /* Returned value. */
|
|
equal(one, "abc"); /* Did the copy go right? */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) memcpy(one+1, "xyz", 2);
|
|
equal(one, "axydefgh"); /* Basic test. */
|
|
|
|
(void) strcpy(one, "abc");
|
|
(void) memcpy(one, "xyz", 0);
|
|
equal(one, "abc"); /* Zero-length copy. */
|
|
|
|
(void) strcpy(one, "hi there");
|
|
(void) strcpy(two, "foo");
|
|
(void) memcpy(two, one, 9);
|
|
equal(two, "hi there"); /* Just paranoia. */
|
|
equal(one, "hi there"); /* Stomped on source? */
|
|
#if 0
|
|
/* memmove - must work on overlap. */
|
|
it = "memmove";
|
|
check(memmove(one, "abc", 4) == one); /* Returned value. */
|
|
equal(one, "abc"); /* Did the copy go right? */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) memmove(one+1, "xyz", 2);
|
|
equal(one, "axydefgh"); /* Basic test. */
|
|
|
|
(void) strcpy(one, "abc");
|
|
(void) memmove(one, "xyz", 0);
|
|
equal(one, "abc"); /* Zero-length copy. */
|
|
|
|
(void) strcpy(one, "hi there");
|
|
(void) strcpy(two, "foo");
|
|
(void) memmove(two, one, 9);
|
|
equal(two, "hi there"); /* Just paranoia. */
|
|
equal(one, "hi there"); /* Stomped on source? */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) memmove(one+1, one, 9);
|
|
equal(one, "aabcdefgh"); /* Overlap, right-to-left. */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) memmove(one+1, one+2, 7);
|
|
equal(one, "acdefgh"); /* Overlap, left-to-right. */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) memmove(one, one, 9);
|
|
equal(one, "abcdefgh"); /* 100% overlap. */
|
|
#endif
|
|
#if 0
|
|
/* memccpy - first test like memcpy, then the search part
|
|
The SVID, the only place where memccpy is mentioned, says
|
|
overlap might fail, so we don't try it. Besides, it's hard
|
|
to see the rationale for a non-left-to-right memccpy. */
|
|
it = "memccpy";
|
|
check(memccpy(one, "abc", 'q', 4) == NULL); /* Returned value. */
|
|
equal(one, "abc"); /* Did the copy go right? */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) memccpy(one+1, "xyz", 'q', 2);
|
|
equal(one, "axydefgh"); /* Basic test. */
|
|
|
|
(void) strcpy(one, "abc");
|
|
(void) memccpy(one, "xyz", 'q', 0);
|
|
equal(one, "abc"); /* Zero-length copy. */
|
|
|
|
(void) strcpy(one, "hi there");
|
|
(void) strcpy(two, "foo");
|
|
(void) memccpy(two, one, 'q', 9);
|
|
equal(two, "hi there"); /* Just paranoia. */
|
|
equal(one, "hi there"); /* Stomped on source? */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) strcpy(two, "horsefeathers");
|
|
check(memccpy(two, one, 'f', 9) == two+6); /* Returned value. */
|
|
equal(one, "abcdefgh"); /* Source intact? */
|
|
equal(two, "abcdefeathers"); /* Copy correct? */
|
|
|
|
(void) strcpy(one, "abcd");
|
|
(void) strcpy(two, "bumblebee");
|
|
check(memccpy(two, one, 'a', 4) == two+1); /* First char. */
|
|
equal(two, "aumblebee");
|
|
check(memccpy(two, one, 'd', 4) == two+4); /* Last char. */
|
|
equal(two, "abcdlebee");
|
|
(void) strcpy(one, "xyz");
|
|
check(memccpy(two, one, 'x', 1) == two+1); /* Singleton. */
|
|
equal(two, "xbcdlebee");
|
|
#endif
|
|
/* memset. */
|
|
it = "memset";
|
|
(void) strcpy(one, "abcdefgh");
|
|
check(memset(one+1, 'x', 3) == one+1); /* Return value. */
|
|
equal(one, "axxxefgh"); /* Basic test. */
|
|
|
|
(void) memset(one+2, 'y', 0);
|
|
equal(one, "axxxefgh"); /* Zero-length set. */
|
|
|
|
(void) memset(one+5, 0, 1);
|
|
equal(one, "axxxe"); /* Zero fill. */
|
|
equal(one+6, "gh"); /* _AND the leftover. */
|
|
|
|
(void) memset(one+2, 010045, 1);
|
|
equal(one, "ax\045xe"); /* Unsigned char convert. */
|
|
|
|
/* bcopy - much like memcpy.
|
|
Berklix manual is silent about overlap, so don't test it. */
|
|
it = "bcopy";
|
|
(void) bcopy("abc", one, 4);
|
|
equal(one, "abc"); /* Simple copy. */
|
|
|
|
(void) strcpy(one, "abcdefgh");
|
|
(void) bcopy("xyz", one+1, 2);
|
|
equal(one, "axydefgh"); /* Basic test. */
|
|
|
|
(void) strcpy(one, "abc");
|
|
(void) bcopy("xyz", one, 0);
|
|
equal(one, "abc"); /* Zero-length copy. */
|
|
|
|
(void) strcpy(one, "hi there");
|
|
(void) strcpy(two, "foo");
|
|
(void) bcopy(one, two, 9);
|
|
equal(two, "hi there"); /* Just paranoia. */
|
|
equal(one, "hi there"); /* Stomped on source? */
|
|
|
|
/* bzero. */
|
|
it = "bzero";
|
|
(void) strcpy(one, "abcdef");
|
|
bzero(one+2, 2);
|
|
equal(one, "ab"); /* Basic test. */
|
|
equal(one+3, "");
|
|
equal(one+4, "ef");
|
|
|
|
(void) strcpy(one, "abcdef");
|
|
bzero(one+2, 0);
|
|
equal(one, "abcdef"); /* Zero-length copy. */
|
|
|
|
/* bcmp - somewhat like memcmp. */
|
|
it = "bcmp";
|
|
check(bcmp("a", "a", 1) == 0); /* Identity. */
|
|
check(bcmp("abc", "abc", 3) == 0); /* Multicharacter. */
|
|
check(bcmp("abcd", "abce", 4) != 0); /* Honestly unequal. */
|
|
check(bcmp("abce", "abcd",4));
|
|
check(bcmp("alph", "beta", 4) != 0);
|
|
check(bcmp("abce", "abcd", 3) == 0); /* Count limited. */
|
|
check(bcmp("abc", "def", 0) == 0); /* Zero count. */
|
|
|
|
if (errors) abort();
|
|
printf("ok\n");
|
|
|
|
#if 0 /* strerror - VERY system-dependent. */
|
|
{
|
|
extern CONST unsigned int _sys_nerr;
|
|
extern CONST char *CONST _sys_errlist[];
|
|
int f;
|
|
it = "strerror";
|
|
f = open("/", O_WRONLY); /* Should always fail. */
|
|
check(f < 0 && errno > 0 && errno < _sys_nerr);
|
|
equal(strerror(errno), _sys_errlist[errno]);
|
|
}
|
|
#endif
|
|
}
|
|
|