master
  1const std = @import("std");
  2const builtin = @import("builtin");
  3
  4const TestEnum = enum { TestEnumValue };
  5const tag_name = @tagName(TestEnum.TestEnumValue);
  6const ptr_tag_name: [*:0]const u8 = tag_name;
  7
  8test "@tagName() returns a string literal" {
  9    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 10    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 11
 12    try std.testing.expect(*const [13:0]u8 == @TypeOf(tag_name));
 13    try std.testing.expect(std.mem.eql(u8, "TestEnumValue", tag_name));
 14    try std.testing.expect(std.mem.eql(u8, "TestEnumValue", ptr_tag_name[0..tag_name.len]));
 15}
 16
 17const TestError = error{TestErrorCode};
 18const error_name = @errorName(TestError.TestErrorCode);
 19const ptr_error_name: [*:0]const u8 = error_name;
 20
 21test "@errorName() returns a string literal" {
 22    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 23    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 24
 25    try std.testing.expect(*const [13:0]u8 == @TypeOf(error_name));
 26    try std.testing.expect(std.mem.eql(u8, "TestErrorCode", error_name));
 27    try std.testing.expect(std.mem.eql(u8, "TestErrorCode", ptr_error_name[0..error_name.len]));
 28}
 29
 30const TestType = struct {};
 31const type_name = @typeName(TestType);
 32const ptr_type_name: [*:0]const u8 = type_name;
 33
 34test "@typeName() returns a string literal" {
 35    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 36    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 37
 38    try std.testing.expect(*const [type_name.len:0]u8 == @TypeOf(type_name));
 39    try std.testing.expect(std.mem.eql(u8, "behavior.string_literals.TestType", type_name));
 40    try std.testing.expect(std.mem.eql(u8, "behavior.string_literals.TestType", ptr_type_name[0..type_name.len]));
 41}
 42
 43const actual_contents = @embedFile("file_to_embed.txt");
 44const ptr_actual_contents: [*:0]const u8 = actual_contents;
 45const expected_contents = "hello zig\n";
 46
 47test "@embedFile() returns a string literal" {
 48    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 49    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
 50    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 51
 52    try std.testing.expect(*const [expected_contents.len:0]u8 == @TypeOf(actual_contents));
 53    try std.testing.expect(std.mem.eql(u8, expected_contents, actual_contents));
 54    try std.testing.expect(std.mem.eql(u8, expected_contents, actual_contents));
 55    try std.testing.expect(std.mem.eql(u8, expected_contents, ptr_actual_contents[0..actual_contents.len]));
 56}
 57
 58fn testFnForSrc() std.builtin.SourceLocation {
 59    return @src();
 60}
 61
 62test "@src() returns a struct containing 0-terminated string slices" {
 63    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
 64    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 65    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 66
 67    const src = testFnForSrc();
 68    try std.testing.expect([:0]const u8 == @TypeOf(src.file));
 69    try std.testing.expect(std.mem.endsWith(u8, src.file, "string_literals.zig"));
 70    try std.testing.expect([:0]const u8 == @TypeOf(src.fn_name));
 71    try std.testing.expect(std.mem.endsWith(u8, src.fn_name, "testFnForSrc"));
 72
 73    const ptr_src_file: [*:0]const u8 = src.file;
 74    _ = ptr_src_file; // unused
 75
 76    const ptr_src_fn_name: [*:0]const u8 = src.fn_name;
 77    _ = ptr_src_fn_name; // unused
 78}
 79
 80test "string literal pointer sentinel" {
 81    const string_literal = "something";
 82
 83    try std.testing.expect(@TypeOf(string_literal.ptr) == [*:0]const u8);
 84}
 85
 86test "sentinel slice of string literal" {
 87    const string = "Hello!\x00World!";
 88    try std.testing.expect(@TypeOf(string) == *const [13:0]u8);
 89
 90    const slice_without_sentinel: []const u8 = string[0..6];
 91    try std.testing.expect(@TypeOf(slice_without_sentinel) == []const u8);
 92
 93    const slice_with_sentinel: [:0]const u8 = string[0..6 :0];
 94    try std.testing.expect(@TypeOf(slice_with_sentinel) == [:0]const u8);
 95}
 96
 97test "Peer type resolution with string literals and unknown length u8 pointers" {
 98    try std.testing.expect(@TypeOf("", "a", @as([*:0]const u8, "")) == [*:0]const u8);
 99    try std.testing.expect(@TypeOf(@as([*:0]const u8, "baz"), "foo", "bar") == [*:0]const u8);
100}
101
102test "including the sentinel when dereferencing a string literal" {
103    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
104
105    var var_str = "abc";
106    const var_derefed = var_str[0 .. var_str.len + 1].*;
107
108    const const_str = "abc";
109    const const_derefed = const_str[0 .. const_str.len + 1].*;
110
111    try std.testing.expectEqualSlices(u8, &var_derefed, &const_derefed);
112    try std.testing.expectEqual(0, const_derefed[3]);
113}