comparison foosdk/sdk/pfc/selftest.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "pfc-lite.h"
2 #include "pfc.h"
3 #include "string-conv-lite.h"
4 #include "SmartStrStr.h"
5
6 namespace {
7 class foo {};
8 }
9
10 inline pfc::string_base& operator<<(pfc::string_base& p_fmt, foo p_source) { (void)p_source; p_fmt.add_string_("FOO"); return p_fmt; }
11
12 namespace {
13 using namespace pfc;
14 class thread_selftest : public thread {
15 public:
16 void threadProc() {
17 pfc::event ev;
18 ev.wait_for(1);
19 m_event.set_state(true);
20 ev.wait_for(1);
21 }
22 pfc::event m_event;
23
24 void selftest() {
25 lores_timer timer; timer.start();
26 this->start(arg_t());
27 if (!m_event.wait_for(-1)) {
28 PFC_ASSERT(!"Should not get here");
29 return;
30 }
31 PFC_ASSERT(fabs(timer.query() - 1.0) < 0.1);
32 this->waitTillDone();
33 PFC_ASSERT(fabs(timer.query() - 2.0) < 0.1);
34 }
35 };
36 }
37
38 namespace pfc {
39
40 static pfc::string8 testFormatter() {
41 return PFC_string_formatter() << "foo" << 1 << 2 << 3;
42 }
43
44
45 // Self test routines that need to be executed to do their payload
46 void selftest_runtime() {
47
48 {
49 string test = "foo123";
50 auto wide = pfc::wideFromUTF8(test);
51 auto narrow = pfc::utf8FromWide(wide);
52 PFC_ASSERT(test == narrow);
53 }
54 {
55 auto fmt = testFormatter();
56 PFC_ASSERT(fmt == "foo123");
57 }
58 {
59 auto fmt = pfc::format("foo", 1, 2, 3);
60 PFC_ASSERT(fmt == "foo123");
61 }
62
63
64 #if 0 // misfires occassionally, don't run it each time
65 {
66 thread_selftest t; t.selftest();
67 }
68 #endif
69
70 {
71 stringLite s = "foo";
72 PFC_ASSERT( (s + 2) == (s.c_str() + 2) );
73 }
74
75 {
76 stringLite s = "foo";
77 s.insert_chars(2, "00");
78 PFC_ASSERT( strcmp(s, "fo00o" ) == 0 );
79 s.remove_chars(2, 2);
80 PFC_ASSERT( strcmp(s, "foo" ) == 0);
81 }
82
83 {
84 pfc::map_t<pfc::string8, int> map;
85 map["1"] = 1;
86 map["2"] = 2;
87 map["3"] = 3;
88 PFC_ASSERT(map.get_count() == 3);
89 PFC_ASSERT(map["1"] == 1);
90 PFC_ASSERT(map["2"] == 2);
91 PFC_ASSERT(map["3"] == 3);
92 }
93 }
94 // Self test routines that fail at compile time if there's something seriously wrong
95 void selftest_static() {
96 PFC_STATIC_ASSERT(sizeof(t_uint8) == 1);
97 PFC_STATIC_ASSERT(sizeof(t_uint16) == 2);
98 PFC_STATIC_ASSERT(sizeof(t_uint32) == 4);
99 PFC_STATIC_ASSERT(sizeof(t_uint64) == 8);
100
101 PFC_STATIC_ASSERT(sizeof(t_int8) == 1);
102 PFC_STATIC_ASSERT(sizeof(t_int16) == 2);
103 PFC_STATIC_ASSERT(sizeof(t_int32) == 4);
104 PFC_STATIC_ASSERT(sizeof(t_int64) == 8);
105
106 PFC_STATIC_ASSERT(sizeof(t_float32) == 4);
107 PFC_STATIC_ASSERT(sizeof(t_float64) == 8);
108
109 PFC_STATIC_ASSERT(sizeof(t_size) == sizeof(void*));
110 PFC_STATIC_ASSERT(sizeof(t_ssize) == sizeof(void*));
111
112 PFC_STATIC_ASSERT(sizeof(wchar_t) == 2 || sizeof(wchar_t) == 4);
113
114 PFC_STATIC_ASSERT(sizeof(GUID) == 16);
115
116 typedef pfc::avltree_t<int> t_asdf;
117 t_asdf asdf; asdf.add_item(1);
118 t_asdf::iterator iter = asdf._first_var();
119 t_asdf::const_iterator iter2 = asdf._first_var();
120
121 PFC_string_formatter() << "foo" << 1337 << foo();
122
123 pfc::list_t<int> l; l.add_item(3);
124 }
125
126 void selftest() {
127 selftest_static(); selftest_runtime();
128
129 debugLog out; out << "PFC selftest OK";
130 }
131 }