|
| 1 | +const std = @import("std"); |
| 2 | +const async_cache = @import("async_cache.zig"); |
| 3 | +const xl_imports = @import("xl_imports.zig"); |
| 4 | +const xl = xl_imports.xl; |
| 5 | + |
| 6 | +const allocator = std.heap.c_allocator; |
| 7 | + |
| 8 | +fn makeNumericXloper(val: f64) *xl.XLOPER12 { |
| 9 | + const ptr = allocator.create(xl.XLOPER12) catch unreachable; |
| 10 | + ptr.* = .{ |
| 11 | + .xltype = xl.xltypeNum | xl.xlbitDLLFree, |
| 12 | + .val = .{ .num = val }, |
| 13 | + }; |
| 14 | + return ptr; |
| 15 | +} |
| 16 | + |
| 17 | +fn freeXloper(ptr: *xl.XLOPER12) void { |
| 18 | + allocator.destroy(ptr); |
| 19 | +} |
| 20 | + |
| 21 | +test "cache miss returns null" { |
| 22 | + var cache = async_cache.AsyncCache.init(); |
| 23 | + defer cache.clear(); |
| 24 | + |
| 25 | + try std.testing.expect(cache.get("nonexistent") == null); |
| 26 | +} |
| 27 | + |
| 28 | +test "put and get" { |
| 29 | + var cache = async_cache.AsyncCache.init(); |
| 30 | + defer cache.clear(); |
| 31 | + |
| 32 | + const xloper = makeNumericXloper(42.0); |
| 33 | + defer freeXloper(xloper); |
| 34 | + |
| 35 | + cache.put("key1", .{ .xloper = xloper, .completed = true }); |
| 36 | + |
| 37 | + const result = cache.get("key1"); |
| 38 | + try std.testing.expect(result != null); |
| 39 | + try std.testing.expect(result.?.completed); |
| 40 | + try std.testing.expectEqual(42.0, result.?.xloper.val.num); |
| 41 | +} |
| 42 | + |
| 43 | +test "put overwrites existing key" { |
| 44 | + var cache = async_cache.AsyncCache.init(); |
| 45 | + defer cache.clear(); |
| 46 | + |
| 47 | + const xloper1 = makeNumericXloper(1.0); |
| 48 | + defer freeXloper(xloper1); |
| 49 | + const xloper2 = makeNumericXloper(2.0); |
| 50 | + defer freeXloper(xloper2); |
| 51 | + |
| 52 | + cache.put("key", .{ .xloper = xloper1, .completed = false }); |
| 53 | + cache.put("key", .{ .xloper = xloper2, .completed = true }); |
| 54 | + |
| 55 | + const result = cache.get("key"); |
| 56 | + try std.testing.expect(result != null); |
| 57 | + try std.testing.expect(result.?.completed); |
| 58 | + try std.testing.expectEqual(2.0, result.?.xloper.val.num); |
| 59 | +} |
| 60 | + |
| 61 | +test "contains" { |
| 62 | + var cache = async_cache.AsyncCache.init(); |
| 63 | + defer cache.clear(); |
| 64 | + |
| 65 | + const xloper = makeNumericXloper(0.0); |
| 66 | + defer freeXloper(xloper); |
| 67 | + |
| 68 | + try std.testing.expect(!cache.contains("key")); |
| 69 | + cache.put("key", .{ .xloper = xloper, .completed = false }); |
| 70 | + try std.testing.expect(cache.contains("key")); |
| 71 | +} |
| 72 | + |
| 73 | +test "clear removes all entries" { |
| 74 | + var cache = async_cache.AsyncCache.init(); |
| 75 | + |
| 76 | + const xloper1 = makeNumericXloper(1.0); |
| 77 | + defer freeXloper(xloper1); |
| 78 | + const xloper2 = makeNumericXloper(2.0); |
| 79 | + defer freeXloper(xloper2); |
| 80 | + |
| 81 | + cache.put("a", .{ .xloper = xloper1, .completed = true }); |
| 82 | + cache.put("b", .{ .xloper = xloper2, .completed = true }); |
| 83 | + |
| 84 | + cache.clear(); |
| 85 | + |
| 86 | + try std.testing.expect(cache.get("a") == null); |
| 87 | + try std.testing.expect(cache.get("b") == null); |
| 88 | +} |
| 89 | + |
| 90 | +test "in-progress then completed" { |
| 91 | + var cache = async_cache.AsyncCache.init(); |
| 92 | + defer cache.clear(); |
| 93 | + |
| 94 | + const pending = makeNumericXloper(0.0); |
| 95 | + defer freeXloper(pending); |
| 96 | + const done = makeNumericXloper(99.0); |
| 97 | + defer freeXloper(done); |
| 98 | + |
| 99 | + // Mark in-progress |
| 100 | + cache.put("calc|5", .{ .xloper = pending, .completed = false }); |
| 101 | + const r1 = cache.get("calc|5"); |
| 102 | + try std.testing.expect(!r1.?.completed); |
| 103 | + |
| 104 | + // Complete |
| 105 | + cache.put("calc|5", .{ .xloper = done, .completed = true }); |
| 106 | + const r2 = cache.get("calc|5"); |
| 107 | + try std.testing.expect(r2.?.completed); |
| 108 | + try std.testing.expectEqual(99.0, r2.?.xloper.val.num); |
| 109 | +} |
| 110 | + |
| 111 | +test "concurrent reads and writes" { |
| 112 | + var cache = async_cache.AsyncCache.init(); |
| 113 | + defer cache.clear(); |
| 114 | + |
| 115 | + const xloper = makeNumericXloper(42.0); |
| 116 | + defer freeXloper(xloper); |
| 117 | + cache.put("shared", .{ .xloper = xloper, .completed = true }); |
| 118 | + |
| 119 | + const Reader = struct { |
| 120 | + fn run(c: *async_cache.AsyncCache) void { |
| 121 | + for (0..1000) |_| { |
| 122 | + if (c.get("shared")) |r| { |
| 123 | + std.debug.assert(r.xloper.val.num == 42.0); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + var threads: [4]std.Thread = undefined; |
| 130 | + for (&threads) |*t| { |
| 131 | + t.* = std.Thread.spawn(.{}, Reader.run, .{&cache}) catch unreachable; |
| 132 | + } |
| 133 | + for (&threads) |t| t.join(); |
| 134 | +} |
| 135 | + |
| 136 | +test "global cache singleton" { |
| 137 | + const c1 = async_cache.getGlobalCache(); |
| 138 | + const c2 = async_cache.getGlobalCache(); |
| 139 | + try std.testing.expectEqual(c1, c2); |
| 140 | +} |
0 commit comments