1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-08-01 03:47:23 +03:00

Migrate from astyle to clang-format (#8464)

This commit is contained in:
Maxim Prokhorov
2022-02-20 19:23:33 +03:00
committed by Max Prokhorov
parent 46190b61f1
commit 19b7a29720
241 changed files with 15925 additions and 16197 deletions

View File

@ -7,29 +7,30 @@ namespace bs
class ArduinoIOHelper
{
public:
ArduinoIOHelper(Stream& stream) : m_stream(stream)
{
}
ArduinoIOHelper(Stream& stream) : m_stream(stream) { }
size_t printf(const char *format, ...)
size_t printf(const char* format, ...)
{
va_list arg;
va_start(arg, format);
char temp[128];
char* buffer = temp;
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
char temp[128];
char* buffer = temp;
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
va_end(arg);
if (len > sizeof(temp) - 1) {
if (len > sizeof(temp) - 1)
{
buffer = new char[len + 1];
if (!buffer) {
if (!buffer)
{
return 0;
}
va_start(arg, format);
ets_vsnprintf(buffer, len + 1, format, arg);
va_end(arg);
}
len = m_stream.write((const uint8_t*) buffer, len);
if (buffer != temp) {
len = m_stream.write((const uint8_t*)buffer, len);
if (buffer != temp)
{
delete[] buffer;
}
return len;
@ -40,16 +41,20 @@ public:
size_t len = 0;
// Can't use Stream::readBytesUntil here because it can't tell the
// difference between timing out and receiving the terminator.
while (len < dest_size - 1) {
while (len < dest_size - 1)
{
int c = m_stream.read();
if (c < 0) {
if (c < 0)
{
delay(1);
continue;
}
if (c == '\r') {
if (c == '\r')
{
continue;
}
if (c == '\n') {
if (c == '\n')
{
dest[len] = 0;
break;
}
@ -64,10 +69,11 @@ protected:
typedef ArduinoIOHelper IOHelper;
inline void fatal() {
inline void fatal()
{
ESP.restart();
}
} // namespace bs
} // namespace bs
#endif //BS_ARDUINO_H
#endif // BS_ARDUINO_H

View File

@ -18,134 +18,162 @@ namespace protocol
#define SS_FLAG_ESCAPE 0x8
typedef enum {
/* parsing the space between arguments */
SS_SPACE = 0x0,
/* parsing an argument which isn't quoted */
SS_ARG = 0x1,
/* parsing a quoted argument */
SS_QUOTED_ARG = 0x2,
/* parsing an escape sequence within unquoted argument */
SS_ARG_ESCAPED = SS_ARG | SS_FLAG_ESCAPE,
/* parsing an escape sequence within a quoted argument */
SS_QUOTED_ARG_ESCAPED = SS_QUOTED_ARG | SS_FLAG_ESCAPE,
} split_state_t;
typedef enum
{
/* parsing the space between arguments */
SS_SPACE = 0x0,
/* parsing an argument which isn't quoted */
SS_ARG = 0x1,
/* parsing a quoted argument */
SS_QUOTED_ARG = 0x2,
/* parsing an escape sequence within unquoted argument */
SS_ARG_ESCAPED = SS_ARG | SS_FLAG_ESCAPE,
/* parsing an escape sequence within a quoted argument */
SS_QUOTED_ARG_ESCAPED = SS_QUOTED_ARG | SS_FLAG_ESCAPE,
} split_state_t;
/* helper macro, called when done with an argument */
#define END_ARG() do { \
char_out = 0; \
argv[argc++] = next_arg_start; \
state = SS_SPACE; \
} while(0);
#define END_ARG() \
do \
{ \
char_out = 0; \
argv[argc++] = next_arg_start; \
state = SS_SPACE; \
} while (0);
/**
* @brief Split command line into arguments in place
*
* - This function finds whitespace-separated arguments in the given input line.
*
* 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
*
* - Argument which include spaces may be surrounded with quotes. In this case
* spaces are preserved and quotes are stripped.
*
* 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
*
* - Escape sequences may be used to produce backslash, double quote, and space:
*
* 'a\ b\\c\"' -> [ 'a b\c"' ]
*
* Pointers to at most argv_size - 1 arguments are returned in argv array.
* The pointer after the last one (i.e. argv[argc]) is set to NULL.
*
* @param line pointer to buffer to parse; it is modified in place
* @param argv array where the pointers to arguments are written
* @param argv_size number of elements in argv_array (max. number of arguments will be argv_size
* - 1)
* @return number of arguments found (argc)
*/
inline size_t split_args(char* line, char** argv, size_t argv_size)
{
const int QUOTE = '"';
const int ESCAPE = '\\';
const int SPACE = ' ';
split_state_t state = SS_SPACE;
size_t argc = 0;
char* next_arg_start = line;
char* out_ptr = line;
for (char* in_ptr = line; argc < argv_size - 1; ++in_ptr)
{
int char_in = (unsigned char)*in_ptr;
if (char_in == 0)
{
break;
}
int char_out = -1;
/**
* @brief Split command line into arguments in place
*
* - This function finds whitespace-separated arguments in the given input line.
*
* 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
*
* - Argument which include spaces may be surrounded with quotes. In this case
* spaces are preserved and quotes are stripped.
*
* 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
*
* - Escape sequences may be used to produce backslash, double quote, and space:
*
* 'a\ b\\c\"' -> [ 'a b\c"' ]
*
* Pointers to at most argv_size - 1 arguments are returned in argv array.
* The pointer after the last one (i.e. argv[argc]) is set to NULL.
*
* @param line pointer to buffer to parse; it is modified in place
* @param argv array where the pointers to arguments are written
* @param argv_size number of elements in argv_array (max. number of arguments will be argv_size - 1)
* @return number of arguments found (argc)
*/
inline size_t split_args(char *line, char **argv, size_t argv_size)
{
const int QUOTE = '"';
const int ESCAPE = '\\';
const int SPACE = ' ';
split_state_t state = SS_SPACE;
size_t argc = 0;
char *next_arg_start = line;
char *out_ptr = line;
for (char *in_ptr = line; argc < argv_size - 1; ++in_ptr) {
int char_in = (unsigned char) *in_ptr;
if (char_in == 0) {
break;
switch (state)
{
case SS_SPACE:
if (char_in == SPACE)
{
/* skip space */
}
else if (char_in == QUOTE)
{
next_arg_start = out_ptr;
state = SS_QUOTED_ARG;
}
else if (char_in == ESCAPE)
{
next_arg_start = out_ptr;
state = SS_ARG_ESCAPED;
}
else
{
next_arg_start = out_ptr;
state = SS_ARG;
char_out = char_in;
}
break;
case SS_QUOTED_ARG:
if (char_in == QUOTE)
{
END_ARG();
}
else if (char_in == ESCAPE)
{
state = SS_QUOTED_ARG_ESCAPED;
}
else
{
char_out = char_in;
}
break;
case SS_ARG_ESCAPED:
case SS_QUOTED_ARG_ESCAPED:
if (char_in == ESCAPE || char_in == QUOTE || char_in == SPACE)
{
char_out = char_in;
}
else
{
/* unrecognized escape character, skip */
}
state = (split_state_t)(state & (~SS_FLAG_ESCAPE));
break;
case SS_ARG:
if (char_in == SPACE)
{
END_ARG();
}
else if (char_in == ESCAPE)
{
state = SS_ARG_ESCAPED;
}
else
{
char_out = char_in;
}
break;
}
/* need to output anything? */
if (char_out >= 0)
{
*out_ptr = char_out;
++out_ptr;
}
}
int char_out = -1;
switch (state) {
case SS_SPACE:
if (char_in == SPACE) {
/* skip space */
} else if (char_in == QUOTE) {
next_arg_start = out_ptr;
state = SS_QUOTED_ARG;
} else if (char_in == ESCAPE) {
next_arg_start = out_ptr;
state = SS_ARG_ESCAPED;
} else {
next_arg_start = out_ptr;
state = SS_ARG;
char_out = char_in;
}
break;
case SS_QUOTED_ARG:
if (char_in == QUOTE) {
END_ARG();
} else if (char_in == ESCAPE) {
state = SS_QUOTED_ARG_ESCAPED;
} else {
char_out = char_in;
}
break;
case SS_ARG_ESCAPED:
case SS_QUOTED_ARG_ESCAPED:
if (char_in == ESCAPE || char_in == QUOTE || char_in == SPACE) {
char_out = char_in;
} else {
/* unrecognized escape character, skip */
}
state = (split_state_t) (state & (~SS_FLAG_ESCAPE));
break;
case SS_ARG:
if (char_in == SPACE) {
END_ARG();
} else if (char_in == ESCAPE) {
state = SS_ARG_ESCAPED;
} else {
char_out = char_in;
}
break;
}
/* need to output anything? */
if (char_out >= 0) {
*out_ptr = char_out;
++out_ptr;
/* make sure the final argument is terminated */
*out_ptr = 0;
/* finalize the last argument */
if (state != SS_SPACE && argc < argv_size - 1)
{
argv[argc++] = next_arg_start;
}
/* add a NULL at the end of argv */
argv[argc] = NULL;
return argc;
}
/* make sure the final argument is terminated */
*out_ptr = 0;
/* finalize the last argument */
if (state != SS_SPACE && argc < argv_size - 1) {
argv[argc++] = next_arg_start;
}
/* add a NULL at the end of argv */
argv[argc] = NULL;
return argc;
}
} // namespace protocol
} // namespace bs
} // namespace bs
} // namespace protocol
#endif //BS_ARGS_H
#endif // BS_ARGS_H

View File

@ -11,108 +11,120 @@ namespace bs
{
namespace protocol
{
template<typename IO>
void output_test_start(IO& io, const char* file, size_t line, const char* name, const char* desc)
{
io.printf(BS_LINE_PREFIX "start file=\"%s\" line=%d name=\"%s\" desc=\"%s\"\n", file, line, name, desc);
}
template<typename IO>
void output_check_failure(IO& io, size_t line)
{
io.printf(BS_LINE_PREFIX "check_failure line=%d\n", line);
}
template<typename IO>
void output_test_end(IO& io, bool success, size_t checks, size_t failed_checks, size_t line=0)
{
io.printf(BS_LINE_PREFIX "end line=%d result=%d checks=%d failed_checks=%d\n", line, success, checks, failed_checks);
}
template<typename IO>
void output_menu_begin(IO& io)
{
io.printf(BS_LINE_PREFIX "menu_begin\n");
}
template<typename IO>
void output_menu_item(IO& io, int index, const char* name, const char* desc)
{
io.printf(BS_LINE_PREFIX "item id=%d name=\"%s\" desc=\"%s\"\n", index, name, desc);
}
template<typename IO>
void output_menu_end(IO& io)
{
io.printf(BS_LINE_PREFIX "menu_end\n");
}
template<typename IO>
void output_setenv_result(IO& io, const char* key, const char* value)
{
io.printf(BS_LINE_PREFIX "setenv key=\"%s\" value=\"%s\"\n", key, value);
}
template<typename IO>
void output_getenv_result(IO& io, const char* key, const char* value)
{
(void) key;
io.printf(BS_LINE_PREFIX "getenv value=\"%s\"\n", value);
}
template<typename IO>
void output_pretest_result(IO& io, bool res)
{
io.printf(BS_LINE_PREFIX "pretest result=%d\n", res?1:0);
}
template<typename IO>
bool input_handle(IO& io, char* line_buf, size_t line_buf_size, int& test_num)
{
int cb_read = io.read_line(line_buf, line_buf_size);
if (cb_read == 0 || line_buf[0] == '\n') {
return false;
template<typename IO>
void output_test_start(IO& io, const char* file, size_t line, const char* name,
const char* desc)
{
io.printf(BS_LINE_PREFIX "start file=\"%s\" line=%d name=\"%s\" desc=\"%s\"\n", file, line,
name, desc);
}
char* argv[4];
size_t argc = split_args(line_buf, argv, sizeof(argv)/sizeof(argv[0]));
if (argc == 0) {
return false;
template<typename IO>
void output_check_failure(IO& io, size_t line)
{
io.printf(BS_LINE_PREFIX "check_failure line=%d\n", line);
}
if (strcmp(argv[0], "setenv") == 0) {
if (argc != 3) {
template<typename IO>
void output_test_end(IO& io, bool success, size_t checks, size_t failed_checks, size_t line = 0)
{
io.printf(BS_LINE_PREFIX "end line=%d result=%d checks=%d failed_checks=%d\n", line,
success, checks, failed_checks);
}
template<typename IO>
void output_menu_begin(IO& io)
{
io.printf(BS_LINE_PREFIX "menu_begin\n");
}
template<typename IO>
void output_menu_item(IO& io, int index, const char* name, const char* desc)
{
io.printf(BS_LINE_PREFIX "item id=%d name=\"%s\" desc=\"%s\"\n", index, name, desc);
}
template<typename IO>
void output_menu_end(IO& io)
{
io.printf(BS_LINE_PREFIX "menu_end\n");
}
template<typename IO>
void output_setenv_result(IO& io, const char* key, const char* value)
{
io.printf(BS_LINE_PREFIX "setenv key=\"%s\" value=\"%s\"\n", key, value);
}
template<typename IO>
void output_getenv_result(IO& io, const char* key, const char* value)
{
(void)key;
io.printf(BS_LINE_PREFIX "getenv value=\"%s\"\n", value);
}
template<typename IO>
void output_pretest_result(IO& io, bool res)
{
io.printf(BS_LINE_PREFIX "pretest result=%d\n", res ? 1 : 0);
}
template<typename IO>
bool input_handle(IO& io, char* line_buf, size_t line_buf_size, int& test_num)
{
int cb_read = io.read_line(line_buf, line_buf_size);
if (cb_read == 0 || line_buf[0] == '\n')
{
return false;
}
setenv(argv[1], argv[2], 1);
output_setenv_result(io, argv[1], argv[2]);
test_num = -1;
return false; /* we didn't get the test number yet, so return false */
}
if (strcmp(argv[0], "getenv") == 0) {
if (argc != 2) {
char* argv[4];
size_t argc = split_args(line_buf, argv, sizeof(argv) / sizeof(argv[0]));
if (argc == 0)
{
return false;
}
const char* value = getenv(argv[1]);
output_getenv_result(io, argv[1], (value != NULL) ? value : "");
return false;
}
if (strcmp(argv[0], "pretest") == 0) {
if (argc != 1) {
if (strcmp(argv[0], "setenv") == 0)
{
if (argc != 3)
{
return false;
}
setenv(argv[1], argv[2], 1);
output_setenv_result(io, argv[1], argv[2]);
test_num = -1;
return false; /* we didn't get the test number yet, so return false */
}
if (strcmp(argv[0], "getenv") == 0)
{
if (argc != 2)
{
return false;
}
const char* value = getenv(argv[1]);
output_getenv_result(io, argv[1], (value != NULL) ? value : "");
return false;
}
bool res = ::pretest();
output_pretest_result(io, res);
return false;
if (strcmp(argv[0], "pretest") == 0)
{
if (argc != 1)
{
return false;
}
bool res = ::pretest();
output_pretest_result(io, res);
return false;
}
/* not one of the commands, try to parse as test number */
char* endptr;
test_num = (int)strtol(argv[0], &endptr, 10);
if (endptr != argv[0] + strlen(argv[0]))
{
return false;
}
return true;
}
/* not one of the commands, try to parse as test number */
char* endptr;
test_num = (int) strtol(argv[0], &endptr, 10);
if (endptr != argv[0] + strlen(argv[0])) {
return false;
}
return true;
}
} // ::protocol
} // ::bs
} // namespace protocol
} // namespace bs
#endif //BS_PROTOCOL_H
#endif // BS_PROTOCOL_H

View File

@ -9,11 +9,9 @@ namespace bs
class StdIOHelper
{
public:
StdIOHelper()
{
}
StdIOHelper() { }
size_t printf(const char *format, ...)
size_t printf(const char* format, ...)
{
va_list arg;
va_start(arg, format);
@ -25,11 +23,13 @@ public:
size_t read_line(char* dest, size_t dest_size)
{
char* res = fgets(dest, dest_size, stdin);
if (res == NULL) {
if (res == NULL)
{
return 0;
}
size_t len = strlen(dest);
if (dest[len - 1] == '\n') {
if (dest[len - 1] == '\n')
{
dest[len - 1] = 0;
len--;
}
@ -39,10 +39,11 @@ public:
typedef StdIOHelper IOHelper;
inline void fatal() {
inline void fatal()
{
throw std::runtime_error("fatal error");
}
} // namespace bs
} // namespace bs
#endif //BS_STDIO_H
#endif // BS_STDIO_H

View File

@ -20,15 +20,18 @@
namespace bs
{
typedef void(*test_case_func_t)();
typedef void (*test_case_func_t)();
class TestCase
{
public:
TestCase(TestCase* prev, test_case_func_t func, const char* file, size_t line, const char* name, const char* desc)
: m_func(func), m_file(file), m_line(line), m_name(name), m_desc(desc)
TestCase(TestCase* prev, test_case_func_t func, const char* file, size_t line, const char* name,
const char* desc) :
m_func(func),
m_file(file), m_line(line), m_name(name), m_desc(desc)
{
if (prev) {
if (prev)
{
prev->m_next = this;
}
}
@ -60,36 +63,40 @@ public:
const char* desc() const
{
return (m_desc)?m_desc:"";
return (m_desc) ? m_desc : "";
}
protected:
TestCase* m_next = nullptr;
TestCase* m_next = nullptr;
test_case_func_t m_func;
const char* m_file;
size_t m_line;
const char* m_name;
const char* m_desc;
const char* m_file;
size_t m_line;
const char* m_name;
const char* m_desc;
};
struct Registry {
void add(test_case_func_t func, const char* file, size_t line, const char* name, const char* desc)
struct Registry
{
void add(test_case_func_t func, const char* file, size_t line, const char* name,
const char* desc)
{
TestCase* tc = new TestCase(m_last, func, file, line, name, desc);
if (!m_first) {
if (!m_first)
{
m_first = tc;
}
m_last = tc;
}
TestCase* m_first = nullptr;
TestCase* m_last = nullptr;
TestCase* m_last = nullptr;
};
struct Env {
std::function<void(void)> m_check_pass;
struct Env
{
std::function<void(void)> m_check_pass;
std::function<void(size_t)> m_check_fail;
std::function<void(size_t)> m_fail;
Registry m_registry;
Registry m_registry;
};
extern Env g_env;
@ -98,25 +105,27 @@ template<typename IO>
class Runner
{
typedef Runner<IO> Tself;
public:
Runner(IO& io) : m_io(io)
{
g_env.m_check_pass = std::bind(&Tself::check_pass, this);
g_env.m_check_fail = std::bind(&Tself::check_fail, this, std::placeholders::_1);
g_env.m_fail = std::bind(&Tself::fail, this, std::placeholders::_1);
g_env.m_fail = std::bind(&Tself::fail, this, std::placeholders::_1);
}
~Runner()
{
g_env.m_check_pass = 0;
g_env.m_check_fail = 0;
g_env.m_fail = 0;
g_env.m_fail = 0;
}
void run()
{
do {
} while(do_menu());
do
{
} while (do_menu());
}
void check_pass()
@ -132,7 +141,8 @@ public:
void fail(size_t line)
{
protocol::output_test_end(m_io, false, m_check_pass_count + m_check_fail_count, m_check_fail_count, line);
protocol::output_test_end(m_io, false, m_check_pass_count + m_check_fail_count,
m_check_fail_count, line);
bs::fatal();
}
@ -141,22 +151,28 @@ protected:
{
protocol::output_menu_begin(m_io);
int id = 1;
for (TestCase* tc = g_env.m_registry.m_first; tc; tc = tc->next(), ++id) {
for (TestCase* tc = g_env.m_registry.m_first; tc; tc = tc->next(), ++id)
{
protocol::output_menu_item(m_io, id, tc->name(), tc->desc());
}
protocol::output_menu_end(m_io);
while(true) {
int id;
while (true)
{
int id;
char line_buf[BS_LINE_BUF_SIZE];
if (!protocol::input_handle(m_io, line_buf, sizeof(line_buf), id)) {
if (!protocol::input_handle(m_io, line_buf, sizeof(line_buf), id))
{
continue;
}
if (id < 0) {
if (id < 0)
{
return true;
}
TestCase* tc = g_env.m_registry.m_first;
for (int i = 0; i != id - 1 && tc; ++i, tc = tc->next());
if (!tc) {
for (int i = 0; i != id - 1 && tc; ++i, tc = tc->next())
;
if (!tc)
{
bs::fatal();
}
m_check_pass_count = 0;
@ -164,12 +180,13 @@ protected:
protocol::output_test_start(m_io, tc->file(), tc->line(), tc->name(), tc->desc());
tc->run();
bool success = m_check_fail_count == 0;
protocol::output_test_end(m_io, success, m_check_pass_count + m_check_fail_count, m_check_fail_count);
protocol::output_test_end(m_io, success, m_check_pass_count + m_check_fail_count,
m_check_fail_count);
}
}
protected:
IO& m_io;
IO& m_io;
size_t m_check_pass_count;
size_t m_check_fail_count;
};
@ -177,7 +194,8 @@ protected:
class AutoReg
{
public:
AutoReg(test_case_func_t func, const char* file, size_t line, const char* name, const char* desc = nullptr)
AutoReg(test_case_func_t func, const char* file, size_t line, const char* name,
const char* desc = nullptr)
{
g_env.m_registry.add(func, file, line, name, desc);
}
@ -185,39 +203,59 @@ public:
inline void check(bool condition, size_t line)
{
if (!condition) {
if (!condition)
{
g_env.m_check_fail(line);
} else {
}
else
{
g_env.m_check_pass();
}
}
inline void require(bool condition, size_t line)
{
if (!condition) {
if (!condition)
{
g_env.m_check_fail(line);
g_env.m_fail(line);
} else {
}
else
{
g_env.m_check_pass();
}
}
} // ::bs
} // namespace bs
#define BS_NAME_LINE2( name, line ) name##line
#define BS_NAME_LINE( name, line ) BS_NAME_LINE2( name, line )
#define BS_UNIQUE_NAME( name ) BS_NAME_LINE( name, __LINE__ )
#define BS_NAME_LINE2(name, line) name##line
#define BS_NAME_LINE(name, line) BS_NAME_LINE2(name, line)
#define BS_UNIQUE_NAME(name) BS_NAME_LINE(name, __LINE__)
#define TEST_CASE( ... ) \
static void BS_UNIQUE_NAME( TEST_FUNC__ )(); \
namespace{ bs::AutoReg BS_UNIQUE_NAME( test_autoreg__ )( &BS_UNIQUE_NAME( TEST_FUNC__ ), __FILE__, __LINE__, __VA_ARGS__ ); }\
static void BS_UNIQUE_NAME( TEST_FUNC__ )()
#define TEST_CASE(...) \
static void BS_UNIQUE_NAME(TEST_FUNC__)(); \
namespace \
{ \
bs::AutoReg BS_UNIQUE_NAME(test_autoreg__)(&BS_UNIQUE_NAME(TEST_FUNC__), __FILE__, \
__LINE__, __VA_ARGS__); \
} \
static void BS_UNIQUE_NAME(TEST_FUNC__)()
#define CHECK(condition) bs::check((condition), __LINE__)
#define REQUIRE(condition) bs::require((condition), __LINE__)
#define FAIL() bs::g_env.m_fail(__LINE__)
#define BS_ENV_DECLARE() namespace bs { Env g_env; }
#define BS_RUN(...) do { bs::IOHelper helper = bs::IOHelper(__VA_ARGS__); bs::Runner<bs::IOHelper> runner(helper); runner.run(); } while(0);
#define BS_ENV_DECLARE() \
namespace bs \
{ \
Env g_env; \
}
#define BS_RUN(...) \
do \
{ \
bs::IOHelper helper = bs::IOHelper(__VA_ARGS__); \
bs::Runner<bs::IOHelper> runner(helper); \
runner.run(); \
} while (0);
#endif //BSTEST_H
#endif // BSTEST_H

View File

@ -3,21 +3,23 @@
BS_ENV_DECLARE();
int main()
{
while(true) {
try{
while (true)
{
try
{
BS_RUN();
return 0;
}catch(...) {
}
catch (...)
{
printf("Exception\n\n");
}
}
return 1;
}
TEST_CASE("this test runs successfully", "[bluesmoke]")
{
CHECK(1 + 1 == 2);
@ -38,17 +40,12 @@ TEST_CASE("another test which fails and crashes", "[bluesmoke][fail]")
REQUIRE(false);
}
TEST_CASE("third test which should be skipped", "[.]")
{
FAIL();
}
TEST_CASE("this test also runs successfully", "[bluesmoke]")
{
}
TEST_CASE("this test also runs successfully", "[bluesmoke]") { }
TEST_CASE("environment variables can be set and read from python", "[bluesmoke]")
{
@ -57,4 +54,3 @@ TEST_CASE("environment variables can be set and read from python", "[bluesmoke]"
CHECK(strcmp(res, "42") == 0);
setenv("VAR_FROM_TEST", "24", 1);
}

View File

@ -3,8 +3,6 @@
#include <stdlib.h>
#include <errno.h>
#define memcmp memcmp_P
#define memcpy memcpy_P
#define memmem memmem_P
@ -18,416 +16,404 @@
#define strcmp strcmp_P
#define strncmp strncmp_P
_CONST char *it = "<UNSET>"; /* Routine name for message routines. */
static int errors = 0;
_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 )
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;
}
}
// newfunc(it);
// line(l);
/* 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);
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 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. */
/* 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(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(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. */
(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. */
/* 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, "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, "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");
(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. */
/* 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, "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, "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, "");
(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)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", 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. */
(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? */
/* 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", 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", 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", 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, "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)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)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? */
(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. */
/* 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. */
/* 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. */
/* 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. */
/* 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. */
/* 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. */
/* 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. */
/* 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. */
/* 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. */
/* 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");
/* 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. */
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. */
/* memcmp should test strings as unsigned */
one[0] = 0xfe;
two[0] = 0x03;
check(memcmp(one, two, 1) > 0);
/* memcpy - need not work for overlap. */
it = "memcpy";
check(memcpy(one, "abc", 4) == one); /* Returned value. */
equal(one, "abc"); /* Did the copy go right? */
/* 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. */
(void) strcpy(one, "abcdefgh");
(void) memcpy(one+1, "xyz", 2);
equal(one, "axydefgh"); /* Basic test. */
/* 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, "abc");
(void) memcpy(one, "xyz", 0);
equal(one, "abc"); /* Zero-length copy. */
(void)strcpy(one, "abcdefgh");
(void)memcpy(one + 1, "xyz", 2);
equal(one, "axydefgh"); /* Basic test. */
(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? */
(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";
@ -499,68 +485,69 @@ void libm_test_string()
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. */
/* 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 + 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 + 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. */
(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. */
/* 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, "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, "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? */
(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");
/* 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. */
(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. */
/* 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 (errors)
abort();
printf("ok\n");
#if 0 /* strerror - VERY system-dependent. */
#if 0 /* strerror - VERY system-dependent. */
{
extern CONST unsigned int _sys_nerr;
extern CONST char *CONST _sys_errlist[];
@ -572,4 +559,3 @@ void libm_test_string()
}
#endif
}

View File

@ -31,7 +31,6 @@
#include <stdio.h>
#include <stdarg.h>
#define memcmp memcmp_P
#define memcpy memcpy_P
#define memmem memmem_P
@ -69,106 +68,105 @@
#define TOO_MANY_ERRORS 11
static int errors = 0;
static void
print_error (char const* msg, ...)
{
errors++;
if (errors == TOO_MANY_ERRORS)
static void print_error(char const* msg, ...)
{
errors++;
if (errors == TOO_MANY_ERRORS)
{
fprintf (stderr, "Too many errors.\n");
fprintf(stderr, "Too many errors.\n");
}
else if (errors < TOO_MANY_ERRORS)
else if (errors < TOO_MANY_ERRORS)
{
va_list ap;
va_start (ap, msg);
vfprintf (stderr, msg, ap);
va_end (ap);
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
}
else
else
{
/* Further errors omitted. */
/* Further errors omitted. */
}
}
extern int rand_seed;
void memcpy_main(void)
void memcpy_main(void)
{
/* Allocate buffers to read and write from. */
char src[BUFF_SIZE], dest[BUFF_SIZE], backup_src[BUFF_SIZE];
/* Allocate buffers to read and write from. */
char src[BUFF_SIZE], dest[BUFF_SIZE], backup_src[BUFF_SIZE];
/* Fill the source buffer with non-null values, reproducible random data. */
srand (rand_seed);
int i, j;
unsigned sa;
unsigned da;
unsigned n;
for (i = 0; i < BUFF_SIZE; i++)
/* Fill the source buffer with non-null values, reproducible random data. */
srand(rand_seed);
int i, j;
unsigned sa;
unsigned da;
unsigned n;
for (i = 0; i < BUFF_SIZE; i++)
{
src[i] = (char)rand () | 1;
backup_src[i] = src[i];
src[i] = (char)rand() | 1;
backup_src[i] = src[i];
}
/* Make calls to memcpy with block sizes ranging between 1 and
MAX_BLOCK_SIZE bytes, aligned and misaligned source and destination. */
for (sa = 0; sa <= MAX_OFFSET; sa++)
for (da = 0; da <= MAX_OFFSET; da++)
for (n = 1; n <= MAX_BLOCK_SIZE; n++)
{
//printf (".");
/* Zero dest so we can check it properly after the copying. */
for (j = 0; j < BUFF_SIZE; j++)
dest[j] = 0;
void *ret = memcpy (dest + START_COPY + da, src + sa, n);
/* Check return value. */
if (ret != (dest + START_COPY + da))
print_error ("\nFailed: wrong return value in memcpy of %u bytes "
"with src_align %u and dst_align %u. "
"Return value and dest should be the same"
"(ret is %p, dest is %p)\n",
n, sa, da, ret, dest + START_COPY + da);
/* Check that content of the destination buffer
is the same as the source buffer, and
memory outside destination buffer is not modified. */
for (j = 0; j < BUFF_SIZE; j++)
if ((unsigned)j < START_COPY + da)
{
if (dest[j] != 0)
print_error ("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u before the start of dest is not 0.\n",
n, sa, da, START_COPY - j);
}
else if ((unsigned)j < START_COPY + da + n)
{
i = j - START_COPY - da;
if (dest[j] != (src + sa)[i])
print_error ("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u in dest and src are not the same.\n",
n, sa, da, i);
}
else if (dest[j] != 0)
{
print_error ("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u after the end of dest is not 0.\n",
n, sa, da, j - START_COPY - da - n);
}
/* Make calls to memcpy with block sizes ranging between 1 and
MAX_BLOCK_SIZE bytes, aligned and misaligned source and destination. */
for (sa = 0; sa <= MAX_OFFSET; sa++)
for (da = 0; da <= MAX_OFFSET; da++)
for (n = 1; n <= MAX_BLOCK_SIZE; n++)
{
// printf (".");
/* Zero dest so we can check it properly after the copying. */
for (j = 0; j < BUFF_SIZE; j++)
dest[j] = 0;
/* Check src is not modified. */
for (j = 0; j < BUFF_SIZE; j++)
if (src[i] != backup_src[i])
print_error ("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u of src is modified.\n",
n, sa, da, j);
}
void* ret = memcpy(dest + START_COPY + da, src + sa, n);
if (errors != 0)
abort ();
/* Check return value. */
if (ret != (dest + START_COPY + da))
print_error("\nFailed: wrong return value in memcpy of %u bytes "
"with src_align %u and dst_align %u. "
"Return value and dest should be the same"
"(ret is %p, dest is %p)\n",
n, sa, da, ret, dest + START_COPY + da);
printf("ok\n");
/* Check that content of the destination buffer
is the same as the source buffer, and
memory outside destination buffer is not modified. */
for (j = 0; j < BUFF_SIZE; j++)
if ((unsigned)j < START_COPY + da)
{
if (dest[j] != 0)
print_error("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u before the start of dest is not 0.\n",
n, sa, da, START_COPY - j);
}
else if ((unsigned)j < START_COPY + da + n)
{
i = j - START_COPY - da;
if (dest[j] != (src + sa)[i])
print_error("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u in dest and src are not the same.\n",
n, sa, da, i);
}
else if (dest[j] != 0)
{
print_error("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u after the end of dest is not 0.\n",
n, sa, da, j - START_COPY - da - n);
}
/* Check src is not modified. */
for (j = 0; j < BUFF_SIZE; j++)
if (src[i] != backup_src[i])
print_error("\nFailed: after memcpy of %u bytes "
"with src_align %u and dst_align %u, "
"byte %u of src is modified.\n",
n, sa, da, j);
}
if (errors != 0)
abort();
printf("ok\n");
}

View File

@ -55,140 +55,132 @@
#define TOO_MANY_ERRORS 11
int errors = 0;
#define DEBUGP \
if (errors == TOO_MANY_ERRORS) \
printf ("Further errors omitted\n"); \
else if (errors < TOO_MANY_ERRORS) \
printf
#define DEBUGP \
if (errors == TOO_MANY_ERRORS) \
printf("Further errors omitted\n"); \
else if (errors < TOO_MANY_ERRORS) \
printf
/* A safe target-independent memmove. */
void
mymemmove (unsigned char *dest, unsigned char *src, size_t n)
void mymemmove(unsigned char* dest, unsigned char* src, size_t n)
{
if ((src <= dest && src + n <= dest)
|| src >= dest)
while (n-- > 0)
*dest++ = *src++;
else
if ((src <= dest && src + n <= dest) || src >= dest)
while (n-- > 0)
*dest++ = *src++;
else
{
dest += n;
src += n;
while (n-- > 0)
*--dest = *--src;
dest += n;
src += n;
while (n-- > 0)
*--dest = *--src;
}
}
/* It's either the noinline attribute or forcing the test framework to
pass -fno-builtin-memmove. */
void
xmemmove (unsigned char *dest, unsigned char *src, size_t n)
__attribute__ ((__noinline__));
void xmemmove(unsigned char* dest, unsigned char* src, size_t n) __attribute__((__noinline__));
void
xmemmove (unsigned char *dest, unsigned char *src, size_t n)
void xmemmove(unsigned char* dest, unsigned char* src, size_t n)
{
void *retp;
retp = memmove (dest, src, n);
void* retp;
retp = memmove(dest, src, n);
if (retp != dest)
if (retp != dest)
{
errors++;
DEBUGP ("memmove of n bytes returned %p instead of dest=%p\n",
retp, dest);
errors++;
DEBUGP("memmove of n bytes returned %p instead of dest=%p\n", retp, dest);
}
}
/* Fill the array with something we can associate with a position, but
not exactly the same as the position index. */
void
fill (unsigned char dest[MAX*3])
void fill(unsigned char dest[MAX * 3])
{
size_t i;
for (i = 0; i < MAX*3; i++)
dest[i] = (10 + i) % MAX;
size_t i;
for (i = 0; i < MAX * 3; i++)
dest[i] = (10 + i) % MAX;
}
void memmove_main(void)
{
size_t i;
int errors = 0;
size_t i;
int errors = 0;
/* Leave some room before and after the area tested, so we can detect
overwrites of up to N bytes, N being the amount tested. If you
want to test using valgrind, make these malloced instead. */
unsigned char from_test[MAX*3];
unsigned char to_test[MAX*3];
unsigned char from_known[MAX*3];
unsigned char to_known[MAX*3];
/* Leave some room before and after the area tested, so we can detect
overwrites of up to N bytes, N being the amount tested. If you
want to test using valgrind, make these malloced instead. */
unsigned char from_test[MAX * 3];
unsigned char to_test[MAX * 3];
unsigned char from_known[MAX * 3];
unsigned char to_known[MAX * 3];
/* Non-overlap. */
for (i = 0; i < MAX; i++)
/* Non-overlap. */
for (i = 0; i < MAX; i++)
{
/* Do the memmove first before setting the known array, so we know
it didn't change any of the known array. */
fill (from_test);
fill (to_test);
xmemmove (to_test + MAX, 1 + from_test + MAX, i);
/* Do the memmove first before setting the known array, so we know
it didn't change any of the known array. */
fill(from_test);
fill(to_test);
xmemmove(to_test + MAX, 1 + from_test + MAX, i);
fill (from_known);
fill (to_known);
mymemmove (to_known + MAX, 1 + from_known + MAX, i);
fill(from_known);
fill(to_known);
mymemmove(to_known + MAX, 1 + from_known + MAX, i);
if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
{
errors++;
DEBUGP ("memmove failed non-overlap test for %d bytes\n", i);
}
if (memcmp(to_known, to_test, sizeof(to_known)) != 0)
{
errors++;
DEBUGP("memmove failed non-overlap test for %d bytes\n", i);
}
}
/* Overlap-from-before. */
for (i = 0; i < MAX; i++)
/* Overlap-from-before. */
for (i = 0; i < MAX; i++)
{
size_t j;
for (j = 0; j < i; j++)
{
fill (to_test);
xmemmove (to_test + MAX * 2 - i, to_test + MAX * 2 - i - j, i);
size_t j;
for (j = 0; j < i; j++)
{
fill(to_test);
xmemmove(to_test + MAX * 2 - i, to_test + MAX * 2 - i - j, i);
fill (to_known);
mymemmove (to_known + MAX * 2 - i, to_known + MAX * 2 - i - j, i);
fill(to_known);
mymemmove(to_known + MAX * 2 - i, to_known + MAX * 2 - i - j, i);
if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
{
errors++;
DEBUGP ("memmove failed for %d bytes,"
" with src %d bytes before dest\n",
i, j);
}
}
if (memcmp(to_known, to_test, sizeof(to_known)) != 0)
{
errors++;
DEBUGP("memmove failed for %d bytes,"
" with src %d bytes before dest\n",
i, j);
}
}
}
/* Overlap-from-after. */
for (i = 0; i < MAX; i++)
/* Overlap-from-after. */
for (i = 0; i < MAX; i++)
{
size_t j;
for (j = 0; j < i; j++)
{
fill (to_test);
xmemmove (to_test + MAX, to_test + MAX + j, i);
size_t j;
for (j = 0; j < i; j++)
{
fill(to_test);
xmemmove(to_test + MAX, to_test + MAX + j, i);
fill (to_known);
mymemmove (to_known + MAX, to_known + MAX + j, i);
fill(to_known);
mymemmove(to_known + MAX, to_known + MAX + j, i);
if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
{
errors++;
DEBUGP ("memmove failed when moving %d bytes,"
" with src %d bytes after dest\n",
i, j);
}
}
if (memcmp(to_known, to_test, sizeof(to_known)) != 0)
{
errors++;
DEBUGP("memmove failed when moving %d bytes,"
" with src %d bytes after dest\n",
i, j);
}
}
}
if (errors != 0)
abort ();
printf("ok\n");
if (errors != 0)
abort();
printf("ok\n");
}

View File

@ -46,7 +46,6 @@
#define BUFF_SIZE 256
/* The macro LONG_TEST controls whether a short or a more comprehensive test
of strcmp should be performed. */
#ifdef LONG_TEST
@ -106,186 +105,183 @@
#error "Buffer overrun: MAX_OFFSET + MAX_BLOCK_SIZE + MAX_DIFF + MAX_LEN + MAX_ZEROS >= BUFF_SIZE."
#endif
#define TOO_MANY_ERRORS 11
static int errors = 0;
const char *testname = "strcmp";
const char* testname = "strcmp";
static void
print_error (char const* msg, ...)
static void print_error(char const* msg, ...)
{
errors++;
if (errors == TOO_MANY_ERRORS)
errors++;
if (errors == TOO_MANY_ERRORS)
{
fprintf (stderr, "Too many errors.\n");
fprintf(stderr, "Too many errors.\n");
}
else if (errors < TOO_MANY_ERRORS)
else if (errors < TOO_MANY_ERRORS)
{
va_list ap;
va_start (ap, msg);
vfprintf (stderr, msg, ap);
va_end (ap);
va_list ap;
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
}
else
else
{
/* Further errors omitted. */
/* Further errors omitted. */
}
}
extern int rand_seed;
void strcmp_main(void)
void strcmp_main(void)
{
/* Allocate buffers to read and write from. */
char src[BUFF_SIZE], dest[BUFF_SIZE];
/* Allocate buffers to read and write from. */
char src[BUFF_SIZE], dest[BUFF_SIZE];
/* Fill the source buffer with non-null values, reproducible random data. */
srand (rand_seed);
int i, j, zeros;
unsigned sa;
unsigned da;
unsigned n, m, len;
char *p;
int ret;
/* Fill the source buffer with non-null values, reproducible random data. */
srand(rand_seed);
int i, j, zeros;
unsigned sa;
unsigned da;
unsigned n, m, len;
char* p;
int ret;
/* Make calls to strcmp with block sizes ranging between 1 and
MAX_BLOCK_SIZE bytes, aligned and misaligned source and destination. */
for (sa = 0; sa <= MAX_OFFSET; sa++)
for (da = 0; da <= MAX_OFFSET; da++)
for (n = 1; n <= MAX_BLOCK_SIZE; n++)
{
for (m = 1; m < n + MAX_DIFF; m++)
for (len = 0; len < MAX_LEN; len++)
for (zeros = 1; zeros < MAX_ZEROS; zeros++)
{
if (n - m > MAX_DIFF)
continue;
/* Make a copy of the source. */
for (i = 0; i < BUFF_SIZE; i++)
{
src[i] = 'A' + (i % 26);
dest[i] = src[i];
}
delay(0);
memcpy (dest + da, src + sa, n);
/* Make calls to strcmp with block sizes ranging between 1 and
MAX_BLOCK_SIZE bytes, aligned and misaligned source and destination. */
for (sa = 0; sa <= MAX_OFFSET; sa++)
for (da = 0; da <= MAX_OFFSET; da++)
for (n = 1; n <= MAX_BLOCK_SIZE; n++)
{
for (m = 1; m < n + MAX_DIFF; m++)
for (len = 0; len < MAX_LEN; len++)
for (zeros = 1; zeros < MAX_ZEROS; zeros++)
{
if (n - m > MAX_DIFF)
continue;
/* Make a copy of the source. */
for (i = 0; i < BUFF_SIZE; i++)
{
src[i] = 'A' + (i % 26);
dest[i] = src[i];
}
delay(0);
memcpy(dest + da, src + sa, n);
/* Make src 0-terminated. */
p = src + sa + n - 1;
for (i = 0; i < zeros; i++)
{
*p++ = '\0';
}
/* Make src 0-terminated. */
p = src + sa + n - 1;
for (i = 0; i < zeros; i++)
{
*p++ = '\0';
}
/* Modify dest. */
p = dest + da + m - 1;
for (j = 0; j < (int)len; j++)
*p++ = 'x';
/* Make dest 0-terminated. */
*p = '\0';
/* Modify dest. */
p = dest + da + m - 1;
for (j = 0; j < (int)len; j++)
*p++ = 'x';
/* Make dest 0-terminated. */
*p = '\0';
ret = strcmp (src + sa, dest + da);
ret = strcmp(src + sa, dest + da);
/* Check return value. */
if (n == m)
{
if (len == 0)
{
if (ret != 0)
{
print_error ("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected 0.\n",
testname, n, sa, da, m, len, ret);
}
}
else
{
if (ret >= 0)
print_error ("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected negative.\n",
testname, n, sa, da, m, len, ret);
}
}
else if (m > n)
{
if (ret >= 0)
{
print_error ("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected negative.\n",
testname, n, sa, da, m, len, ret);
}
}
else /* m < n */
{
if (len == 0)
{
if (ret <= 0)
print_error ("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected positive.\n",
testname, n, sa, da, m, len, ret);
}
else
{
if (ret >= 0)
print_error ("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected negative.\n",
testname, n, sa, da, m, len, ret);
}
}
}
}
/* Check return value. */
if (n == m)
{
if (len == 0)
{
if (ret != 0)
{
print_error("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected 0.\n",
testname, n, sa, da, m, len, ret);
}
}
else
{
if (ret >= 0)
print_error("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected negative.\n",
testname, n, sa, da, m, len, ret);
}
}
else if (m > n)
{
if (ret >= 0)
{
print_error("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected negative.\n",
testname, n, sa, da, m, len, ret);
}
}
else /* m < n */
{
if (len == 0)
{
if (ret <= 0)
print_error("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected positive.\n",
testname, n, sa, da, m, len, ret);
}
else
{
if (ret >= 0)
print_error("\nFailed: after %s of %u bytes "
"with src_align %u and dst_align %u, "
"dest after %d bytes is modified for %d bytes, "
"return value is %d, expected negative.\n",
testname, n, sa, da, m, len, ret);
}
}
}
}
/* Check some corner cases. */
src[1] = 'A';
dest[1] = 'A';
src[2] = 'B';
dest[2] = 'B';
src[3] = 'C';
dest[3] = 'C';
src[4] = '\0';
dest[4] = '\0';
/* Check some corner cases. */
src[1] = 'A';
dest[1] = 'A';
src[2] = 'B';
dest[2] = 'B';
src[3] = 'C';
dest[3] = 'C';
src[4] = '\0';
dest[4] = '\0';
src[0] = 0xc1;
dest[0] = 0x41;
ret = strcmp (src, dest);
if (ret <= 0)
print_error ("\nFailed: expected positive, return %d\n", ret);
src[0] = 0xc1;
dest[0] = 0x41;
ret = strcmp(src, dest);
if (ret <= 0)
print_error("\nFailed: expected positive, return %d\n", ret);
src[0] = 0x01;
dest[0] = 0x82;
ret = strcmp (src, dest);
if (ret >= 0)
print_error ("\nFailed: expected negative, return %d\n", ret);
src[0] = 0x01;
dest[0] = 0x82;
ret = strcmp(src, dest);
if (ret >= 0)
print_error("\nFailed: expected negative, return %d\n", ret);
dest[0] = src[0] = 'D';
src[3] = 0xc1;
dest[3] = 0x41;
ret = strcmp (src, dest);
if (ret <= 0)
print_error ("\nFailed: expected positive, return %d\n", ret);
dest[0] = src[0] = 'D';
src[3] = 0xc1;
dest[3] = 0x41;
ret = strcmp(src, dest);
if (ret <= 0)
print_error("\nFailed: expected positive, return %d\n", ret);
src[3] = 0x01;
dest[3] = 0x82;
ret = strcmp (src, dest);
if (ret >= 0)
print_error ("\nFailed: expected negative, return %d\n", ret);
src[3] = 0x01;
dest[3] = 0x82;
ret = strcmp(src, dest);
if (ret >= 0)
print_error("\nFailed: expected negative, return %d\n", ret);
//printf ("\n");
if (errors != 0)
// printf ("\n");
if (errors != 0)
{
printf ("ERROR. FAILED.\n");
abort ();
printf("ERROR. FAILED.\n");
abort();
}
//exit (0);
printf("ok\n");
// exit (0);
printf("ok\n");
}

View File

@ -26,336 +26,297 @@
#define MAX_2 (2 * MAX_1 + MAX_1 / 10)
void eprintf (int line, char *result, char *expected, int size)
void eprintf(int line, char* result, char* expected, int size)
{
if (size != 0)
printf ("Failure at line %d, result is <%.*s>, should be <%s> of size %d\n",
line, size, result, expected, size);
else
printf ("Failure at line %d, result is <%s>, should be <%s>\n",
line, result, expected);
if (size != 0)
printf("Failure at line %d, result is <%.*s>, should be <%s> of size %d\n", line, size,
result, expected, size);
else
printf("Failure at line %d, result is <%s>, should be <%s>\n", line, result, expected);
}
void mycopy (char *target, char *source, int size)
void mycopy(char* target, char* source, int size)
{
int i;
int i;
for (i = 0; i < size; ++i)
for (i = 0; i < size; ++i)
{
target[i] = source[i];
target[i] = source[i];
}
}
void myset (char *target, char ch, int size)
void myset(char* target, char ch, int size)
{
int i;
for (i = 0; i < size; ++i)
int i;
for (i = 0; i < size; ++i)
{
target[i] = ch;
target[i] = ch;
}
}
void tstring_main(void)
{
char target[MAX_1] = "A";
char first_char;
char second_char;
char array[] = "abcdefghijklmnopqrstuvwxz";
char array2[] = "0123456789!@#$%^&*(";
char buffer2[MAX_1];
char buffer3[MAX_1];
char buffer4[MAX_1];
char buffer5[MAX_2];
char buffer6[MAX_2];
char buffer7[MAX_2];
char expected[MAX_1];
char *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7;
int i, j, k, x, z, align_test_iterations;
z = 0;
int test_failed = 0;
char target[MAX_1] = "A";
char first_char;
char second_char;
char array[] = "abcdefghijklmnopqrstuvwxz";
char array2[] = "0123456789!@#$%^&*(";
char buffer2[MAX_1];
char buffer3[MAX_1];
char buffer4[MAX_1];
char buffer5[MAX_2];
char buffer6[MAX_2];
char buffer7[MAX_2];
char expected[MAX_1];
char *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7;
int i, j, k, x, z, align_test_iterations;
z = 0;
tmp1 = target;
tmp2 = buffer2;
tmp3 = buffer3;
tmp4 = buffer4;
tmp5 = buffer5;
tmp6 = buffer6;
tmp7 = buffer7;
int test_failed = 0;
tmp2[0] = 'Z';
tmp2[1] = '\0';
tmp1 = target;
tmp2 = buffer2;
tmp3 = buffer3;
tmp4 = buffer4;
tmp5 = buffer5;
tmp6 = buffer6;
tmp7 = buffer7;
if (memset (target, 'X', 0) != target ||
memcpy (target, "Y", 0) != target ||
memmove (target, "K", 0) != target ||
strncpy (tmp2, "4", 0) != tmp2 ||
strncat (tmp2, "123", 0) != tmp2 ||
strcat (target, "") != target)
tmp2[0] = 'Z';
tmp2[1] = '\0';
if (memset(target, 'X', 0) != target || memcpy(target, "Y", 0) != target
|| memmove(target, "K", 0) != target || strncpy(tmp2, "4", 0) != tmp2
|| strncat(tmp2, "123", 0) != tmp2 || strcat(target, "") != target)
{
eprintf (__LINE__, target, "A", 0);
test_failed = 1;
eprintf(__LINE__, target, "A", 0);
test_failed = 1;
}
if (strcmp (target, "A") || strlen(target) != 1 || memchr (target, 'A', 0) != NULL
|| memcmp (target, "J", 0) || strncmp (target, "A", 1) || strncmp (target, "J", 0) ||
tmp2[0] != 'Z' || tmp2[1] != '\0')
if (strcmp(target, "A") || strlen(target) != 1 || memchr(target, 'A', 0) != NULL
|| memcmp(target, "J", 0) || strncmp(target, "A", 1) || strncmp(target, "J", 0)
|| tmp2[0] != 'Z' || tmp2[1] != '\0')
{
eprintf (__LINE__, target, "A", 0);
test_failed = 1;
eprintf(__LINE__, target, "A", 0);
test_failed = 1;
}
tmp2[2] = 'A';
if (strcpy (target, "") != target ||
strncpy (tmp2, "", 4) != tmp2 ||
strcat (target, "") != target)
tmp2[2] = 'A';
if (strcpy(target, "") != target || strncpy(tmp2, "", 4) != tmp2
|| strcat(target, "") != target)
{
eprintf (__LINE__, target, "", 0);
test_failed = 1;
eprintf(__LINE__, target, "", 0);
test_failed = 1;
}
if (target[0] != '\0' || strncmp (target, "", 1) ||
memcmp (tmp2, "\0\0\0\0", 4))
if (target[0] != '\0' || strncmp(target, "", 1) || memcmp(tmp2, "\0\0\0\0", 4))
{
eprintf (__LINE__, target, "", 0);
test_failed = 1;
eprintf(__LINE__, target, "", 0);
test_failed = 1;
}
tmp2[2] = 'A';
if (strncat (tmp2, "1", 3) != tmp2 ||
memcmp (tmp2, "1\0A", 3))
tmp2[2] = 'A';
if (strncat(tmp2, "1", 3) != tmp2 || memcmp(tmp2, "1\0A", 3))
{
eprintf (__LINE__, tmp2, "1\0A", 3);
test_failed = 1;
eprintf(__LINE__, tmp2, "1\0A", 3);
test_failed = 1;
}
if (strcpy (tmp3, target) != tmp3 ||
strcat (tmp3, "X") != tmp3 ||
strncpy (tmp2, "X", 2) != tmp2 ||
memset (target, tmp2[0], 1) != target)
if (strcpy(tmp3, target) != tmp3 || strcat(tmp3, "X") != tmp3 || strncpy(tmp2, "X", 2) != tmp2
|| memset(target, tmp2[0], 1) != target)
{
eprintf (__LINE__, target, "X", 0);
test_failed = 1;
eprintf(__LINE__, target, "X", 0);
test_failed = 1;
}
if (strcmp (target, "X") || strlen (target) != 1 ||
memchr (target, 'X', 2) != target ||
strchr (target, 'X') != target ||
memchr (target, 'Y', 2) != NULL ||
strchr (target, 'Y') != NULL ||
strcmp (tmp3, target) ||
strncmp (tmp3, target, 2) ||
memcmp (target, "K", 0) ||
strncmp (target, tmp3, 3))
if (strcmp(target, "X") || strlen(target) != 1 || memchr(target, 'X', 2) != target
|| strchr(target, 'X') != target || memchr(target, 'Y', 2) != NULL
|| strchr(target, 'Y') != NULL || strcmp(tmp3, target) || strncmp(tmp3, target, 2)
|| memcmp(target, "K", 0) || strncmp(target, tmp3, 3))
{
eprintf (__LINE__, target, "X", 0);
test_failed = 1;
eprintf(__LINE__, target, "X", 0);
test_failed = 1;
}
if (strcpy (tmp3, "Y") != tmp3 ||
strcat (tmp3, "Y") != tmp3 ||
memset (target, 'Y', 2) != target)
if (strcpy(tmp3, "Y") != tmp3 || strcat(tmp3, "Y") != tmp3 || memset(target, 'Y', 2) != target)
{
eprintf (__LINE__, target, "Y", 0);
test_failed = 1;
eprintf(__LINE__, target, "Y", 0);
test_failed = 1;
}
target[2] = '\0';
if (memcmp (target, "YY", 2) || strcmp (target, "YY") ||
strlen (target) != 2 || memchr (target, 'Y', 2) != target ||
strcmp (tmp3, target) ||
strncmp (target, tmp3, 3) ||
strncmp (target, tmp3, 4) ||
strncmp (target, tmp3, 2) ||
strchr (target, 'Y') != target)
target[2] = '\0';
if (memcmp(target, "YY", 2) || strcmp(target, "YY") || strlen(target) != 2
|| memchr(target, 'Y', 2) != target || strcmp(tmp3, target) || strncmp(target, tmp3, 3)
|| strncmp(target, tmp3, 4) || strncmp(target, tmp3, 2) || strchr(target, 'Y') != target)
{
eprintf (__LINE__, target, "YY", 2);
test_failed = 1;
eprintf(__LINE__, target, "YY", 2);
test_failed = 1;
}
strcpy (target, "WW");
if (memcmp (target, "WW", 2) || strcmp (target, "WW") ||
strlen (target) != 2 || memchr (target, 'W', 2) != target ||
strchr (target, 'W') != target)
strcpy(target, "WW");
if (memcmp(target, "WW", 2) || strcmp(target, "WW") || strlen(target) != 2
|| memchr(target, 'W', 2) != target || strchr(target, 'W') != target)
{
eprintf (__LINE__, target, "WW", 2);
test_failed = 1;
eprintf(__LINE__, target, "WW", 2);
test_failed = 1;
}
if (strncpy (target, "XX", 16) != target ||
memcmp (target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
if (strncpy(target, "XX", 16) != target || memcmp(target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
{
eprintf (__LINE__, target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
test_failed = 1;
eprintf(__LINE__, target, "XX\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16);
test_failed = 1;
}
if (strcpy (tmp3, "ZZ") != tmp3 ||
strcat (tmp3, "Z") != tmp3 ||
memcpy (tmp4, "Z", 2) != tmp4 ||
strcat (tmp4, "ZZ") != tmp4 ||
memset (target, 'Z', 3) != target)
if (strcpy(tmp3, "ZZ") != tmp3 || strcat(tmp3, "Z") != tmp3 || memcpy(tmp4, "Z", 2) != tmp4
|| strcat(tmp4, "ZZ") != tmp4 || memset(target, 'Z', 3) != target)
{
eprintf (__LINE__, target, "ZZZ", 3);
test_failed = 1;
eprintf(__LINE__, target, "ZZZ", 3);
test_failed = 1;
}
target[3] = '\0';
tmp5[0] = '\0';
strncat (tmp5, "123", 2);
if (memcmp (target, "ZZZ", 3) || strcmp (target, "ZZZ") ||
strcmp (tmp3, target) || strcmp (tmp4, target) ||
strncmp (target, "ZZZ", 4) || strncmp (target, "ZZY", 3) <= 0 ||
strncmp ("ZZY", target, 4) >= 0 ||
memcmp (tmp5, "12", 3) ||
strlen (target) != 3)
target[3] = '\0';
tmp5[0] = '\0';
strncat(tmp5, "123", 2);
if (memcmp(target, "ZZZ", 3) || strcmp(target, "ZZZ") || strcmp(tmp3, target)
|| strcmp(tmp4, target) || strncmp(target, "ZZZ", 4) || strncmp(target, "ZZY", 3) <= 0
|| strncmp("ZZY", target, 4) >= 0 || memcmp(tmp5, "12", 3) || strlen(target) != 3)
{
eprintf (__LINE__, target, "ZZZ", 3);
test_failed = 1;
eprintf(__LINE__, target, "ZZZ", 3);
test_failed = 1;
}
target[2] = 'K';
if (memcmp (target, "ZZZ", 2) || strcmp (target, "ZZZ") >= 0 ||
memcmp (target, "ZZZ", 3) >= 0 || strlen (target) != 3 ||
memchr (target, 'K', 3) != target + 2 ||
strncmp (target, "ZZZ", 2) || strncmp (target, "ZZZ", 4) >= 0 ||
strchr (target, 'K') != target + 2)
target[2] = 'K';
if (memcmp(target, "ZZZ", 2) || strcmp(target, "ZZZ") >= 0 || memcmp(target, "ZZZ", 3) >= 0
|| strlen(target) != 3 || memchr(target, 'K', 3) != target + 2 || strncmp(target, "ZZZ", 2)
|| strncmp(target, "ZZZ", 4) >= 0 || strchr(target, 'K') != target + 2)
{
eprintf (__LINE__, target, "ZZK", 3);
test_failed = 1;
eprintf(__LINE__, target, "ZZK", 3);
test_failed = 1;
}
strcpy (target, "AAA");
if (memcmp (target, "AAA", 3) || strcmp (target, "AAA") ||
strncmp (target, "AAA", 3) ||
strlen (target) != 3)
strcpy(target, "AAA");
if (memcmp(target, "AAA", 3) || strcmp(target, "AAA") || strncmp(target, "AAA", 3)
|| strlen(target) != 3)
{
eprintf (__LINE__, target, "AAA", 3);
test_failed = 1;
eprintf(__LINE__, target, "AAA", 3);
test_failed = 1;
}
j = 5;
while (j < MAX_1)
j = 5;
while (j < MAX_1)
{
for (i = j-1; i <= j+1; ++i)
for (i = j - 1; i <= j + 1; ++i)
{
/* don't bother checking unaligned data in the larger
sizes since it will waste time without performing additional testing */
if ((size_t)i <= 16 * sizeof(long))
{
align_test_iterations = 2*sizeof(long);
if ((size_t)i <= 2 * sizeof(long) + 1)
z = 2;
else
z = 2 * sizeof(long);
}
else
/* don't bother checking unaligned data in the larger
sizes since it will waste time without performing additional testing */
if ((size_t)i <= 16 * sizeof(long))
{
align_test_iterations = 1;
align_test_iterations = 2 * sizeof(long);
if ((size_t)i <= 2 * sizeof(long) + 1)
z = 2;
else
z = 2 * sizeof(long);
}
else
{
align_test_iterations = 1;
}
for (x = 0; x < align_test_iterations; ++x)
{
tmp1 = target + x;
tmp2 = buffer2 + x;
tmp3 = buffer3 + x;
tmp4 = buffer4 + x;
tmp5 = buffer5 + x;
tmp6 = buffer6 + x;
for (x = 0; x < align_test_iterations; ++x)
{
tmp1 = target + x;
tmp2 = buffer2 + x;
tmp3 = buffer3 + x;
tmp4 = buffer4 + x;
tmp5 = buffer5 + x;
tmp6 = buffer6 + x;
first_char = array[i % (sizeof(array) - 1)];
second_char = array2[i % (sizeof(array2) - 1)];
memset (tmp1, first_char, i);
mycopy (tmp2, tmp1, i);
myset (tmp2 + z, second_char, i - z - 1);
if (memcpy (tmp1 + z, tmp2 + z, i - z - 1) != tmp1 + z)
{
printf ("error at line %d\n", __LINE__);
test_failed = 1;
}
first_char = array[i % (sizeof(array) - 1)];
second_char = array2[i % (sizeof(array2) - 1)];
memset(tmp1, first_char, i);
mycopy(tmp2, tmp1, i);
myset(tmp2 + z, second_char, i - z - 1);
if (memcpy(tmp1 + z, tmp2 + z, i - z - 1) != tmp1 + z)
{
printf("error at line %d\n", __LINE__);
test_failed = 1;
}
tmp1[i] = '\0';
tmp2[i] = '\0';
if (strcpy (expected, tmp2) != expected)
{
printf ("error at line %d\n", __LINE__);
test_failed = 1;
}
tmp2[i-z] = first_char + 1;
if (memmove (tmp2 + z + 1, tmp2 + z, i - z - 1) != tmp2 + z + 1 ||
memset (tmp3, first_char, i) != tmp3)
{
printf ("error at line %d\n", __LINE__);
test_failed = 1;
}
tmp1[i] = '\0';
tmp2[i] = '\0';
if (strcpy(expected, tmp2) != expected)
{
printf("error at line %d\n", __LINE__);
test_failed = 1;
}
tmp2[i - z] = first_char + 1;
if (memmove(tmp2 + z + 1, tmp2 + z, i - z - 1) != tmp2 + z + 1
|| memset(tmp3, first_char, i) != tmp3)
{
printf("error at line %d\n", __LINE__);
test_failed = 1;
}
myset (tmp4, first_char, i);
tmp5[0] = '\0';
if (strncpy (tmp5, tmp1, i+1) != tmp5 ||
strcat (tmp5, tmp1) != tmp5)
{
printf ("error at line %d\n", __LINE__);
test_failed = 1;
}
mycopy (tmp6, tmp1, i);
mycopy (tmp6 + i, tmp1, i + 1);
myset(tmp4, first_char, i);
tmp5[0] = '\0';
if (strncpy(tmp5, tmp1, i + 1) != tmp5 || strcat(tmp5, tmp1) != tmp5)
{
printf("error at line %d\n", __LINE__);
test_failed = 1;
}
mycopy(tmp6, tmp1, i);
mycopy(tmp6 + i, tmp1, i + 1);
tmp7[2*i+z] = second_char;
strcpy (tmp7, tmp1);
(void)strchr (tmp1, second_char);
if (memcmp (tmp1, expected, i) || strcmp (tmp1, expected) ||
strncmp (tmp1, expected, i) ||
strncmp (tmp1, expected, i+1) ||
strcmp (tmp1, tmp2) >= 0 || memcmp (tmp1, tmp2, i) >= 0 ||
strncmp (tmp1, tmp2, i+1) >= 0 ||
(int)strlen (tmp1) != i || memchr (tmp1, first_char, i) != tmp1 ||
strchr (tmp1, first_char) != tmp1 ||
memchr (tmp1, second_char, i) != tmp1 + z ||
strchr (tmp1, second_char) != tmp1 + z ||
strcmp (tmp5, tmp6) ||
strncat (tmp7, tmp1, i+2) != tmp7 ||
strcmp (tmp7, tmp6) ||
tmp7[2*i+z] != second_char)
{
eprintf (__LINE__, tmp1, expected, 0);
printf ("x is %d\n",x);
printf ("i is %d\n", i);
printf ("tmp1 is <%p>\n", tmp1);
printf ("tmp5 is <%p> <%s>\n", tmp5, tmp5);
printf ("tmp6 is <%p> <%s>\n", tmp6, tmp6);
test_failed = 1;
}
tmp7[2 * i + z] = second_char;
strcpy(tmp7, tmp1);
for (k = 1; k <= align_test_iterations && k <= i; ++k)
{
if (memcmp (tmp3, tmp4, i - k + 1) != 0 ||
strncmp (tmp3, tmp4, i - k + 1) != 0)
{
printf ("Failure at line %d, comparing %.*s with %.*s\n",
__LINE__, i, tmp3, i, tmp4);
test_failed = 1;
}
tmp4[i-k] = first_char + 1;
if (memcmp (tmp3, tmp4, i) >= 0 ||
strncmp (tmp3, tmp4, i) >= 0 ||
memcmp (tmp4, tmp3, i) <= 0 ||
strncmp (tmp4, tmp3, i) <= 0)
{
printf ("Failure at line %d, comparing %.*s with %.*s\n",
__LINE__, i, tmp3, i, tmp4);
test_failed = 1;
}
tmp4[i-k] = first_char;
}
}
(void)strchr(tmp1, second_char);
if (memcmp(tmp1, expected, i) || strcmp(tmp1, expected)
|| strncmp(tmp1, expected, i) || strncmp(tmp1, expected, i + 1)
|| strcmp(tmp1, tmp2) >= 0 || memcmp(tmp1, tmp2, i) >= 0
|| strncmp(tmp1, tmp2, i + 1) >= 0 || (int)strlen(tmp1) != i
|| memchr(tmp1, first_char, i) != tmp1 || strchr(tmp1, first_char) != tmp1
|| memchr(tmp1, second_char, i) != tmp1 + z
|| strchr(tmp1, second_char) != tmp1 + z || strcmp(tmp5, tmp6)
|| strncat(tmp7, tmp1, i + 2) != tmp7 || strcmp(tmp7, tmp6)
|| tmp7[2 * i + z] != second_char)
{
eprintf(__LINE__, tmp1, expected, 0);
printf("x is %d\n", x);
printf("i is %d\n", i);
printf("tmp1 is <%p>\n", tmp1);
printf("tmp5 is <%p> <%s>\n", tmp5, tmp5);
printf("tmp6 is <%p> <%s>\n", tmp6, tmp6);
test_failed = 1;
}
for (k = 1; k <= align_test_iterations && k <= i; ++k)
{
if (memcmp(tmp3, tmp4, i - k + 1) != 0 || strncmp(tmp3, tmp4, i - k + 1) != 0)
{
printf("Failure at line %d, comparing %.*s with %.*s\n", __LINE__, i, tmp3,
i, tmp4);
test_failed = 1;
}
tmp4[i - k] = first_char + 1;
if (memcmp(tmp3, tmp4, i) >= 0 || strncmp(tmp3, tmp4, i) >= 0
|| memcmp(tmp4, tmp3, i) <= 0 || strncmp(tmp4, tmp3, i) <= 0)
{
printf("Failure at line %d, comparing %.*s with %.*s\n", __LINE__, i, tmp3,
i, tmp4);
test_failed = 1;
}
tmp4[i - k] = first_char;
}
}
}
j = ((2 * j) >> 2) << 2;
j = ((2 * j) >> 2) << 2;
}
if (test_failed)
abort();
if (test_failed)
abort();
printf("ok\n");
printf("ok\n");
}