| 8 | #include "fl/stl/thread.h" |
| 9 | |
| 10 | FL_TEST_FILE(FL_FILEPATH) { |
| 11 | #endif |
| 12 | |
| 13 | |
| 14 | FL_TEST_CASE("ThreadLocal - basic functionality") { |
| 15 | fl::ThreadLocal<int> tls; |
| 16 | |
| 17 | // Default constructed value should be 0 |
| 18 | FL_REQUIRE(tls.access() == 0); |
| 19 | |
| 20 | // Set a value |
| 21 | tls.set(42); |
| 22 | FL_REQUIRE(tls.access() == 42); |
| 23 | |
| 24 | // Use assignment operator |
| 25 | tls = 100; |
| 26 | FL_REQUIRE(tls.access() == 100); |
| 27 | |
| 28 | // Use conversion operator |
| 29 | int value = tls; |
| 30 | FL_REQUIRE(value == 100); |
| 31 | } |
| 32 | |
| 33 | FL_TEST_CASE("ThreadLocal - with default value") { |
| 34 | fl::ThreadLocal<int> tls(999); |
| 35 | |
| 36 | // Should start with default value |
| 37 | FL_REQUIRE(tls.access() == 999); |
| 38 | |
| 39 | // Set a different value |
| 40 | tls.set(123); |
| 41 | FL_REQUIRE(tls.access() == 123); |
| 42 | } |
| 43 | |
| 44 | FL_TEST_CASE("ThreadLocal - with custom type") { |
| 45 | struct TestStruct { |
| 46 | int value = 0; |
| 47 | fl::string name = "default"; |
| 48 | |
| 49 | TestStruct() = default; |
| 50 | TestStruct(int v, const fl::string& n) : value(v), name(n) {} |
| 51 | |
| 52 | bool operator==(const TestStruct& other) const { |
| 53 | return value == other.value && name == other.name; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | fl::ThreadLocal<TestStruct> tls; |
| 58 | |
| 59 | // Default constructed |
| 60 | FL_REQUIRE(tls.access().value == 0); |
| 61 | FL_REQUIRE(tls.access().name == "default"); |
| 62 | |
| 63 | // Set a value |
| 64 | TestStruct custom(42, "test"); |
| 65 | tls.set(custom); |
| 66 | FL_REQUIRE(tls.access() == custom); |
| 67 | |