cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
test_base.c
1#include "test/test.h"
2
3#include "base/base_macros.h"
4#include "base/base_strings.h"
5#include "base/base_types.h"
6
8typedef struct TestNode {
9 struct TestNode *next;
10 struct TestNode *prev;
11 int val;
12} TestNode;
13
14TEST_CASE(macros_math)
15{
16 EXPECT(MIN(5, 10) == 5);
17 EXPECT(MAX(5, 10) == 10);
18 EXPECT(CLAMP(15, 0, 10) == 10);
19 EXPECT(ALIGN_UP_POW2(10, 8) == 16);
20 EXPECT(IS_POW2_OR_ZERO(16) == 1);
21 EXPECT(IS_POW2_OR_ZERO(15) == 0);
22}
23
24TEST_CASE(macros_linked_list)
25{
26 TestNode *head = NULL, *tail = NULL;
27 TestNode n1 = {.val = 1};
28 TestNode n2 = {.val = 2};
29
30 DLL_PUSH_BACK(head, tail, &n1);
31 EXPECT(head == &n1 && tail == &n1);
32
33 DLL_PUSH_BACK(head, tail, &n2);
34 EXPECT(head == &n1 && tail == &n2);
35 EXPECT(n1.next == &n2);
36 EXPECT(n2.prev == &n1);
37
38 DLL_REMOVE(head, tail, &n1);
39 EXPECT(head == &n2 && tail == &n2);
40}
41
42TEST_CASE(strings_operations)
43{
44 String8 hw = STR8("Hello World");
45
46 EXPECT(hw.size == 11);
47
48 String8 prefix = str8_prefix(hw, 5);
49 EXPECT_STR8_EQ(prefix, STR8("Hello"));
50
51 String8 skipped = str8_skip(hw, 6);
52 EXPECT_STR8_EQ(skipped, STR8("World"));
53
54 String8 sub = str8_substr(hw, 1, 5);
55 EXPECT_STR8_EQ(sub, STR8("ello"));
56
57 // Safety checking (out of bounds clamp)
58 String8 safe = str8_prefix(hw, 100);
59 EXPECT(safe.size == 11);
60}
Context detection, compiler abstractions, utility macros, and data structures.
Sized UTF-8 string slices (String8) and manipulation utilities.
Core fixed-width type aliases and memory size constants.
#define IS_POW2_OR_ZERO(x)
Checks if a given integer is a power of 2 or zero.
#define ALIGN_UP_POW2(x, p)
Aligns a value UP to the nearest multiple of p (must be power of 2).
#define DLL_PUSH_BACK(f, l, n)
Pushes a node to the back of a doubly linked list.
#define DLL_REMOVE(f, l, n)
Removes a node from a doubly linked list.
static String8 str8_skip(String8 str, usize amt)
Returns str with the first amt bytes removed.
static String8 str8_substr(String8 str, usize first, usize one_past_last)
Returns the substring from byte index first to one_past_last.
static String8 str8_prefix(String8 str, usize size)
Returns the first size bytes of str.
#define STR8(s)
Wraps a C string literal into a String8 at compile time.
#define MIN(a, b)
Returns the minimum of two values (Standard C fallback).
#define CLAMP(val, min, max)
Clamps a value between a minimum and a maximum (Standard C fallback).
#define MAX(a, b)
Returns the maximum of two values (Standard C fallback).
A sized UTF-8 string slice.
usize size
Dummy node used for testing intrusive linked list macros.
Definition test_base.c:8
struct TestNode * prev
Definition test_base.c:10
int val
Definition test_base.c:11
struct TestNode * next
Definition test_base.c:9
Single-header testing framework with pretty-printed output.
#define EXPECT_STR8_EQ(a, b)
Asserts two String8 values are exactly equal.
Definition test.h:193
#define EXPECT(cond)
Evaluates a condition. Records and counts the failure if false.
Definition test.h:161
#define TEST_CASE(name)
Declares a test case function.
Definition test.h:205