2022-10-22 10:03:10 +00:00
|
|
|
#include "Test.h"
|
|
|
|
#include <stdlib.h>
|
2022-11-05 17:07:45 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2022-10-22 10:03:10 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-10-22 10:10:19 +00:00
|
|
|
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);
|
|
|
|
|
2022-10-22 16:06:01 +00:00
|
|
|
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);
|
|
|
|
|
2022-11-05 17:07:45 +00:00
|
|
|
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);
|
|
|
|
|
2022-11-06 16:16:08 +00:00
|
|
|
EXPECT_NOT_EQ(rc, 0); // FIXME: This could actually happen, since that's why mktemp is deprecated.
|
2022-11-05 17:07:45 +00:00
|
|
|
// Another process could create the file between generating the name and actually using it.
|
|
|
|
|
|
|
|
rc = strcmp(ptr, template2);
|
|
|
|
|
|
|
|
EXPECT_NOT_EQ(rc, 0);
|
|
|
|
|
2022-11-06 16:16:08 +00:00
|
|
|
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);
|
|
|
|
|
2022-11-06 16:34:35 +00:00
|
|
|
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]);
|
|
|
|
|
2022-10-22 10:03:10 +00:00
|
|
|
TEST_SUCCESS();
|
|
|
|
}
|