aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-02-13 13:12:18 -0500
committerGitHub <noreply@github.com>2020-02-13 13:12:18 -0500
commit1675d4f82b94b4db0272aff483760cd526963a4c (patch)
tree1c2f56508d70b602d9520eb7a593a3079c54e540 /lib/std
parentfa377dbd1510fc733607afe47dc04dfe55d5ff88 (diff)
parentf93c219f30b4b4f3bb9280d39be7b585ddcd8447 (diff)
downloadzig-1675d4f82b94b4db0272aff483760cd526963a4c.tar.gz
zig-1675d4f82b94b4db0272aff483760cd526963a4c.zip
Merge pull request #4443 from LemonBoy/werkzeug
A train of small patches
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/meta.zig15
-rw-r--r--lib/std/os/test.zig19
-rw-r--r--lib/std/testing.zig17
3 files changed, 39 insertions, 12 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 5cb9c6589c..f090e1e8e4 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -433,6 +433,14 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {
if (!eql(e, b[i])) return false;
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 => {
const info = @typeInfo(T).Pointer;
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(false), EU.tst(false)));
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" {
diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig
index f33b5d5261..989653116c 100644
--- a/lib/std/os/test.zig
+++ b/lib/std/os/test.zig
@@ -276,8 +276,11 @@ test "mmap" {
testing.expectEqual(@as(usize, 1234), data.len);
// By definition the data returned by mmap is zero-filled
- std.mem.set(u8, data[0 .. data.len - 1], 0x55);
- testing.expect(mem.indexOfScalar(u8, data, 0).? == 1234 - 1);
+ testing.expect(mem.eql(u8, data, &[_]u8{0x00} ** 1234));
+
+ // Make sure the memory is writeable as requested
+ std.mem.set(u8, data, 0x55);
+ testing.expect(mem.eql(u8, data, &[_]u8{0x55} ** 1234));
}
const test_out_file = "os_tmp_test";
@@ -300,10 +303,7 @@ test "mmap" {
// Map the whole file
{
- const file = try fs.cwd().createFile(test_out_file, .{
- .read = true,
- .truncate = false,
- });
+ const file = try fs.cwd().openFile(test_out_file, .{});
defer file.close();
const data = try os.mmap(
@@ -327,15 +327,12 @@ test "mmap" {
// Map the upper half of the file
{
- const file = try fs.cwd().createFile(test_out_file, .{
- .read = true,
- .truncate = false,
- });
+ const file = try fs.cwd().openFile(test_out_file, .{});
defer file.close();
const data = try os.mmap(
null,
- alloc_size,
+ alloc_size / 2,
os.PROT_READ,
os.MAP_PRIVATE,
file.handle,
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index f8247b5a9d..8a4491d1d7 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -56,7 +56,6 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
.EnumLiteral,
.Enum,
.Fn,
- .Vector,
.ErrorSet,
=> {
if (actual != expected) {
@@ -88,6 +87,15 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
.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| {
inline for (structType.fields) |field| {
expectEqual(@field(expected, field.name), @field(actual, field.name));
@@ -202,3 +210,10 @@ test "expectEqual nested array" {
expectEqual(a, b);
}
+
+test "expectEqual vector" {
+ var a = @splat(4, @as(u32, 4));
+ var b = @splat(4, @as(u32, 4));
+
+ expectEqual(a, b);
+}