diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-26 10:30:07 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-26 10:30:07 -0400 |
| commit | 190596500eaf3f353fb2d6432c5b48a39cdf9282 (patch) | |
| tree | 60148d6e1c59ec886ead142368e5abbceb979932 | |
| parent | 8bab21d24ece666a9af89efbd461b722a4a7f1b2 (diff) | |
pkuuid: add == and !=
| -rw-r--r-- | pkuuid.h | 12 | ||||
| -rw-r--r-- | test/pkuuid.c | 15 | ||||
| -rw-r--r-- | test/pkuuid.cpp | 15 |
3 files changed, 42 insertions, 0 deletions
@@ -18,12 +18,15 @@ void pk_uuid_init(time_t srand_seed); void pk_uuid_teardown(); struct pk_uuid pk_uuid_new_v7(); +bool pk_uuid_equals(struct pk_uuid lhs, struct pk_uuid rhs); #if defined(__cplusplus) #include <ostream> #include <iomanip> std::ostream& operator<<(std::ostream &o, const struct pk_uuid& uuid); std::istream& operator>>(std::istream &i, struct pk_uuid& uuid); +bool operator==(const pk_uuid &lhs, const pk_uuid &rhs) { return pk_uuid_equals(lhs, rhs); } +bool operator!=(const pk_uuid &lhs, const pk_uuid &rhs) { return !pk_uuid_equals(lhs, rhs); } #endif #endif /* PK_UUID_H */ @@ -135,6 +138,15 @@ pk_uuid_new_v7() return ret; } +bool pk_uuid_equals(struct pk_uuid lhs, struct pk_uuid rhs) +{ + int i; + for (i = 0; i < 16; ++i) { + if (lhs.uuid[i] != rhs.uuid[i]) return false; + } + return true; +} + #if defined(__cplusplus) #include "./pkstn.h" /* deleteme */ std::ostream& diff --git a/test/pkuuid.c b/test/pkuuid.c index ee550cf..42cf3e4 100644 --- a/test/pkuuid.c +++ b/test/pkuuid.c @@ -77,6 +77,21 @@ main(int argc, char *argv[]) } { + struct pk_uuid id1, id2; + bool equals; + id1 = pk_uuid_zed; + id2 = pk_uuid_zed; + equals = pk_uuid_equals(id1, id2); + fprintf(stdout, "[%s] equals (zed, zed): %b\n", __FILE__, equals); + if (equals != true) exit(1); + id1 = pk_uuid_new_v7(); + id2 = pk_uuid_new_v7(); + equals = pk_uuid_equals(id1, id2); + fprintf(stdout, "[%s] equals (rand, rand): %b\n", __FILE__, equals); + if (equals != false) exit(1); + } + + { const int count = 4; struct pk_uuid ids[count]; diff --git a/test/pkuuid.cpp b/test/pkuuid.cpp index 278cd02..da59264 100644 --- a/test/pkuuid.cpp +++ b/test/pkuuid.cpp @@ -80,6 +80,21 @@ main(int argc, char *argv[]) } { + struct pk_uuid id1, id2; + bool equals; + id1 = pk_uuid_zed; + id2 = pk_uuid_zed; + equals = id1 == id2; + fprintf(stdout, "[%s] equals (zed, zed): %b\n", __FILE__, equals); + if (equals != true) exit(1); + id1 = pk_uuid_new_v7(); + id2 = pk_uuid_new_v7(); + equals = id1 == id2; + fprintf(stdout, "[%s] equals (rand, rand): %b\n", __FILE__, equals); + if (equals != false) exit(1); + } + + { const int count = 4; struct pk_uuid ids[count]; |
