Tests for memory copy and write functions
This commit is contained in:
parent
954696728c
commit
b9e9df32f5
@ -2,7 +2,14 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// NOTE: Functions in this file have different names during testing, since they
|
||||
// are otherwise overridden by standard library functions
|
||||
|
||||
#ifdef TTEST
|
||||
void *my_memcpy(void *dst, const void *src, size_t n)
|
||||
#else
|
||||
void *memcpy(void *dst, const void *src, size_t n)
|
||||
#endif
|
||||
{
|
||||
char *dp = dst;
|
||||
const char *sp = src;
|
||||
@ -10,21 +17,35 @@ void *memcpy(void *dst, const void *src, size_t n)
|
||||
return dst;
|
||||
}
|
||||
|
||||
#ifdef TTEST
|
||||
void *my_memset(void *s, int c, size_t n)
|
||||
#else
|
||||
void *memset(void *s, int c, size_t n)
|
||||
#endif
|
||||
{
|
||||
unsigned char *p = s;
|
||||
while(n--) *p++ = (unsigned char)c;
|
||||
return s;
|
||||
}
|
||||
|
||||
#ifdef TTEST
|
||||
void *my_memmove(void *dest, const void *src, size_t n)
|
||||
#else
|
||||
void *memmove(void *dest, const void *src, size_t n)
|
||||
#endif
|
||||
{
|
||||
// Since our memcpy implementation copies one char* at a time, this is safe
|
||||
// We'll need some form of malloc to implement a good memmove.
|
||||
// For now, we'll just use defer to memcpy - WHICH IS UNSAFE!
|
||||
// TODO: Write a good implementation of memmove
|
||||
memcpy(dest, src, n);
|
||||
return dest;
|
||||
}
|
||||
|
||||
#ifdef TTEST
|
||||
int my_memcmp(const void *s1, const void *s2, size_t n)
|
||||
#else
|
||||
int memcmp(const void *s1, const void *s2, size_t n)
|
||||
#endif
|
||||
{
|
||||
const unsigned char *p1 = s1, *p2 = s2;
|
||||
for(; n--; p1++, p2++)
|
||||
@ -35,7 +56,11 @@ int memcmp(const void *s1, const void *s2, size_t n)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef TTEST
|
||||
size_t my_strlen(const char *s)
|
||||
#else
|
||||
size_t strlen(const char *s)
|
||||
#endif
|
||||
{
|
||||
size_t len = 0;
|
||||
while(*s++) len++;
|
||||
|
72
src/kernel/memory/string.tt
Normal file
72
src/kernel/memory/string.tt
Normal file
@ -0,0 +1,72 @@
|
||||
// vim: ft=c
|
||||
#include <ttest.h>
|
||||
#include <string.h>
|
||||
#include "string.c"
|
||||
|
||||
char *lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus interdum eros at enim tempus sed.";
|
||||
|
||||
char dst[100];
|
||||
|
||||
BEFORE()
|
||||
{
|
||||
for(int i = 0; i < 100; dst[i++] = '\0');
|
||||
}
|
||||
|
||||
TEST(memcpy_copies_data)
|
||||
{
|
||||
my_memcpy(dst, lipsum, 100);
|
||||
ASSERT_EQ_STR(dst, lipsum, 100);
|
||||
}
|
||||
TEST(memcpy_doesnt_copy_too_much)
|
||||
{
|
||||
my_memcpy(dst, lipsum, 10);
|
||||
ASSERT_EQ_CHR(dst[10], 0);
|
||||
}
|
||||
|
||||
TEST(memset_sets_data)
|
||||
{
|
||||
memset(dst, 0, 10);
|
||||
my_memset(dst, 'a', 5);
|
||||
ASSERT_EQ_STR(dst, "aaaaa", 100);
|
||||
}
|
||||
|
||||
// Those test won't pass right now since our memmove implementation is
|
||||
// incomplete (see string.c)
|
||||
|
||||
/*TEST(memmove_moves_data)
|
||||
{
|
||||
memcpy(&dst[10], "12345", 5);
|
||||
my_memmove(dst, &dst[10], 5);
|
||||
ASSERT_EQ_STR(dst, "12345", 5);
|
||||
}*/
|
||||
/*TEST(memmove_handles_overlap)
|
||||
{
|
||||
memcpy(&dst[5], "1234567890", 10);
|
||||
my_memmove(dst, &dst[5], 10);
|
||||
ASSERT_EQ_STR(dst, "1234567890", 10);
|
||||
}*/
|
||||
/*TEST(memmove_handles_overlap_the_other_way)
|
||||
{
|
||||
memcpy(dst, "1234567890", 10);
|
||||
my_memmove(&dst[5], dst, 10);
|
||||
ASSERT_EQ_STR(&dst[5], "1234567890", 10);
|
||||
}*/
|
||||
|
||||
TEST(memcmp_returns_zero_for_equal_strings)
|
||||
{
|
||||
ASSERT_EQ_INT(my_memcmp("1234", "1234", 4), 0);
|
||||
}
|
||||
TEST(memcmp_returns_difference_for_unequal_strings)
|
||||
{
|
||||
ASSERT_EQ_INT(my_memcmp("1234", "0234", 4), 1);
|
||||
}
|
||||
TEST(memcmp_returns_signed_difference_for_unequal_strings)
|
||||
{
|
||||
ASSERT_EQ_INT(my_memcmp("1234", "1236", 4), -2);
|
||||
}
|
||||
|
||||
TEST(strlen_counts_correctly)
|
||||
{
|
||||
ASSERT_EQ_INT(my_strlen("12345"), 5);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user