LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
test_string.cxx
Go to the documentation of this file.
1#include <lfostest.h>
2
3namespace LFOS {
4
5 #include <string.c>
6
7 #define __kernel 1
8 #include <uuid.h>
9 #include <sys/known_services.h>
10 #include "../uuid.c"
11
12 TEST(KernelString, memcmp) {
13 uint8_t data_A[] = { 42, 23, 42, 13, 37, 0, 12, 34, 0, 56 };
14 uint8_t data_B[] = { 42, 23, 42, 13, 37, 0, 12, 33, 0, 56 };
15 uint8_t data_C[] = { 42, 23, 42, 13, 37, 0, 12, 35, 0, 56 };
16
17 EXPECT_EQ(memcmp(data_A, data_A, 10), 0) << "memcmp returns zero correctly";
18 EXPECT_GT(memcmp(data_A, data_B, 10), 0) << "memcmp returns positive correctly";
19 EXPECT_LT(memcmp(data_A, data_C, 10), 0) << "memcmp returns negative correctly";
20 }
21
22 TEST(KernelString, strlen) {
23 EXPECT_EQ(strlen("Hello, world!"), 13) << "simple strlen correct";
24 EXPECT_EQ(strlen("\t\t"), 2) << "control characters strlen correct";
25 EXPECT_EQ(strlen("\0"), 0) << "double empty strlen correct";
26 EXPECT_EQ(strlen(0), 0) << "null pointer strlen correct";
27 }
28
29 TEST(KernelString, strcmp) {
30 EXPECT_LT(strcmp("bar", "foo"), 0) << "strcmp returns negative correctly";
31 EXPECT_GT(strcmp("foo", "bar"), 0) << "strcmp returns positive correctly";
32 EXPECT_EQ(strcmp("foo", "foo"), 0) << "strcmp returns zero correctly";
33 }
34
35 TEST(KernelString, strcasecmp) {
36 EXPECT_LT(strcasecmp("bar", "foo"), 0) << "strcasecmp returns negative correctly";
37 EXPECT_GT(strcasecmp("foo", "bar"), 0) << "strcasecmp returns positive correctly";
38 EXPECT_EQ(strcasecmp("foo", "foo"), 0) << "strcasecmp returns zero correctly";
39 EXPECT_EQ(strcasecmp("FoO", "foo"), 0) << "strcasecmp returns zero correctly";
40 }
41
42 class SputFamilyTest : public ::testing::Test {
43 public:
45 memset(_buffer, 0, sizeof(_buffer));
46 }
47
48 protected:
49 char _buffer[20];
50 };
51
53 EXPECT_EQ(sputs(_buffer, 20, "Hello, world!", 13), 13) << "sputs return correct";
54 EXPECT_STREQ(_buffer, "Hello, world!") << "sputs modified buffer correctly";
55 }
56
58 EXPECT_EQ(sputi(_buffer, 20, 42, 10), 2) << "sputi return correct";
59 EXPECT_STREQ(_buffer, "42") << "sputi modified buffer correctly";
60 }
61
63 EXPECT_EQ(sputui(_buffer, 20, 23, 10), 2) << "sputui return correct";
64 EXPECT_STREQ(_buffer, "23") << "sputui modified buffer correctly";
65 }
66
68 EXPECT_EQ(sputbytes(_buffer, 20, 4096), 4) << "sputbytes return correct";
69 EXPECT_STREQ(_buffer, "4KiB") << "sputbytes modified buffer correctly";
70 }
71
72 TEST(ksnprintf, UUID) {
73 char* buffer = new char[64];
74 size_t len = uuid_fmt(buffer, 64, (uuid_t*)&FileSystemDriverUUID);
75
76 EXPECT_EQ(len, 36) << "correct length";
77 EXPECT_STREQ(buffer, "74347BC9-1DE4-4D8B-BFC9-1718F5D0AA6A") << "UUID formatted correctly";
78
79 delete[] buffer;
80 }
81
82 TEST(ksnprintf, max_len_return) {
83 EXPECT_EQ(ksnprintf(0, 0, "Hello world!"), 12) << "format string only";
84 EXPECT_EQ(ksnprintf(0, 0, "What the %d", 42), 11) << "with a %d placeholder";
85 EXPECT_EQ(ksnprintf(0, 0, "What the %x", 42), 11) << "with a %x placeholder";
86 EXPECT_EQ(ksnprintf(0, 0, "What the %s", "fluff"), 14) << "with a %s placeholder";
87 EXPECT_EQ(ksnprintf(0, 0, "What the %B", 1024*1024), 13) << "with a %B placeholder";
88 }
89
90 TEST(ksnprintf, null_terminating) {
91 char buffer[5];
92 buffer[4] = 42;
93 EXPECT_EQ(ksnprintf(buffer, sizeof(buffer) - 1, "Test %s", "fooooo"), 11) << "returns correct number of characters to write";
94 EXPECT_EQ(buffer[3], 0) << "correctly early-terminates output";
95 EXPECT_EQ(buffer[4], 42) << "not writing outside given buffer size";
96 }
97
98 TEST(ksnprintf, random_actually_used_format_string) {
99 char message[256];
100 ksnprintf(message, 256, "Interrupt: 0x%02x (%s), error: 0x%04x%s", 0x0D, "General protection fault", 0, "");
101 EXPECT_STREQ(message, "Interrupt: 0x0D (General protection fault), error: 0x0000");
102 }
103}
unsigned char uint8_t
Definition arch.h:5
size_t strlen(const char *str)
Definition string.c:30
int strcmp(const char *s1, const char *s2)
Definition string.c:7
int sputui(char *buffer, int buffer_size, uint64_t number, int base)
Definition string.c:143
int sputbytes(char *buffer, int buffer_size, int64_t number)
Definition string.c:190
void * memset(void *dest, int c, size_t size)
Definition string.c:72
size_t ksnprintf(char *buffer, size_t buffer_size, const char *format,...)
Definition string.c:339
int strcasecmp(const char *s1, const char *s2)
Definition string.c:21
int sputi(char *buffer, int buffer_size, int64_t number, int base)
Definition string.c:163
int memcmp(const void *s1, const void *s2, size_t size)
Definition string.c:91
int sputs(char *buffer, int buffer_size, char *string, int length)
Definition string.c:121
static const uuid_t FileSystemDriverUUID
TEST(KernelServiceDiscovery, Basic)
Definition test_sd.cxx:20
TEST_F(MessageQueueTest, PopFromEmptyQueue)
Definition test_mq.cxx:39
static bool struct Message * message
Definition syscalls.h:338
size_t uuid_fmt(char *buffer, size_t len, uuid_t *uuid)
Definition uuid.c:18
Definition uuid.h:6