Skip to content

Commit 62a15f1

Browse files
committed
Separate tests
1 parent 08b8a46 commit 62a15f1

File tree

3 files changed

+161
-147
lines changed

3 files changed

+161
-147
lines changed

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn build(b: *std.Build) void {
3333
// Creates a step for unit testing. This only builds the test executable
3434
// but does not run it.
3535
const lib_unit_tests = b.addTest(.{
36-
.root_source_file = b.path("src/mailbox.zig"),
36+
.root_source_file = b.path("src/mailbox_tests.zig"),
3737
.target = target,
3838
.optimize = optimize,
3939
});

src/mailbox.zig

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -152,149 +152,3 @@ pub fn MailBox(comptime Letter: type) type {
152152
};
153153
}
154154
//-----------------------------
155-
156-
//-----------------------------
157-
test {
158-
@import("std").testing.refAllDecls(@This());
159-
}
160-
//-----------------------------
161-
162-
//-----------------------------
163-
test "basic MailBox test" {
164-
const Mbx = MailBox(u32);
165-
var mbox: Mbx = .{};
166-
167-
try testing.expectError(error.Timeout, mbox.receive(10));
168-
169-
var one = Mbx.Envelope{ .letter = 1 };
170-
var two = Mbx.Envelope{ .letter = 2 };
171-
var three = Mbx.Envelope{ .letter = 3 };
172-
var four = Mbx.Envelope{ .letter = 4 };
173-
var five = Mbx.Envelope{ .letter = 5 };
174-
175-
try mbox.send(&one);
176-
try mbox.send(&two);
177-
try mbox.send(&three);
178-
try mbox.send(&four);
179-
try mbox.send(&five);
180-
181-
try testing.expect(mbox.letters() == 5);
182-
183-
for (1..6) |i| {
184-
const recv = mbox.receive(1000);
185-
186-
if (recv) |val| {
187-
try testing.expect(val.letter == i);
188-
} else |_| {
189-
try testing.expect(false);
190-
}
191-
}
192-
193-
try testing.expectError(error.Timeout, mbox.receive(10));
194-
195-
_ = mbox.close();
196-
try testing.expectError(error.Closed, mbox.receive(10));
197-
}
198-
//-----------------------------
199-
200-
//-----------------------------
201-
test "Echo mailboxes test" {
202-
203-
// Mbx is Mailbox with usize letter(data)
204-
const Mbx = MailBox(usize);
205-
206-
// Echo - runs on own thread
207-
// It has two mailboxes
208-
// "TO" and "FROM" - from the client point of the view
209-
// Receives letter via 'TO' mailbox
210-
// Replies letter without change (echo) to "FROM" mailbox
211-
const Echo = struct {
212-
const Self = @This();
213-
214-
to: Mbx = undefined,
215-
from: Mbx = undefined,
216-
thread: Thread = undefined,
217-
218-
// Mailboxes creation and start of the thread
219-
// Pay attention, that client code does not use
220-
// any thread "API" - all embedded within Echo
221-
pub fn start(echo: *Self) void {
222-
echo.to = .{};
223-
echo.from = .{};
224-
echo.thread = std.Thread.spawn(.{}, run, .{echo}) catch unreachable;
225-
}
226-
227-
// Echo thread function
228-
fn run(echo: *Self) void {
229-
// Main loop:
230-
while (true) {
231-
// Receive - exit from the thread if mailbox was closed
232-
const envelope = echo.to.receive(100000000) catch break;
233-
// Reply to the client
234-
// Exit from the thread if mailbox was closed
235-
_ = echo.from.send(envelope) catch break;
236-
}
237-
}
238-
239-
// Wait exit from the thread
240-
pub fn waitFinish(echo: *Self) void {
241-
echo.thread.join();
242-
}
243-
244-
// Close mailboxes
245-
// As result Echo should stop processing
246-
// and exit from the thread.
247-
pub fn stop(echo: *Self) !void {
248-
_ = echo.to.close();
249-
_ = echo.from.close();
250-
}
251-
};
252-
253-
var echo = try std.testing.allocator.create(Echo);
254-
255-
// Start Echo(on own thread)
256-
echo.start();
257-
258-
defer {
259-
// Wait finish of Echo
260-
echo.waitFinish();
261-
std.testing.allocator.destroy(echo);
262-
}
263-
264-
// because nothing was send to 'TO' mailbox, nothing should be received
265-
// from 'FROM' mailbox
266-
try testing.expectError(error.Timeout, echo.from.receive(100));
267-
268-
// Create wrapper for the data
269-
const envl = try std.testing.allocator.create(Mbx.Envelope);
270-
defer std.testing.allocator.destroy(envl);
271-
272-
// Send/Receive loop
273-
for (0..6) |indx| {
274-
// Set value for send [0-5]
275-
envl.letter = indx;
276-
277-
// Send to 'TO' mailbox
278-
try echo.to.send(envl);
279-
280-
// Wait received data from OUT mailbox
281-
const back = echo.from.receive(1000000);
282-
283-
if (back) |val| {
284-
// Expected value == index [0-5]
285-
try testing.expect(val.letter == indx);
286-
} else |_| {
287-
try testing.expect(false);
288-
}
289-
}
290-
291-
// Stop Echo
292-
try echo.stop();
293-
}
294-
// defered:
295-
// Wait finish of Echo
296-
// echo.waitFinish();
297-
// Free allocated memory:
298-
// std.testing.allocator.destroy(echo);
299-
// std.testing.allocator.destroy(envl);
300-
//-----------------------------

src/mailbox_tests.zig

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright (c) 2024 g41797
2+
// SPDX-License-Identifier: MIT
3+
4+
const std = @import("std");
5+
const builtin = @import("builtin");
6+
const debug = std.debug;
7+
const assert = debug.assert;
8+
const testing = std.testing;
9+
10+
const Mutex = std.Thread.Mutex;
11+
const Condition = std.Thread.Condition;
12+
const Thread = std.Thread;
13+
14+
const mailbox = @import("mailbox.zig");
15+
16+
//-----------------------------
17+
test {
18+
@import("std").testing.refAllDecls(@This());
19+
}
20+
//-----------------------------
21+
22+
//-----------------------------
23+
test "basic MailBox test" {
24+
const Mbx = mailbox.MailBox(u32);
25+
var mbox: Mbx = .{};
26+
27+
try testing.expectError(error.Timeout, mbox.receive(10));
28+
29+
var one = Mbx.Envelope{ .letter = 1 };
30+
var two = Mbx.Envelope{ .letter = 2 };
31+
var three = Mbx.Envelope{ .letter = 3 };
32+
var four = Mbx.Envelope{ .letter = 4 };
33+
var five = Mbx.Envelope{ .letter = 5 };
34+
35+
try mbox.send(&one);
36+
try mbox.send(&two);
37+
try mbox.send(&three);
38+
try mbox.send(&four);
39+
try mbox.send(&five);
40+
41+
try testing.expect(mbox.letters() == 5);
42+
43+
for (1..6) |i| {
44+
const recv = mbox.receive(1000);
45+
46+
if (recv) |val| {
47+
try testing.expect(val.letter == i);
48+
} else |_| {
49+
try testing.expect(false);
50+
}
51+
}
52+
53+
try testing.expectError(error.Timeout, mbox.receive(10));
54+
55+
_ = mbox.close();
56+
try testing.expectError(error.Closed, mbox.receive(10));
57+
}
58+
//-----------------------------
59+
60+
//-----------------------------
61+
test "Echo mailboxes test" {
62+
63+
// Mbx is Mailbox with usize letter(data)
64+
const Mbx = mailbox.MailBox(usize);
65+
66+
// Echo - runs on own thread
67+
// It has two mailboxes
68+
// "TO" and "FROM" - from the client point of the view
69+
// Receives letter via 'TO' mailbox
70+
// Replies letter without change (echo) to "FROM" mailbox
71+
const Echo = struct {
72+
const Self = @This();
73+
74+
to: Mbx = undefined,
75+
from: Mbx = undefined,
76+
thread: Thread = undefined,
77+
78+
// Mailboxes creation and start of the thread
79+
// Pay attention, that client code does not use
80+
// any thread "API" - all embedded within Echo
81+
pub fn start(echo: *Self) void {
82+
echo.to = .{};
83+
echo.from = .{};
84+
echo.thread = std.Thread.spawn(.{}, run, .{echo}) catch unreachable;
85+
}
86+
87+
// Echo thread function
88+
fn run(echo: *Self) void {
89+
// Main loop:
90+
while (true) {
91+
// Receive - exit from the thread if mailbox was closed
92+
const envelope = echo.to.receive(100000000) catch break;
93+
// Reply to the client
94+
// Exit from the thread if mailbox was closed
95+
_ = echo.from.send(envelope) catch break;
96+
}
97+
}
98+
99+
// Wait exit from the thread
100+
pub fn waitFinish(echo: *Self) void {
101+
echo.thread.join();
102+
}
103+
104+
// Close mailboxes
105+
// As result Echo should stop processing
106+
// and exit from the thread.
107+
pub fn stop(echo: *Self) !void {
108+
_ = echo.to.close();
109+
_ = echo.from.close();
110+
}
111+
};
112+
113+
var echo = try std.testing.allocator.create(Echo);
114+
115+
// Start Echo(on own thread)
116+
echo.start();
117+
118+
defer {
119+
// Wait finish of Echo
120+
echo.waitFinish();
121+
std.testing.allocator.destroy(echo);
122+
}
123+
124+
// because nothing was send to 'TO' mailbox, nothing should be received
125+
// from 'FROM' mailbox
126+
try testing.expectError(error.Timeout, echo.from.receive(100));
127+
128+
// Create wrapper for the data
129+
const envl = try std.testing.allocator.create(Mbx.Envelope);
130+
defer std.testing.allocator.destroy(envl);
131+
132+
// Send/Receive loop
133+
for (0..6) |indx| {
134+
// Set value for send [0-5]
135+
envl.letter = indx;
136+
137+
// Send to 'TO' mailbox
138+
try echo.to.send(envl);
139+
140+
// Wait received data from OUT mailbox
141+
const back = echo.from.receive(1000000);
142+
143+
if (back) |val| {
144+
// Expected value == index [0-5]
145+
try testing.expect(val.letter == indx);
146+
} else |_| {
147+
try testing.expect(false);
148+
}
149+
}
150+
151+
// Stop Echo
152+
try echo.stop();
153+
}
154+
// defered:
155+
// Wait finish of Echo
156+
// echo.waitFinish();
157+
// Free allocated memory:
158+
// std.testing.allocator.destroy(echo);
159+
// std.testing.allocator.destroy(envl);
160+
//-----------------------------

0 commit comments

Comments
 (0)