mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
Vector comparison in meta and testing
This commit is contained in:
parent
55304128c0
commit
4578d13b49
@ -433,6 +433,14 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {
|
|||||||
if (!eql(e, b[i])) return false;
|
if (!eql(e, b[i])) return false;
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
builtin.TypeId.Vector => {
|
||||||
|
const info = @typeInfo(T).Vector;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < info.len) : (i += 1) {
|
||||||
|
if (!eql(a[i], b[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
builtin.TypeId.Pointer => {
|
builtin.TypeId.Pointer => {
|
||||||
const info = @typeInfo(T).Pointer;
|
const info = @typeInfo(T).Pointer;
|
||||||
switch (info.size) {
|
switch (info.size) {
|
||||||
@ -510,6 +518,13 @@ test "std.meta.eql" {
|
|||||||
testing.expect(eql(EU.tst(true), EU.tst(true)));
|
testing.expect(eql(EU.tst(true), EU.tst(true)));
|
||||||
testing.expect(eql(EU.tst(false), EU.tst(false)));
|
testing.expect(eql(EU.tst(false), EU.tst(false)));
|
||||||
testing.expect(!eql(EU.tst(false), EU.tst(true)));
|
testing.expect(!eql(EU.tst(false), EU.tst(true)));
|
||||||
|
|
||||||
|
var v1 = @splat(4, @as(u32, 1));
|
||||||
|
var v2 = @splat(4, @as(u32, 1));
|
||||||
|
var v3 = @splat(4, @as(u32, 2));
|
||||||
|
|
||||||
|
testing.expect(eql(v1, v2));
|
||||||
|
testing.expect(!eql(v1, v3));
|
||||||
}
|
}
|
||||||
|
|
||||||
test "intToEnum with error return" {
|
test "intToEnum with error return" {
|
||||||
|
|||||||
@ -56,7 +56,6 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
|
|||||||
.EnumLiteral,
|
.EnumLiteral,
|
||||||
.Enum,
|
.Enum,
|
||||||
.Fn,
|
.Fn,
|
||||||
.Vector,
|
|
||||||
.ErrorSet,
|
.ErrorSet,
|
||||||
=> {
|
=> {
|
||||||
if (actual != expected) {
|
if (actual != expected) {
|
||||||
@ -88,6 +87,15 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
|
|||||||
|
|
||||||
.Array => |array| expectEqualSlices(array.child, &expected, &actual),
|
.Array => |array| expectEqualSlices(array.child, &expected, &actual),
|
||||||
|
|
||||||
|
.Vector => |vectorType| {
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < vectorType.len) : (i += 1) {
|
||||||
|
if (!std.meta.eql(expected[i], actual[i])) {
|
||||||
|
std.debug.panic("index {} incorrect. expected {}, found {}", .{ i, expected[i], actual[i] });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
.Struct => |structType| {
|
.Struct => |structType| {
|
||||||
inline for (structType.fields) |field| {
|
inline for (structType.fields) |field| {
|
||||||
expectEqual(@field(expected, field.name), @field(actual, field.name));
|
expectEqual(@field(expected, field.name), @field(actual, field.name));
|
||||||
@ -202,3 +210,10 @@ test "expectEqual nested array" {
|
|||||||
|
|
||||||
expectEqual(a, b);
|
expectEqual(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "expectEqual vector" {
|
||||||
|
var a = @splat(4, @as(u32, 4));
|
||||||
|
var b = @splat(4, @as(u32, 4));
|
||||||
|
|
||||||
|
expectEqual(a, b);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user