Luna/tests/libc/stdlib.c

234 lines
3.8 KiB
C

#include "Test.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
DEFINE_TEST(atoi)
{
START_TEST(atoi);
const char* str = "42";
int num = atoi(str);
EXPECT_EQ(num, 42);
str = "-56";
num = atoi(str);
EXPECT_EQ(num, -56);
str = "Not a number";
num = atoi(str);
EXPECT_EQ(num, 0);
TEST_SUCCESS();
}
DEFINE_TEST(atol)
{
START_TEST(atol);
const char* str = "42";
long num = atol(str);
EXPECT_EQ(num, 42);
str = "-56";
num = atol(str);
EXPECT_EQ(num, -56);
str = "Not a number";
num = atol(str);
EXPECT_EQ(num, 0);
str = "68719476735";
num = atol(str);
EXPECT_EQ(num, 68719476735);
TEST_SUCCESS();
}
DEFINE_TEST(atoll)
{
START_TEST(atoll);
const char* str = "42";
long long num = atoll(str);
EXPECT_EQ(num, 42);
str = "-56";
num = atoll(str);
EXPECT_EQ(num, -56);
str = "Not a number";
num = atoll(str);
EXPECT_EQ(num, 0);
str = "68719476735";
num = atoll(str);
EXPECT_EQ(num, 68719476735);
TEST_SUCCESS();
}
DEFINE_TEST(srand)
{
START_TEST(srand);
srand(5849);
int val = rand();
EXPECT_EQ(val, -1731894882);
TEST_SUCCESS();
}
DEFINE_TEST(malloc)
{
START_TEST(malloc);
int* ptr = malloc(6 * sizeof(int));
if (!ptr)
{
TEST_NOT_SURE(ptr);
return true;
}
*ptr = 6;
EXPECT_EQ(*ptr, 6);
ptr[5] = 4;
EXPECT_EQ(ptr[5], 4);
free(ptr);
TEST_SUCCESS();
}
DEFINE_TEST(calloc)
{
START_TEST(calloc);
int* ptr = calloc(6, sizeof(int));
if (!ptr) { TEST_NOT_SURE(ptr); }
EXPECT_EQ(*ptr, 0);
EXPECT_EQ(ptr[5], 0);
*ptr = 6;
EXPECT_EQ(*ptr, 6);
ptr[5] = 4;
EXPECT_EQ(ptr[5], 4);
free(ptr);
TEST_SUCCESS();
}
DEFINE_TEST(mktemp)
{
START_TEST(mktemp);
char template[] = "/tmp/file.XXXXXX";
const char* template2 = "/tmp/file.XXXXXX";
char* ptr = mktemp(template);
EXPECT_NOT_EQ(ptr, NULL); // mktemp only fails if we give it an invalid template.
int rc = access(ptr, F_OK);
EXPECT_NOT_EQ(rc, 0); // FIXME: This could actually happen, since that's why mktemp is deprecated.
// Another process could create the file between generating the name and actually using it.
rc = strcmp(ptr, template2);
EXPECT_NOT_EQ(rc, 0);
TEST_SUCCESS();
}
static int compare_char(const void* p1, const void* p2)
{
const char* c1 = (const char*)p1;
const char* c2 = (const char*)p2;
if (*c1 < *c2) return -1;
if (*c1 > *c2) return 1;
return 0;
}
static int compare_int(const void* p1, const void* p2)
{
const int* c1 = (const int*)p1;
const int* c2 = (const int*)p2;
if (*c1 < *c2) return -1;
if (*c1 > *c2) return 1;
return 0;
}
DEFINE_TEST(qsort)
{
START_TEST(qsort);
char unsorted[] = "elacbkdjf";
const char sorted[] = "abcdefjkl";
qsort(unsorted, 9, sizeof(char), compare_char);
int rc = memcmp(sorted, unsorted, 9);
EXPECT_EQ(rc, 0);
int unsorted2[] = {1500, 45, 3, 11, 41, 90, 6, 32, 5, 76};
const int sorted2[] = {3, 5, 6, 11, 32, 41, 45, 76, 90, 1500};
qsort(unsorted2, 10, sizeof(int), compare_int);
rc = memcmp(unsorted2, sorted2, 10);
EXPECT_EQ(rc, 0);
TEST_SUCCESS();
}
DEFINE_TEST(bsearch)
{
START_TEST(bsearch);
const char sorted[] = "abcdefjkl";
char key = 'j';
char* ptr = bsearch(&key, sorted, 9, sizeof(char), compare_char); // try with sorted array first
EXPECT_EQ(*ptr, 'j');
EXPECT_EQ(ptr, &sorted[6]);
int unsorted[] = {1500, 45, 3, 11, 41, 90, 6, 32, 5, 76};
qsort(unsorted, 10, sizeof(int), compare_int);
int key2 = 90;
int* ptr2 = bsearch(&key2, unsorted, 10, sizeof(int), compare_int);
EXPECT_EQ(*ptr2, 90);
EXPECT_EQ(ptr2, &unsorted[8]);
TEST_SUCCESS();
}