authorgravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-01-07 19:14:37+05:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-13 12:13:55+01:00
log4578d13b49d16463b870119005877f7b6e10092e
tree11279ba404a894022ebc08f401bea6fff89e347c
parent55304128c086989ad4c08e51c7708d61d544e820

Vector comparison in meta and testing


2 files changed, 31 insertions(+), 1 deletions(-)

lib/std/meta.zig+15
...@@ -433,6 +433,14 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {...@@ -433,6 +433,14 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {
433 if (!eql(e, b[i])) return false;433 if (!eql(e, b[i])) return false;
434 return true;434 return true;
435 },435 },
436 builtin.TypeId.Vector => {
437 const info = @typeInfo(T).Vector;
438 var i: usize = 0;
439 while (i < info.len) : (i += 1) {
440 if (!eql(a[i], b[i])) return false;
441 }
442 return true;
443 },
436 builtin.TypeId.Pointer => {444 builtin.TypeId.Pointer => {
437 const info = @typeInfo(T).Pointer;445 const info = @typeInfo(T).Pointer;
438 switch (info.size) {446 switch (info.size) {
...@@ -510,6 +518,13 @@ test "std.meta.eql" {...@@ -510,6 +518,13 @@ test "std.meta.eql" {
510 testing.expect(eql(EU.tst(true), EU.tst(true)));518 testing.expect(eql(EU.tst(true), EU.tst(true)));
511 testing.expect(eql(EU.tst(false), EU.tst(false)));519 testing.expect(eql(EU.tst(false), EU.tst(false)));
512 testing.expect(!eql(EU.tst(false), EU.tst(true)));520 testing.expect(!eql(EU.tst(false), EU.tst(true)));
521
522 var v1 = @splat(4, @as(u32, 1));
523 var v2 = @splat(4, @as(u32, 1));
524 var v3 = @splat(4, @as(u32, 2));
525
526 testing.expect(eql(v1, v2));
527 testing.expect(!eql(v1, v3));
513}528}
514529
515test "intToEnum with error return" {530test "intToEnum with error return" {
lib/std/testing.zig+16-1
...@@ -56,7 +56,6 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {...@@ -56,7 +56,6 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
56 .EnumLiteral,56 .EnumLiteral,
57 .Enum,57 .Enum,
58 .Fn,58 .Fn,
59 .Vector,
60 .ErrorSet,59 .ErrorSet,
61 => {60 => {
62 if (actual != expected) {61 if (actual != expected) {
...@@ -88,6 +87,15 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {...@@ -88,6 +87,15 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
8887
89 .Array => |array| expectEqualSlices(array.child, &expected, &actual),88 .Array => |array| expectEqualSlices(array.child, &expected, &actual),
9089
90 .Vector => |vectorType| {
91 var i: usize = 0;
92 while (i < vectorType.len) : (i += 1) {
93 if (!std.meta.eql(expected[i], actual[i])) {
94 std.debug.panic("index {} incorrect. expected {}, found {}", .{ i, expected[i], actual[i] });
95 }
96 }
97 },
98
91 .Struct => |structType| {99 .Struct => |structType| {
92 inline for (structType.fields) |field| {100 inline for (structType.fields) |field| {
93 expectEqual(@field(expected, field.name), @field(actual, field.name));101 expectEqual(@field(expected, field.name), @field(actual, field.name));
...@@ -202,3 +210,10 @@ test "expectEqual nested array" {...@@ -202,3 +210,10 @@ test "expectEqual nested array" {
202210
203 expectEqual(a, b);211 expectEqual(a, b);
204}212}
213
214test "expectEqual vector" {
215 var a = @splat(4, @as(u32, 4));
216 var b = @splat(4, @as(u32, 4));
217
218 expectEqual(a, b);
219}