diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-19 18:15:52 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-20 12:01:56 -0400 |
| commit | 9cd55867de91013bdbfe0d73112df504eb7963ba (patch) | |
| tree | 35c7d25805c10a61dd65bd80febedb81e82c50bf /test | |
| parent | bc7e78b1766e8d9c9af1d47c207d084e161db98d (diff) | |
pkuuid.h: first-pass + bump version to 0.4.1
Diffstat (limited to 'test')
| -rw-r--r-- | test/pkuuid.c | 165 | ||||
| -rw-r--r-- | test/pkuuid.cpp | 169 |
2 files changed, 334 insertions, 0 deletions
diff --git a/test/pkuuid.c b/test/pkuuid.c new file mode 100644 index 0000000..ee550cf --- /dev/null +++ b/test/pkuuid.c @@ -0,0 +1,165 @@ + +#define PK_IMPL_UUID +#define PK_IMPL_TMR +// #define PK_UUID_CLOCK CLOCK_REALTIME_ALARM + +#include "../pkmacros.h" +#include "../pkuuid.h" + +#include "../pktmr.h" + +#include <stdio.h> + +void +print_uuid_v7_breakdown(FILE *f, struct pk_uuid id) +{ + fprintf(f, "-------------------------------------------\n"); + fprintf(f, "field bits value\n"); + fprintf(f, "-------------------------------------------\n"); + fprintf(f, "unix_ts_ms 48 0x"); + fprintf(f, "%.2x", id.uuid[0]); + fprintf(f, "%.2x", id.uuid[1]); + fprintf(f, "%.2x", id.uuid[2]); + fprintf(f, "%.2x", id.uuid[3]); + fprintf(f, "%.2x", id.uuid[4]); + fprintf(f, "%.2x", id.uuid[5]); + fprintf(f, "\n"); + + fprintf(f, "ver 4 0x"); + fprintf(f, "%.x", (id.uuid[6] & 0xF0) >> 4); + fprintf(f, "\n"); + + fprintf(f, "rand_a 12 0x"); + fprintf(f, "%.1x", (id.uuid[6] & 0x0F)); + fprintf(f, "%.2x", id.uuid[7]); + fprintf(f, "\n"); + + fprintf(f, "var 2 0b"); + fprintf(f, "%.c", (id.uuid[8] & 0x80) ? '1' : '0'); + fprintf(f, "%.c", (id.uuid[8] & 0x40) ? '1' : '0'); + fprintf(f, "\n"); + + fprintf(f, "rand_b 62 0b"); + fprintf(f, "%.c", (id.uuid[8] & 0x20) ? '1' : '0'); + fprintf(f, "%.c", (id.uuid[8] & 0x10) ? '1' : '0'); + fprintf(f, ", 0x"); + fprintf(f, "%.x", (id.uuid[8] & 0x0F)); + fprintf(f, "%.2x", id.uuid[9]); + fprintf(f, "%.2x", id.uuid[10]); + fprintf(f, "%.2x", id.uuid[11]); + fprintf(f, "%.2x", id.uuid[12]); + fprintf(f, "%.2x", id.uuid[13]); + fprintf(f, "%.2x", id.uuid[14]); + fprintf(f, "%.2x", id.uuid[15]); + fprintf(f, "\n"); + + fprintf(f, "-------------------------------------------\n"); +} + +int +main(int argc, char *argv[]) +{ + (void)argc; + (void)argv; + pk_uuid_init(time(NULL)); + + fprintf(stdout, "\n"); + { + struct pk_uuid id; + id = pk_uuid_zed; + fprintf(stdout, "[%s] zed: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(id)); + } + + { + struct pk_uuid id; + id = pk_uuid_max; + fprintf(stdout, "[%s] one: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(id)); + } + + { + const int count = 4; + struct pk_uuid ids[count]; + + for (int i = 0; i < count; ++i) { + ids[i] = pk_uuid_new_v7(); + } + for (int i = 0; i < count; ++i) { + fprintf(stdout, "[%s] new: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(ids[i])); + } + + for (int i = 0; i < count; ++i) { + fprintf(stdout, "\n"); + print_uuid_v7_breakdown(stdout, ids[i]); + fprintf(stdout, "[%s] new: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(ids[i])); + } + fprintf(stdout, "\n"); + } + + { + double ms; + const int count = 1000; + struct pk_uuid ids[count]; + struct pk_tmr tmr; + pk_tmr_start(tmr); + for (int i = 0; i < count; ++i) { + ids[i] = pk_uuid_new_v7(); + } + pk_tmr_stop(tmr); + fprintf(stdout, "generated %i ids...\n", count); + ms = pk_tmr_duration_dbl_mili(tmr); + fprintf(stdout, "elapsed time : %f ms\n", ms); + fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); + } + + // not really a test, just checking time + /* 2025-03-19 JCB - Disabled because we added a macro to define the clock + * instead of passing it as a parameter. + { + double ms; + const int count = 1000; + struct pk_uuid ids[count]; + struct pk_tmr tmr; + pk_tmr_start(tmr); + for (int i = 0; i < count; ++i) { + ids[i] = pk_uuid_new_v7(CLOCK_REALTIME); + } + pk_tmr_stop(tmr); + fprintf(stdout, "generated %i ids using CLOCK_REALTIME...\n", count); + ms = pk_tmr_duration_dbl_mili(tmr); + fprintf(stdout, "elapsed time : %f ms\n", ms); + fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); + + pk_tmr_start(tmr); + for (int i = 0; i < count; ++i) { + ids[i] = pk_uuid_new_v7(CLOCK_REALTIME_COARSE); + } + pk_tmr_stop(tmr); + fprintf(stdout, "generated %i ids using CLOCK_REALTIME_COARSE...\n", count); + ms = pk_tmr_duration_dbl_mili(tmr); + fprintf(stdout, "elapsed time : %f ms\n", ms); + fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); + + pk_tmr_start(tmr); + for (int i = 0; i < count; ++i) { + ids[i] = pk_uuid_new_v7(CLOCK_TAI); + } + pk_tmr_stop(tmr); + fprintf(stdout, "generated %i ids using CLOCK_TAI...\n", count); + ms = pk_tmr_duration_dbl_mili(tmr); + fprintf(stdout, "elapsed time : %f ms\n", ms); + fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); + } + */ + + pk_uuid_teardown(); + + return 0; +} diff --git a/test/pkuuid.cpp b/test/pkuuid.cpp new file mode 100644 index 0000000..278cd02 --- /dev/null +++ b/test/pkuuid.cpp @@ -0,0 +1,169 @@ + +#define PK_IMPL_UUID +#define PK_IMPL_TMR +// #define PK_UUID_CLOCK CLOCK_REALTIME_ALARM + +#include "../pkmacros.h" +#include "../pkuuid.h" + +#include "../pktmr.h" + +#include <iostream> +#include <sstream> +#include <stdio.h> + +void +print_uuid_v7_breakdown(FILE *f, struct pk_uuid id) +{ + fprintf(f, "-------------------------------------------\n"); + fprintf(f, "field bits value\n"); + fprintf(f, "-------------------------------------------\n"); + fprintf(f, "unix_ts_ms 48 0x"); + fprintf(f, "%.2x", id.uuid[0]); + fprintf(f, "%.2x", id.uuid[1]); + fprintf(f, "%.2x", id.uuid[2]); + fprintf(f, "%.2x", id.uuid[3]); + fprintf(f, "%.2x", id.uuid[4]); + fprintf(f, "%.2x", id.uuid[5]); + fprintf(f, "\n"); + + fprintf(f, "ver 4 0x"); + fprintf(f, "%.x", (id.uuid[6] & 0xF0) >> 4); + fprintf(f, "\n"); + + fprintf(f, "rand_a 12 0x"); + fprintf(f, "%.1x", (id.uuid[6] & 0x0F)); + fprintf(f, "%.2x", id.uuid[7]); + fprintf(f, "\n"); + + fprintf(f, "var 2 0b"); + fprintf(f, "%.c", (id.uuid[8] & 0x80) ? '1' : '0'); + fprintf(f, "%.c", (id.uuid[8] & 0x40) ? '1' : '0'); + fprintf(f, "\n"); + + fprintf(f, "rand_b 62 0b"); + fprintf(f, "%.c", (id.uuid[8] & 0x20) ? '1' : '0'); + fprintf(f, "%.c", (id.uuid[8] & 0x10) ? '1' : '0'); + fprintf(f, ", 0x"); + fprintf(f, "%.x", (id.uuid[8] & 0x0F)); + fprintf(f, "%.2x", id.uuid[9]); + fprintf(f, "%.2x", id.uuid[10]); + fprintf(f, "%.2x", id.uuid[11]); + fprintf(f, "%.2x", id.uuid[12]); + fprintf(f, "%.2x", id.uuid[13]); + fprintf(f, "%.2x", id.uuid[14]); + fprintf(f, "%.2x", id.uuid[15]); + fprintf(f, "\n"); + + fprintf(f, "-------------------------------------------\n"); +} + +int +main(int argc, char *argv[]) +{ + (void)argc; + (void)argv; + pk_uuid_init(time(NULL)); + const struct pk_uuid example_id = { .uuid = { 0x01, 0x7F, 0x22, 0xE2, 0x79, 0xB0, 0x7C, 0xC3, 0x98, 0xC4, 0xDC, 0x0C, 0x0C, 0x07, 0x39, 0x8F } }; + + fprintf(stdout, "\n"); + { + struct pk_uuid id; + id = pk_uuid_zed; + fprintf(stdout, "[%s] zed: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(id)); + } + + { + struct pk_uuid id; + id = pk_uuid_max; + fprintf(stdout, "[%s] one: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(id)); + } + + { + const int count = 4; + struct pk_uuid ids[count]; + + for (int i = 0; i < count; ++i) { + ids[i] = pk_uuid_new_v7(); + } + for (int i = 0; i < count; ++i) { + fprintf(stdout, "[%s] new: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(ids[i])); + } + + for (int i = 0; i < count; ++i) { + fprintf(stdout, "\n"); + print_uuid_v7_breakdown(stdout, ids[i]); + fprintf(stdout, "[%s] new: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(ids[i])); + } + fprintf(stdout, "\n"); + } + + { + double ms; + const int count = 1000; + struct pk_uuid ids[count]; + struct pk_tmr tmr; + pk_tmr_start(tmr); + for (int i = 0; i < count; ++i) { + ids[i] = pk_uuid_new_v7(); + } + pk_tmr_stop(tmr); + fprintf(stdout, "generated %i ids...\n", count); + ms = pk_tmr_duration_dbl_mili(tmr); + fprintf(stdout, "elapsed time : %f ms\n", ms); + fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); + fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); + } + + // << + { + std::stringstream ss; + ss << example_id; + + fprintf(stdout, "\n"); + fprintf(stdout, "[%s] expected: 017f22e2-79b0-7cc3-98c4-dc0c0c07398f\n", __FILE__); + fprintf(stdout, "[%s] result : %s\n", __FILE__, ss.str().c_str()); + if (ss.str() != "017f22e2-79b0-7cc3-98c4-dc0c0c07398f") return 1; + } + + // >> + { + struct pk_uuid id; + std::istringstream ssi("017f22e2-79b0-7cc3-98c4-dc0c0c07398f"); + + ssi >> id; + ssi >> std::cout.rdbuf(); + + fprintf(stdout, "\n"); + fprintf(stdout, "[%s] excpected: 017f22e2-79b0-7cc3-98c4-dc0c0c07398f\n", __FILE__); + fprintf(stdout, "[%s] result : " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(id)); + + for (int i = 0; i < 16; ++i) { + if (example_id.uuid[i] != id.uuid[i]) return 1; + } + } + + // >> err + { + struct pk_uuid id; + std::istringstream ssi("gnud"); + + ssi >> id; + + fprintf(stdout, "\n"); + fprintf(stdout, "[%s] leftovers: ", __FILE__); + ssi >> std::cout.rdbuf(); + std::cout << std::endl; + fprintf(stdout, "[%s] excpected: 00000000-0000-0000-0000-000000000000\n", __FILE__); + fprintf(stdout, "[%s] result : " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(id)); + + for (int i = 0; i < 16; ++i) { + if (pk_uuid_zed.uuid[i] != id.uuid[i]) return 1; + } + } + + pk_uuid_teardown(); + + return 0; +} |
