aboutsummaryrefslogtreecommitdiff
path: root/test/cases/null.zig
diff options
context:
space:
mode:
authorAndrea Orru <andrea@orru.io>2018-08-06 01:43:19 -0400
committerAndrea Orru <andrea@orru.io>2018-08-06 01:43:19 -0400
commitd2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d (patch)
treee9fa3caec533a0d1e2b434868b2fde1f9240e5c8 /test/cases/null.zig
parent06614b3fa09954464c2e2f32756cacedc178a282 (diff)
parent63a23e848a62d5f167f8d5478de9766cb24aa6eb (diff)
downloadzig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.tar.gz
zig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.zip
Merge branch 'master' into zen_stdlib
Diffstat (limited to 'test/cases/null.zig')
-rw-r--r--test/cases/null.zig86
1 files changed, 46 insertions, 40 deletions
diff --git a/test/cases/null.zig b/test/cases/null.zig
index 35d72b729c..c86dd34b06 100644
--- a/test/cases/null.zig
+++ b/test/cases/null.zig
@@ -1,7 +1,7 @@
const assert = @import("std").debug.assert;
-test "nullable type" {
- const x : ?bool = true;
+test "optional type" {
+ const x: ?bool = true;
if (x) |y| {
if (y) {
@@ -13,15 +13,15 @@ test "nullable type" {
unreachable;
}
- const next_x : ?i32 = null;
+ const next_x: ?i32 = null;
- const z = next_x ?? 1234;
+ const z = next_x orelse 1234;
assert(z == 1234);
- const final_x : ?i32 = 13;
+ const final_x: ?i32 = 13;
- const num = final_x ?? unreachable;
+ const num = final_x orelse unreachable;
assert(num == 13);
}
@@ -30,42 +30,43 @@ test "test maybe object and get a pointer to the inner value" {
var maybe_bool: ?bool = true;
if (maybe_bool) |*b| {
- *b = false;
+ b.* = false;
}
- assert(??maybe_bool == false);
+ assert(maybe_bool.? == false);
}
-
test "rhs maybe unwrap return" {
const x: ?bool = true;
- const y = x ?? return;
+ const y = x orelse return;
}
-
test "maybe return" {
maybeReturnImpl();
comptime maybeReturnImpl();
}
fn maybeReturnImpl() void {
- assert(??foo(1235));
- if (foo(null) != null)
- unreachable;
- assert(!??foo(1234));
+ assert(foo(1235).?);
+ if (foo(null) != null) unreachable;
+ assert(!foo(1234).?);
}
fn foo(x: ?i32) ?bool {
- const value = x ?? return null;
+ const value = x orelse return null;
return value > 1234;
}
-
test "if var maybe pointer" {
- assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
-}
-fn shouldBeAPlus1(p: &const Particle) u64 {
- var maybe_particle: ?Particle = *p;
+ assert(shouldBeAPlus1(Particle{
+ .a = 14,
+ .b = 1,
+ .c = 1,
+ .d = 1,
+ }) == 15);
+}
+fn shouldBeAPlus1(p: *const Particle) u64 {
+ var maybe_particle: ?Particle = p.*;
if (maybe_particle) |*particle| {
particle.a += 1;
}
@@ -81,7 +82,6 @@ const Particle = struct {
d: u64,
};
-
test "null literal outside function" {
const is_null = here_is_a_null_literal.context == null;
assert(is_null);
@@ -92,10 +92,7 @@ test "null literal outside function" {
const SillyStruct = struct {
context: ?i32,
};
-const here_is_a_null_literal = SillyStruct {
- .context = null,
-};
-
+const here_is_a_null_literal = SillyStruct{ .context = null };
test "test null runtime" {
testTestNullRuntime(null);
@@ -105,12 +102,12 @@ fn testTestNullRuntime(x: ?i32) void {
assert(!(x != null));
}
-test "nullable void" {
- nullableVoidImpl();
- comptime nullableVoidImpl();
+test "optional void" {
+ optionalVoidImpl();
+ comptime optionalVoidImpl();
}
-fn nullableVoidImpl() void {
+fn optionalVoidImpl() void {
assert(bar(null) == null);
assert(bar({}) != null);
}
@@ -123,21 +120,19 @@ fn bar(x: ?void) ?void {
}
}
-
-
-const StructWithNullable = struct {
+const StructWithOptional = struct {
field: ?i32,
};
-var struct_with_nullable: StructWithNullable = undefined;
+var struct_with_optional: StructWithOptional = undefined;
-test "unwrap nullable which is field of global var" {
- struct_with_nullable.field = null;
- if (struct_with_nullable.field) |payload| {
+test "unwrap optional which is field of global var" {
+ struct_with_optional.field = null;
+ if (struct_with_optional.field) |payload| {
unreachable;
}
- struct_with_nullable.field = 1234;
- if (struct_with_nullable.field) |payload| {
+ struct_with_optional.field = 1234;
+ if (struct_with_optional.field) |payload| {
assert(payload == 1234);
} else {
unreachable;
@@ -145,6 +140,17 @@ test "unwrap nullable which is field of global var" {
}
test "null with default unwrap" {
- const x: i32 = null ?? 1;
+ const x: i32 = null orelse 1;
assert(x == 1);
}
+
+test "optional types" {
+ comptime {
+ const opt_type_struct = StructWithOptionalType{ .t = u8 };
+ assert(opt_type_struct.t != null and opt_type_struct.t.? == u8);
+ }
+}
+
+const StructWithOptionalType = struct {
+ t: ?type,
+};