fix allocation bug and add test for json file to sim config
This commit is contained in:
parent
085deafc74
commit
46067abda5
1 changed files with 53 additions and 33 deletions
86
src/io.zig
86
src/io.zig
|
|
@ -20,18 +20,19 @@ const JsonSim = struct {
|
|||
};
|
||||
|
||||
pub fn jsonFileToSimConfig(
|
||||
file_path: []u8,
|
||||
file_path: []const u8,
|
||||
allocator: std.mem.Allocator,
|
||||
io: std.Io,
|
||||
) !drone.QuadCopterSim {
|
||||
const cwd = std.Io.Dir.cwd();
|
||||
const file = try cwd.openFile(io, file_path, .{ .mode = .read });
|
||||
const file = try cwd.openFile(io, file_path, .{ .mode = .read_only });
|
||||
defer file.close(io);
|
||||
|
||||
var file_reader = file.reader(io, &.{});
|
||||
const json = try file_reader.interface.allocRemaining(allocator, .unlimited);
|
||||
defer allocator.free(json);
|
||||
|
||||
return jsonStringToSimConfig(json, allocator, io);
|
||||
return jsonStringToSimConfig(json, allocator);
|
||||
}
|
||||
|
||||
pub fn jsonStringToSimConfig(
|
||||
|
|
@ -110,40 +111,59 @@ test "Verify parse from Json String works" {
|
|||
\\}
|
||||
;
|
||||
const gpa = std.testing.allocator;
|
||||
const sim_config = try jsonStringToSimConfig(TEST_JSON_STRING, gpa);
|
||||
const string_sim_config = try jsonStringToSimConfig(TEST_JSON_STRING, gpa);
|
||||
|
||||
try std.testing.expectApproxEqAbs(0.5, sim_config.drone.mass_kg, 1e-12);
|
||||
try std.testing.expectApproxEqAbs(0.25, sim_config.drone.drag_coeff, 1e-12);
|
||||
try std.testing.expect(vec3.mat3ApproxEq(sim_config.drone.moment_of_inertia, vec3.Mat3F64.identity(), 1e-12));
|
||||
const validate_sim_config = struct {
|
||||
pub fn validate(sim_config: drone.QuadCopterSim) !void {
|
||||
try std.testing.expectApproxEqAbs(0.5, sim_config.drone.mass_kg, 1e-12);
|
||||
try std.testing.expectApproxEqAbs(0.25, sim_config.drone.drag_coeff, 1e-12);
|
||||
try std.testing.expect(vec3.mat3ApproxEq(sim_config.drone.moment_of_inertia, vec3.Mat3F64.identity(), 1e-12));
|
||||
|
||||
const expected_x: [4]vec3.Vec3F64 = .{
|
||||
.init(1, 0, 0),
|
||||
.init(-1, 0, 0),
|
||||
.init(0, 1, 0),
|
||||
.init(0, -1, 0),
|
||||
const expected_x: [4]vec3.Vec3F64 = .{
|
||||
.init(1, 0, 0),
|
||||
.init(-1, 0, 0),
|
||||
.init(0, 1, 0),
|
||||
.init(0, -1, 0),
|
||||
};
|
||||
|
||||
const expected_thrust: [4]f64 = .{ 1, 2, 1, 2 };
|
||||
const expected_moment: [4]f64 = .{ 2, 1, 2, 1 };
|
||||
const expected_direction: [4]constants.PropSpinDirection = .{ .cw, .ccw, .cw, .ccw };
|
||||
|
||||
var i: u8 = 0;
|
||||
while (i < 4) : (i += 1) {
|
||||
try std.testing.expectApproxEqAbs(
|
||||
expected_thrust[i],
|
||||
sim_config.propellers[i].thrust_coefficient,
|
||||
1e-12,
|
||||
);
|
||||
try std.testing.expectApproxEqAbs(
|
||||
expected_moment[i],
|
||||
sim_config.propellers[i].moment_coefficient,
|
||||
1e-12,
|
||||
);
|
||||
|
||||
try std.testing.expect(
|
||||
vec3.vec3ApproxEq(sim_config.propellers[i].x_cg, expected_x[i], 1e-12),
|
||||
);
|
||||
|
||||
try std.testing.expect(sim_config.propellers[i].direction == expected_direction[i]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const expected_thrust: [4]f64 = .{ 1, 2, 1, 2 };
|
||||
const expected_moment: [4]f64 = .{ 2, 1, 2, 1 };
|
||||
const expected_direction: [4]constants.PropSpinDirection = .{ .cw, .ccw, .cw, .ccw };
|
||||
try validate_sim_config.validate(string_sim_config);
|
||||
|
||||
var i: u8 = 0;
|
||||
while (i < 4) : (i += 1) {
|
||||
try std.testing.expectApproxEqAbs(
|
||||
expected_thrust[i],
|
||||
sim_config.propellers[i].thrust_coefficient,
|
||||
1e-12,
|
||||
);
|
||||
try std.testing.expectApproxEqAbs(
|
||||
expected_moment[i],
|
||||
sim_config.propellers[i].moment_coefficient,
|
||||
1e-12,
|
||||
);
|
||||
|
||||
try std.testing.expect(
|
||||
vec3.vec3ApproxEq(sim_config.propellers[i].x_cg, expected_x[i], 1e-12),
|
||||
);
|
||||
|
||||
try std.testing.expect(sim_config.propellers[i].direction == expected_direction[i]);
|
||||
{
|
||||
const cwd = std.Io.Dir.cwd();
|
||||
const file = try cwd.createFile(std.testing.io, "/tmp/zig_json_config_test.json", .{});
|
||||
// Don't forget to close the file at the end.
|
||||
defer file.close(std.testing.io);
|
||||
// Do things with the file ...
|
||||
_ = try file.writePositionalAll(std.testing.io, TEST_JSON_STRING, 0);
|
||||
}
|
||||
|
||||
const file_sim_config = try jsonFileToSimConfig("/tmp/zig_json_config_test.json", gpa, std.testing.io);
|
||||
|
||||
try validate_sim_config.validate(file_sim_config);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue