cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
test_base.c
1#include "test_harness.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, utilities, compiler abstractions, and data structures.
Sized string (string slice) definitions and helper macros.
Core type definitions and fixed-width aliases.
#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 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 a substring by skipping the first amt bytes of the input string.
static String8 str8_substr(String8 str, usize first, usize one_past_last)
Returns a substring spanning from first to one_past_last indices.
static String8 str8_prefix(String8 str, usize size)
Returns a substring containing the first size bytes of the input string.
#define STR8(s)
Wraps a C string literal into a String8 struct 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
A lightweight, dependency-free unit testing harness.
#define EXPECT_STR8_EQ(a, b)
Helper to test if two String8 objects match exactly.
#define EXPECT(cond)
Evaluates a condition. If it fails, prints the file/line and increments the fail count.
#define TEST_CASE(name)
Defines a test case function.