add Battery test

This commit is contained in:
Jeeves 2025-04-30 04:59:11 -06:00
parent 1c69ce4b2f
commit 584f80e7df

View file

@ -410,6 +410,7 @@ pub const Battery = struct {
pub fn init(allocator: std.mem.Allocator) !*Battery {
var self = try allocator.create(Battery);
errdefer allocator.destroy(self);
self.* = .{ .component = undefined };
try Component.init(&self.component, allocator, "Battery", 0, 1, &update, &deinit);
return self;
}
@ -427,6 +428,32 @@ pub const Battery = struct {
}
};
test "Battery" {
var battery = try Battery.init(std.testing.allocator);
defer battery.component.deinit(std.testing.allocator);
// default in-game is 100%
// the default should be the same here too
try battery.component.update();
try std.testing.expectEqual(Signal{ .digital = 1, .analog = 1.0 }, battery.component.getOutput(0).*);
battery.value = 0.0;
try battery.component.update();
try std.testing.expectEqual(Signal{ .digital = 0, .analog = 0.0 }, battery.component.getOutput(0).*);
battery.value = -1.0;
try battery.component.update();
try std.testing.expectEqual(Signal{ .digital = -1, .analog = -1.0 }, battery.component.getOutput(0).*);
battery.value = 0.42;
try battery.component.update();
try std.testing.expectEqual(Signal{ .digital = 1, .analog = 0.42 }, battery.component.getOutput(0).*);
battery.value = -0.69;
try battery.component.update();
try std.testing.expectEqual(Signal{ .digital = -1, .analog = -0.69 }, battery.component.getOutput(0).*);
}
pub const Not = struct {
component: Component,