std.debug.assert: remove special case for test builds

Previously, std.debug.assert would `@panic` in test builds,
if the assertion failed. Now, it's always `unreachable`.

This makes release mode test builds more accurately test
the actual code that will be run.

However this requires tests to call `std.testing.expect`
rather than `std.debug.assert` to make sure output is correct.

Here is the explanation of when to use either one, copied from
the assert doc comments:

Inside a test block, it is best to use the `std.testing` module
rather than assert, because assert may not detect a test failure
in ReleaseFast and ReleaseSafe mode. Outside of a test block, assert
is the correct function to use.

closes #1304
master
Andrew Kelley 2019-02-08 18:18:47 -05:00
parent be6d022257
commit c2db077574
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
239 changed files with 4084 additions and 3938 deletions

View File

@ -658,6 +658,7 @@ set(ZIG_STD_FILES
"special/test_runner.zig"
"spinlock.zig"
"statically_initialized_mutex.zig"
"testing.zig"
"unicode.zig"
"zig/ast.zig"
"zig/index.zig"

View File

@ -4,7 +4,7 @@ const io = std.io;
const os = std.os;
const warn = std.debug.warn;
const mem = std.mem;
const assert = std.debug.assert;
const testing = std.testing;
const max_doc_file_size = 10 * 1024 * 1024;
@ -620,7 +620,7 @@ const TermState = enum {
test "term color" {
const input_bytes = "A\x1b[32;1mgreen\x1b[0mB";
const result = try termColor(std.debug.global_allocator, input_bytes);
assert(mem.eql(u8, result, "A<span class=\"t32\">green</span>B"));
testing.expectEqualSlices(u8, "A<span class=\"t32\">green</span>B", result));
}
fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 {

View File

@ -1,5 +1,6 @@
const std = @import("std");
const debug = std.debug;
const testing = std.testing;
const mem = std.mem;
const Allocator = mem.Allocator;
@ -272,21 +273,21 @@ test "parse arguments" {
var args = try Args.parse(std.debug.global_allocator, spec1, cliargs);
debug.assert(args.present("help"));
debug.assert(!args.present("help2"));
debug.assert(!args.present("init"));
testing.expect(args.present("help"));
testing.expect(!args.present("help2"));
testing.expect(!args.present("init"));
debug.assert(mem.eql(u8, args.single("build-file").?, "build.zig"));
debug.assert(mem.eql(u8, args.single("color").?, "on"));
testing.expect(mem.eql(u8, args.single("build-file").?, "build.zig"));
testing.expect(mem.eql(u8, args.single("color").?, "on"));
const objects = args.many("object").?;
debug.assert(mem.eql(u8, objects[0], "obj1"));
debug.assert(mem.eql(u8, objects[1], "obj2"));
testing.expect(mem.eql(u8, objects[0], "obj1"));
testing.expect(mem.eql(u8, objects[1], "obj2"));
debug.assert(mem.eql(u8, args.single("library").?, "lib2"));
testing.expect(mem.eql(u8, args.single("library").?, "lib2"));
const pos = args.positionals.toSliceConst();
debug.assert(mem.eql(u8, pos[0], "build"));
debug.assert(mem.eql(u8, pos[1], "pos1"));
debug.assert(mem.eql(u8, pos[2], "pos2"));
testing.expect(mem.eql(u8, pos[0], "build"));
testing.expect(mem.eql(u8, pos[1], "pos1"));
testing.expect(mem.eql(u8, pos[2], "pos2"));
}

View File

@ -4,7 +4,7 @@ const builtin = @import("builtin");
const Target = @import("target.zig").Target;
const Compilation = @import("compilation.zig").Compilation;
const introspect = @import("introspect.zig");
const assertOrPanic = std.debug.assertOrPanic;
const testing = std.testing;
const errmsg = @import("errmsg.zig");
const ZigCompiler = @import("compilation.zig").ZigCompiler;
@ -210,7 +210,7 @@ pub const TestContext = struct {
@panic("build incorrectly failed");
},
Compilation.Event.Fail => |msgs| {
assertOrPanic(msgs.len != 0);
testing.expect(msgs.len != 0);
for (msgs) |msg| {
if (mem.endsWith(u8, msg.realpath, path) and mem.eql(u8, msg.text, text)) {
const span = msg.getSpan();

View File

@ -17543,21 +17543,16 @@ static void make_enum_field_val(IrAnalyze *ira, ConstExprValue *enum_field_val,
enum_field_val->data.x_struct.fields = inner_fields;
}
static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstExprValue **out) {
static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigType *type_entry, ConstExprValue **out) {
Error err;
assert(type_entry != nullptr);
assert(!type_is_invalid(type_entry));
if ((err = ensure_complete_type(ira->codegen, type_entry)))
if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
return err;
if (type_entry == ira->codegen->builtin_types.entry_global_error_set) {
zig_panic("TODO implement @typeInfo for global error set");
}
ConstExprValue *result = nullptr;
switch (type_entry->id)
{
switch (type_entry->id) {
case ZigTypeIdInvalid:
zig_unreachable();
case ZigTypeIdMetaType:
@ -17778,6 +17773,15 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE
ensure_field_index(result->type, "errors", 0);
ZigType *type_info_error_type = ir_type_info_get_type(ira, "Error", nullptr);
if (!resolve_inferred_error_set(ira->codegen, type_entry, source_node)) {
return ErrorSemanticAnalyzeFail;
}
if (type_is_global_error_set(type_entry)) {
ir_add_error_node(ira, source_node,
buf_sprintf("TODO: compiler bug: implement @typeInfo support for anyerror. https://github.com/ziglang/zig/issues/1936"));
return ErrorSemanticAnalyzeFail;
}
uint32_t error_count = type_entry->data.error_set.err_count;
ConstExprValue *error_array = create_const_vals(1);
error_array->special = ConstValSpecialStatic;
@ -18103,7 +18107,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE
{
ZigType *fn_type = type_entry->data.bound_fn.fn_type;
assert(fn_type->id == ZigTypeIdFn);
if ((err = ir_make_type_info_value(ira, fn_type, &result)))
if ((err = ir_make_type_info_value(ira, source_node, fn_type, &result)))
return err;
break;
@ -18128,7 +18132,7 @@ static IrInstruction *ir_analyze_instruction_type_info(IrAnalyze *ira,
ZigType *result_type = ir_type_info_get_type(ira, nullptr, nullptr);
ConstExprValue *payload;
if ((err = ir_make_type_info_value(ira, type_entry, &payload)))
if ((err = ir_make_type_info_value(ira, instruction->base.source_node, type_entry, &payload)))
return ira->codegen->invalid_instruction;
IrInstruction *result = ir_const(ira, &instruction->base, result_type);

View File

@ -1,7 +1,7 @@
const std = @import("index.zig");
const debug = std.debug;
const assert = debug.assert;
const assertError = debug.assertError;
const testing = std.testing;
const mem = std.mem;
const Allocator = mem.Allocator;
@ -212,8 +212,8 @@ test "std.ArrayList.init" {
var list = ArrayList(i32).init(allocator);
defer list.deinit();
assert(list.count() == 0);
assert(list.capacity() == 0);
testing.expect(list.count() == 0);
testing.expect(list.capacity() == 0);
}
test "std.ArrayList.basic" {
@ -224,7 +224,7 @@ test "std.ArrayList.basic" {
defer list.deinit();
// setting on empty list is out of bounds
assertError(list.setOrError(0, 1), error.OutOfBounds);
testing.expectError(error.OutOfBounds, list.setOrError(0, 1));
{
var i: usize = 0;
@ -236,44 +236,44 @@ test "std.ArrayList.basic" {
{
var i: usize = 0;
while (i < 10) : (i += 1) {
assert(list.items[i] == @intCast(i32, i + 1));
testing.expect(list.items[i] == @intCast(i32, i + 1));
}
}
for (list.toSlice()) |v, i| {
assert(v == @intCast(i32, i + 1));
testing.expect(v == @intCast(i32, i + 1));
}
for (list.toSliceConst()) |v, i| {
assert(v == @intCast(i32, i + 1));
testing.expect(v == @intCast(i32, i + 1));
}
assert(list.pop() == 10);
assert(list.len == 9);
testing.expect(list.pop() == 10);
testing.expect(list.len == 9);
list.appendSlice([]const i32{
1,
2,
3,
}) catch unreachable;
assert(list.len == 12);
assert(list.pop() == 3);
assert(list.pop() == 2);
assert(list.pop() == 1);
assert(list.len == 9);
testing.expect(list.len == 12);
testing.expect(list.pop() == 3);
testing.expect(list.pop() == 2);
testing.expect(list.pop() == 1);
testing.expect(list.len == 9);
list.appendSlice([]const i32{}) catch unreachable;
assert(list.len == 9);
testing.expect(list.len == 9);
// can only set on indices < self.len
list.set(7, 33);
list.set(8, 42);
assertError(list.setOrError(9, 99), error.OutOfBounds);
assertError(list.setOrError(10, 123), error.OutOfBounds);
testing.expectError(error.OutOfBounds, list.setOrError(9, 99));
testing.expectError(error.OutOfBounds, list.setOrError(10, 123));
assert(list.pop() == 42);
assert(list.pop() == 33);
testing.expect(list.pop() == 42);
testing.expect(list.pop() == 33);
}
test "std.ArrayList.swapRemove" {
@ -289,18 +289,18 @@ test "std.ArrayList.swapRemove" {
try list.append(7);
//remove from middle
assert(list.swapRemove(3) == 4);
assert(list.at(3) == 7);
assert(list.len == 6);
testing.expect(list.swapRemove(3) == 4);
testing.expect(list.at(3) == 7);
testing.expect(list.len == 6);
//remove from end
assert(list.swapRemove(5) == 6);
assert(list.len == 5);
testing.expect(list.swapRemove(5) == 6);
testing.expect(list.len == 5);
//remove from front
assert(list.swapRemove(0) == 1);
assert(list.at(0) == 5);
assert(list.len == 4);
testing.expect(list.swapRemove(0) == 1);
testing.expect(list.at(0) == 5);
testing.expect(list.len == 4);
}
test "std.ArrayList.swapRemoveOrError" {
@ -308,27 +308,27 @@ test "std.ArrayList.swapRemoveOrError" {
defer list.deinit();
// Test just after initialization
assertError(list.swapRemoveOrError(0), error.OutOfBounds);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
// Test after adding one item and remote it
try list.append(1);
assert((try list.swapRemoveOrError(0)) == 1);
assertError(list.swapRemoveOrError(0), error.OutOfBounds);
testing.expect((try list.swapRemoveOrError(0)) == 1);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
// Test after adding two items and remote both
try list.append(1);
try list.append(2);
assert((try list.swapRemoveOrError(1)) == 2);
assert((try list.swapRemoveOrError(0)) == 1);
assertError(list.swapRemoveOrError(0), error.OutOfBounds);
testing.expect((try list.swapRemoveOrError(1)) == 2);
testing.expect((try list.swapRemoveOrError(0)) == 1);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0));
// Test out of bounds with one item
try list.append(1);
assertError(list.swapRemoveOrError(1), error.OutOfBounds);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(1));
// Test out of bounds with two items
try list.append(2);
assertError(list.swapRemoveOrError(2), error.OutOfBounds);
testing.expectError(error.OutOfBounds, list.swapRemoveOrError(2));
}
test "std.ArrayList.iterator" {
@ -342,22 +342,22 @@ test "std.ArrayList.iterator" {
var count: i32 = 0;
var it = list.iterator();
while (it.next()) |next| {
assert(next == count + 1);
testing.expect(next == count + 1);
count += 1;
}
assert(count == 3);
assert(it.next() == null);
testing.expect(count == 3);
testing.expect(it.next() == null);
it.reset();
count = 0;
while (it.next()) |next| {
assert(next == count + 1);
testing.expect(next == count + 1);
count += 1;
if (count == 2) break;
}
it.reset();
assert(it.next().? == 1);
testing.expect(it.next().? == 1);
}
test "std.ArrayList.insert" {
@ -368,10 +368,10 @@ test "std.ArrayList.insert" {
try list.append(2);
try list.append(3);
try list.insert(0, 5);
assert(list.items[0] == 5);
assert(list.items[1] == 1);
assert(list.items[2] == 2);
assert(list.items[3] == 3);
testing.expect(list.items[0] == 5);
testing.expect(list.items[1] == 1);
testing.expect(list.items[2] == 2);
testing.expect(list.items[3] == 3);
}
test "std.ArrayList.insertSlice" {
@ -386,17 +386,17 @@ test "std.ArrayList.insertSlice" {
9,
8,
});
assert(list.items[0] == 1);
assert(list.items[1] == 9);
assert(list.items[2] == 8);
assert(list.items[3] == 2);
assert(list.items[4] == 3);
assert(list.items[5] == 4);
testing.expect(list.items[0] == 1);
testing.expect(list.items[1] == 9);
testing.expect(list.items[2] == 8);
testing.expect(list.items[3] == 2);
testing.expect(list.items[4] == 3);
testing.expect(list.items[5] == 4);
const items = []const i32{1};
try list.insertSlice(0, items[0..0]);
assert(list.len == 6);
assert(list.items[0] == 1);
testing.expect(list.len == 6);
testing.expect(list.items[0] == 1);
}
const Item = struct {
@ -407,5 +407,5 @@ const Item = struct {
test "std.ArrayList: ArrayList(T) of struct T" {
var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(debug.global_allocator) };
try root.sub_items.append( Item{ .integer = 42, .sub_items = ArrayList(Item).init(debug.global_allocator) } );
assert(root.sub_items.items[0].integer == 42);
testing.expect(root.sub_items.items[0].integer == 42);
}

View File

@ -3,6 +3,7 @@ const builtin = @import("builtin");
const AtomicOrder = builtin.AtomicOrder;
const AtomicRmwOp = builtin.AtomicRmwOp;
const assert = std.debug.assert;
const expect = std.testing.expect;
/// Many producer, many consumer, non-allocating, thread-safe.
/// Uses a mutex to protect access.
@ -174,14 +175,14 @@ test "std.atomic.Queue" {
{
var i: usize = 0;
while (i < put_thread_count) : (i += 1) {
std.debug.assertOrPanic(startPuts(&context) == 0);
expect(startPuts(&context) == 0);
}
}
context.puts_done = 1;
{
var i: usize = 0;
while (i < put_thread_count) : (i += 1) {
std.debug.assertOrPanic(startGets(&context) == 0);
expect(startGets(&context) == 0);
}
}
} else {
@ -264,7 +265,7 @@ test "std.atomic.Queue single-threaded" {
};
queue.put(&node_1);
assert(queue.get().?.data == 0);
expect(queue.get().?.data == 0);
var node_2 = Queue(i32).Node{
.data = 2,
@ -280,9 +281,9 @@ test "std.atomic.Queue single-threaded" {
};
queue.put(&node_3);
assert(queue.get().?.data == 1);
expect(queue.get().?.data == 1);
assert(queue.get().?.data == 2);
expect(queue.get().?.data == 2);
var node_4 = Queue(i32).Node{
.data = 4,
@ -291,12 +292,12 @@ test "std.atomic.Queue single-threaded" {
};
queue.put(&node_4);
assert(queue.get().?.data == 3);
expect(queue.get().?.data == 3);
node_3.next = null;
assert(queue.get().?.data == 4);
expect(queue.get().?.data == 4);
assert(queue.get() == null);
expect(queue.get() == null);
}
test "std.atomic.Queue dump" {
@ -311,7 +312,7 @@ test "std.atomic.Queue dump" {
// Test empty stream
sos.reset();
try queue.dumpToStream(SliceOutStream.Error, &sos.stream);
assert(mem.eql(u8, buffer[0..sos.pos],
expect(mem.eql(u8, buffer[0..sos.pos],
\\head: (null)
\\tail: (null)
\\
@ -335,7 +336,7 @@ test "std.atomic.Queue dump" {
\\ (null)
\\
, @ptrToInt(queue.head), @ptrToInt(queue.tail));
assert(mem.eql(u8, buffer[0..sos.pos], expected));
expect(mem.eql(u8, buffer[0..sos.pos], expected));
// Test a stream with two elements
var node_1 = Queue(i32).Node{
@ -356,5 +357,5 @@ test "std.atomic.Queue dump" {
\\ (null)
\\
, @ptrToInt(queue.head), @ptrToInt(queue.head.?.next), @ptrToInt(queue.tail));
assert(mem.eql(u8, buffer[0..sos.pos], expected));
expect(mem.eql(u8, buffer[0..sos.pos], expected));
}

View File

@ -1,6 +1,7 @@
const assert = std.debug.assert;
const builtin = @import("builtin");
const AtomicOrder = builtin.AtomicOrder;
const expect = std.testing.expect;
/// Many reader, many writer, non-allocating, thread-safe
/// Uses a spinlock to protect push() and pop()
@ -108,14 +109,14 @@ test "std.atomic.stack" {
{
var i: usize = 0;
while (i < put_thread_count) : (i += 1) {
std.debug.assertOrPanic(startPuts(&context) == 0);
expect(startPuts(&context) == 0);
}
}
context.puts_done = 1;
{
var i: usize = 0;
while (i < put_thread_count) : (i += 1) {
std.debug.assertOrPanic(startGets(&context) == 0);
expect(startGets(&context) == 0);
}
}
} else {

View File

@ -1,5 +1,6 @@
const std = @import("index.zig");
const assert = std.debug.assert;
const testing = std.testing;
const mem = std.mem;
pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@ -394,7 +395,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
var buffer: [0x100]u8 = undefined;
var encoded = buffer[0..Base64Encoder.calcSize(expected_decoded.len)];
standard_encoder.encode(encoded, expected_decoded);
assert(mem.eql(u8, encoded, expected_encoded));
testing.expectEqualSlices(u8, expected_encoded, encoded);
}
// Base64Decoder
@ -402,7 +403,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
var buffer: [0x100]u8 = undefined;
var decoded = buffer[0..try standard_decoder.calcSize(expected_encoded)];
try standard_decoder.decode(decoded, expected_encoded);
assert(mem.eql(u8, decoded, expected_decoded));
testing.expectEqualSlices(u8, expected_decoded, decoded);
}
// Base64DecoderWithIgnore
@ -411,8 +412,8 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
var buffer: [0x100]u8 = undefined;
var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)];
var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded);
assert(written <= decoded.len);
assert(mem.eql(u8, decoded[0..written], expected_decoded));
testing.expect(written <= decoded.len);
testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]);
}
// Base64DecoderUnsafe
@ -420,7 +421,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
var buffer: [0x100]u8 = undefined;
var decoded = buffer[0..standard_decoder_unsafe.calcSize(expected_encoded)];
standard_decoder_unsafe.decode(decoded, expected_encoded);
assert(mem.eql(u8, decoded, expected_decoded));
testing.expectEqualSlices(u8, expected_decoded, decoded);
}
}
@ -429,7 +430,7 @@ fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !voi
var buffer: [0x100]u8 = undefined;
var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)];
var written = try standard_decoder_ignore_space.decode(decoded, encoded);
assert(mem.eql(u8, decoded[0..written], expected_decoded));
testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]);
}
fn testError(encoded: []const u8, expected_err: anyerror) !void {

View File

@ -2,7 +2,7 @@ const std = @import("index.zig");
const HashMap = std.HashMap;
const mem = std.mem;
const Allocator = mem.Allocator;
const assert = std.debug.assert;
const testing = std.testing;
/// BufMap copies keys and values before they go into the map, and
/// frees them when they get removed.
@ -90,17 +90,17 @@ test "BufMap" {
defer bufmap.deinit();
try bufmap.set("x", "1");
assert(mem.eql(u8, bufmap.get("x").?, "1"));
assert(1 == bufmap.count());
testing.expect(mem.eql(u8, bufmap.get("x").?, "1"));
testing.expect(1 == bufmap.count());
try bufmap.set("x", "2");
assert(mem.eql(u8, bufmap.get("x").?, "2"));
assert(1 == bufmap.count());
testing.expect(mem.eql(u8, bufmap.get("x").?, "2"));
testing.expect(1 == bufmap.count());
try bufmap.set("x", "3");
assert(mem.eql(u8, bufmap.get("x").?, "3"));
assert(1 == bufmap.count());
testing.expect(mem.eql(u8, bufmap.get("x").?, "3"));
testing.expect(1 == bufmap.count());
bufmap.delete("x");
assert(0 == bufmap.count());
testing.expect(0 == bufmap.count());
}

View File

@ -2,7 +2,7 @@ const std = @import("index.zig");
const HashMap = @import("hash_map.zig").HashMap;
const mem = @import("mem.zig");
const Allocator = mem.Allocator;
const assert = std.debug.assert;
const testing = std.testing;
pub const BufSet = struct {
hash_map: BufSetHashMap,
@ -68,9 +68,9 @@ test "BufSet" {
defer bufset.deinit();
try bufset.put("x");
assert(bufset.count() == 1);
testing.expect(bufset.count() == 1);
bufset.delete("x");
assert(bufset.count() == 0);
testing.expect(bufset.count() == 0);
try bufset.put("x");
try bufset.put("y");

View File

@ -3,6 +3,7 @@ const debug = std.debug;
const mem = std.mem;
const Allocator = mem.Allocator;
const assert = debug.assert;
const testing = std.testing;
const ArrayList = std.ArrayList;
/// A buffer that allocates memory and maintains a null byte at the end.
@ -141,19 +142,19 @@ test "simple Buffer" {
const cstr = @import("cstr.zig");
var buf = try Buffer.init(debug.global_allocator, "");
assert(buf.len() == 0);
testing.expect(buf.len() == 0);
try buf.append("hello");
try buf.append(" ");
try buf.append("world");
assert(buf.eql("hello world"));
assert(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst()));
testing.expect(buf.eql("hello world"));
testing.expect(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst()));
var buf2 = try Buffer.initFromBuffer(buf);
assert(buf.eql(buf2.toSliceConst()));
testing.expect(buf.eql(buf2.toSliceConst()));
assert(buf.startsWith("hell"));
assert(buf.endsWith("orld"));
testing.expect(buf.startsWith("hell"));
testing.expect(buf.endsWith("orld"));
try buf2.resize(4);
assert(buf.startsWith(buf2.toSlice()));
testing.expect(buf.startsWith(buf2.toSlice()));
}

View File

@ -4,6 +4,7 @@ const std = @import("../index.zig");
const mem = std.mem;
const endian = std.endian;
const assert = std.debug.assert;
const testing = std.testing;
const builtin = @import("builtin");
const maxInt = std.math.maxInt;
@ -216,12 +217,12 @@ test "crypto.chacha20 test vector sunscreen" {
};
chaCha20IETF(result[0..], input[0..], 1, key, nonce);
assert(mem.eql(u8, expected_result, result));
testing.expectEqualSlices(u8, expected_result, result);
// Chacha20 is self-reversing.
var plaintext: [114]u8 = undefined;
chaCha20IETF(plaintext[0..], result[0..], 1, key, nonce);
assert(mem.compare(u8, input, plaintext) == mem.Compare.Equal);
testing.expect(mem.compare(u8, input, plaintext) == mem.Compare.Equal);
}
// https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
@ -256,7 +257,7 @@ test "crypto.chacha20 test vector 1" {
const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 0 };
chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
assert(mem.eql(u8, expected_result, result));
testing.expectEqualSlices(u8, expected_result, result);
}
test "crypto.chacha20 test vector 2" {
@ -290,7 +291,7 @@ test "crypto.chacha20 test vector 2" {
const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 0 };
chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
assert(mem.eql(u8, expected_result, result));
testing.expectEqualSlices(u8, expected_result, result);
}
test "crypto.chacha20 test vector 3" {
@ -324,7 +325,7 @@ test "crypto.chacha20 test vector 3" {
const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 1 };
chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
assert(mem.eql(u8, expected_result, result));
testing.expectEqualSlices(u8, expected_result, result);
}
test "crypto.chacha20 test vector 4" {
@ -358,7 +359,7 @@ test "crypto.chacha20 test vector 4" {
const nonce = []u8{ 1, 0, 0, 0, 0, 0, 0, 0 };
chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
assert(mem.eql(u8, expected_result, result));
testing.expectEqualSlices(u8, expected_result, result);
}
test "crypto.chacha20 test vector 5" {
@ -430,5 +431,5 @@ test "crypto.chacha20 test vector 5" {
};
chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce);
assert(mem.eql(u8, expected_result, result));
testing.expectEqualSlices(u8, expected_result, result);
}

View File

@ -230,5 +230,5 @@ test "poly1305 rfc7439 vector1" {
var mac: [16]u8 = undefined;
Poly1305.create(mac[0..], msg, key);
std.debug.assert(std.mem.eql(u8, mac, expected_mac));
std.testing.expectEqualSlices(u8, expected_mac, mac);
}

View File

@ -1,6 +1,7 @@
const debug = @import("../debug/index.zig");
const mem = @import("../mem.zig");
const fmt = @import("../fmt/index.zig");
const std = @import("../index.zig");
const testing = std.testing;
const mem = std.mem;
const fmt = std.fmt;
// Hash using the specified hasher `H` asserting `expected == H(input)`.
pub fn assertEqualHash(comptime Hasher: var, comptime expected: []const u8, input: []const u8) void {
@ -17,5 +18,5 @@ pub fn assertEqual(comptime expected: []const u8, input: []const u8) void {
r.* = fmt.parseInt(u8, expected[2 * i .. 2 * i + 2], 16) catch unreachable;
}
debug.assert(mem.eql(u8, expected_bytes, input));
testing.expectEqualSlices(u8, expected_bytes, input);
}

View File

@ -581,8 +581,8 @@ test "x25519 public key calculation from secret key" {
var pk_calculated: [32]u8 = undefined;
try fmt.hexToBytes(sk[0..], "8052030376d47112be7f73ed7a019293dd12ad910b654455798b4667d73de166");
try fmt.hexToBytes(pk_expected[0..], "f1814f0e8ff1043d8a44d25babff3cedcae6c22c3edaa48f857ae70de2baae50");
std.debug.assert(X25519.createPublicKey(pk_calculated[0..], sk));
std.debug.assert(std.mem.eql(u8, pk_calculated, pk_expected));
std.testing.expect(X25519.createPublicKey(pk_calculated[0..], sk));
std.testing.expect(std.mem.eql(u8, pk_calculated, pk_expected));
}
test "x25519 rfc7748 vector1" {
@ -593,8 +593,8 @@ test "x25519 rfc7748 vector1" {
var output: [32]u8 = undefined;
std.debug.assert(X25519.create(output[0..], secret_key, public_key));
std.debug.assert(std.mem.eql(u8, output, expected_output));
std.testing.expect(X25519.create(output[0..], secret_key, public_key));
std.testing.expect(std.mem.eql(u8, output, expected_output));
}
test "x25519 rfc7748 vector2" {
@ -605,8 +605,8 @@ test "x25519 rfc7748 vector2" {
var output: [32]u8 = undefined;
std.debug.assert(X25519.create(output[0..], secret_key, public_key));
std.debug.assert(std.mem.eql(u8, output, expected_output));
std.testing.expect(X25519.create(output[0..], secret_key, public_key));
std.testing.expect(std.mem.eql(u8, output, expected_output));
}
test "x25519 rfc7748 one iteration" {
@ -619,13 +619,13 @@ test "x25519 rfc7748 one iteration" {
var i: usize = 0;
while (i < 1) : (i += 1) {
var output: [32]u8 = undefined;
std.debug.assert(X25519.create(output[0..], k, u));
std.testing.expect(X25519.create(output[0..], k, u));
std.mem.copy(u8, u[0..], k[0..]);
std.mem.copy(u8, k[0..], output[0..]);
}
std.debug.assert(std.mem.eql(u8, k[0..], expected_output));
std.testing.expect(std.mem.eql(u8, k[0..], expected_output));
}
test "x25519 rfc7748 1,000 iterations" {
@ -643,13 +643,13 @@ test "x25519 rfc7748 1,000 iterations" {
var i: usize = 0;
while (i < 1000) : (i += 1) {
var output: [32]u8 = undefined;
std.debug.assert(X25519.create(output[0..], k, u));
std.testing.expect(X25519.create(output[0..], k, u));
std.mem.copy(u8, u[0..], k[0..]);
std.mem.copy(u8, k[0..], output[0..]);
}
std.debug.assert(std.mem.eql(u8, k[0..], expected_output));
std.testing.expect(std.mem.eql(u8, k[0..], expected_output));
}
test "x25519 rfc7748 1,000,000 iterations" {
@ -666,11 +666,11 @@ test "x25519 rfc7748 1,000,000 iterations" {
var i: usize = 0;
while (i < 1000000) : (i += 1) {
var output: [32]u8 = undefined;
std.debug.assert(X25519.create(output[0..], k, u));
std.testing.expect(X25519.create(output[0..], k, u));
std.mem.copy(u8, u[0..], k[0..]);
std.mem.copy(u8, k[0..], output[0..]);
}
std.debug.assert(std.mem.eql(u8, k[0..], expected_output));
std.testing.expect(std.mem.eql(u8, k[0..], expected_output));
}

View File

@ -2,7 +2,7 @@ const std = @import("index.zig");
const builtin = @import("builtin");
const debug = std.debug;
const mem = std.mem;
const assert = debug.assert;
const testing = std.testing;
pub const line_sep = switch (builtin.os) {
builtin.Os.windows => "\r\n",
@ -42,8 +42,8 @@ test "cstr fns" {
}
fn testCStrFnsImpl() void {
assert(cmp(c"aoeu", c"aoez") == -1);
assert(len(c"123456789") == 9);
testing.expect(cmp(c"aoeu", c"aoez") == -1);
testing.expect(len(c"123456789") == 9);
}
/// Returns a mutable slice with 1 more byte of length which is a null byte.

View File

@ -107,37 +107,15 @@ pub fn dumpStackTrace(stack_trace: *const builtin.StackTrace) void {
/// This function invokes undefined behavior when `ok` is `false`.
/// In Debug and ReleaseSafe modes, calls to this function are always
/// generated, and the `unreachable` statement triggers a panic.
/// In ReleaseFast and ReleaseSmall modes, calls to this function can be
/// optimized away.
/// In ReleaseFast and ReleaseSmall modes, calls to this function are
/// optimized away, and in fact the optimizer is able to use the assertion
/// in its heuristics.
/// Inside a test block, it is best to use the `std.testing` module rather
/// than this function, because this function may not detect a test failure
/// in ReleaseFast and ReleaseSafe mode. Outside of a test block, this assert
/// function is the correct function to use.
pub fn assert(ok: bool) void {
if (!ok) {
// In ReleaseFast test mode, we still want assert(false) to crash, so
// we insert an explicit call to @panic instead of unreachable.
// TODO we should use `assertOrPanic` in tests and remove this logic.
if (builtin.is_test) {
@panic("assertion failure");
} else {
unreachable; // assertion failure
}
}
}
/// TODO: add `==` operator for `error_union == error_set`, and then
/// remove this function
pub fn assertError(value: var, expected_error: anyerror) void {
if (value) {
@panic("expected error");
} else |actual_error| {
assert(actual_error == expected_error);
}
}
/// Call this function when you want to panic if the condition is not true.
/// If `ok` is `false`, this function will panic in every release mode.
pub fn assertOrPanic(ok: bool) void {
if (!ok) {
@panic("assertion failure");
}
if (!ok) unreachable; // assertion failure
}
pub fn panic(comptime format: []const u8, args: ...) noreturn {

View File

@ -6,6 +6,7 @@ const mem = std.mem;
const cstr = std.cstr;
const os = std.os;
const assert = std.debug.assert;
const testing = std.testing;
const elf = std.elf;
const linux = os.linux;
const windows = os.windows;
@ -206,7 +207,7 @@ test "dynamic_library" {
};
const dynlib = DynLib.open(std.debug.global_allocator, libname) catch |err| {
assert(err == error.FileNotFound);
testing.expect(err == error.FileNotFound);
return;
};
@panic("Expected error from function");

View File

@ -1,6 +1,7 @@
const std = @import("../index.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const testing = std.testing;
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
const Loop = std.event.Loop;
@ -350,19 +351,19 @@ async fn testChannelGetter(loop: *Loop, channel: *Channel(i32)) void {
const value1_promise = try async channel.get();
const value1 = await value1_promise;
assert(value1 == 1234);
testing.expect(value1 == 1234);
const value2_promise = try async channel.get();
const value2 = await value2_promise;
assert(value2 == 4567);
testing.expect(value2 == 4567);
const value3_promise = try async channel.getOrNull();
const value3 = await value3_promise;
assert(value3 == null);
testing.expect(value3 == null);
const last_put = try async testPut(channel, 4444);
const value4 = await try async channel.getOrNull();
assert(value4.? == 4444);
testing.expect(value4.? == 4444);
await last_put;
}

View File

@ -2,6 +2,7 @@ const builtin = @import("builtin");
const std = @import("../index.zig");
const event = std.event;
const assert = std.debug.assert;
const testing = std.testing;
const os = std.os;
const mem = std.mem;
const posix = os.posix;
@ -1349,13 +1350,13 @@ async fn testFsWatch(loop: *Loop) !void {
try await try async writeFile(loop, file_path, contents);
const read_contents = try await try async readFile(loop, file_path, 1024 * 1024);
assert(mem.eql(u8, read_contents, contents));
testing.expectEqualSlices(u8, contents, read_contents);
// now watch the file
var watch = try Watch(void).create(loop, 0);
defer watch.destroy();
assert((try await try async watch.addFile(file_path, {})) == null);
testing.expect((try await try async watch.addFile(file_path, {})) == null);
const ev = try async watch.channel.get();
var ev_consumed = false;
@ -1375,10 +1376,10 @@ async fn testFsWatch(loop: *Loop) !void {
WatchEventId.Delete => @panic("wrong event"),
}
const contents_updated = try await try async readFile(loop, file_path, 1024 * 1024);
assert(mem.eql(u8, contents_updated,
testing.expectEqualSlices(u8,
\\line 1
\\lorem ipsum
));
, contents_updated);
// TODO test deleting the file and then re-adding it. we should get events for both
}

View File

@ -1,5 +1,6 @@
const std = @import("../index.zig");
const assert = std.debug.assert;
const testing = std.testing;
const builtin = @import("builtin");
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
@ -114,7 +115,7 @@ async fn testFuture(loop: *Loop) void {
const result = (await a) + (await b);
cancel c;
assert(result == 12);
testing.expect(result == 12);
}
async fn waitOnFuture(future: *Future(i32)) i32 {

View File

@ -4,7 +4,7 @@ const Lock = std.event.Lock;
const Loop = std.event.Loop;
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
const assert = std.debug.assert;
const testing = std.testing;
/// ReturnType must be `void` or `E!void`
pub fn Group(comptime ReturnType: type) type {
@ -146,12 +146,12 @@ async fn testGroup(loop: *Loop) void {
group.add(async sleepALittle(&count) catch @panic("memory")) catch @panic("memory");
group.call(increaseByTen, &count) catch @panic("memory");
await (async group.wait() catch @panic("memory"));
assert(count == 11);
testing.expect(count == 11);
var another = Group(anyerror!void).init(loop);
another.add(async somethingElse() catch @panic("memory")) catch @panic("memory");
another.call(doSomethingThatFails) catch @panic("memory");
std.debug.assertError(await (async another.wait() catch @panic("memory")), error.ItBroke);
testing.expectError(error.ItBroke, await (async another.wait() catch @panic("memory")));
}
async fn sleepALittle(count: *usize) void {

View File

@ -1,6 +1,7 @@
const std = @import("../index.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const testing = std.testing;
const mem = std.mem;
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
@ -141,7 +142,7 @@ test "std.event.Lock" {
defer cancel handle;
loop.run();
assert(mem.eql(i32, shared_test_data, [1]i32{3 * @intCast(i32, shared_test_data.len)} ** shared_test_data.len));
testing.expectEqualSlices(i32, [1]i32{3 * @intCast(i32, shared_test_data.len)} ** shared_test_data.len, shared_test_data);
}
async fn testLock(loop: *Loop, lock: *Lock) void {

View File

@ -1,6 +1,7 @@
const std = @import("../index.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const testing = std.testing;
const mem = std.mem;
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
@ -896,7 +897,7 @@ test "std.event.Loop - call" {
loop.run();
assert(did_it);
testing.expect(did_it);
}
async fn testEventLoop() i32 {
@ -905,6 +906,6 @@ async fn testEventLoop() i32 {
async fn testEventLoop2(h: promise->i32, did_it: *bool) void {
const value = await h;
assert(value == 1234);
testing.expect(value == 1234);
did_it.* = true;
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const testing = std.testing;
const event = std.event;
const mem = std.mem;
const os = std.os;
@ -326,7 +326,7 @@ async fn doAsyncTest(loop: *Loop, address: *const std.net.Address, server: *Serv
var buf: [512]u8 = undefined;
const amt_read = try socket_file.read(buf[0..]);
const msg = buf[0..amt_read];
assert(mem.eql(u8, msg, "hello from server\n"));
testing.expect(mem.eql(u8, msg, "hello from server\n"));
server.close();
}

View File

@ -1,6 +1,7 @@
const std = @import("../index.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const testing = std.testing;
const mem = std.mem;
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
@ -231,7 +232,7 @@ test "std.event.RwLock" {
loop.run();
const expected_result = [1]i32{shared_it_count * @intCast(i32, shared_test_data.len)} ** shared_test_data.len;
assert(mem.eql(i32, shared_test_data, expected_result));
testing.expectEqualSlices(i32, expected_result, shared_test_data);
}
async fn testLock(loop: *Loop, lock: *RwLock) void {
@ -293,7 +294,7 @@ async fn readRunner(lock: *RwLock) void {
const handle = await lock_promise;
defer handle.release();
assert(shared_test_index == 0);
assert(shared_test_data[i] == @intCast(i32, shared_count));
testing.expect(shared_test_index == 0);
testing.expect(shared_test_data[i] == @intCast(i32, shared_count));
}
}

View File

@ -2,7 +2,7 @@ const std = @import("../index.zig");
const math = std.math;
const debug = std.debug;
const assert = debug.assert;
const assertError = debug.assertError;
const testing = std.testing;
const mem = std.mem;
const builtin = @import("builtin");
const errol = @import("errol/index.zig");
@ -588,7 +588,7 @@ pub fn formatFloatDecimal(
}
// Remaining fractional portion, zero-padding if insufficient.
debug.assert(precision >= printed);
assert(precision >= printed);
if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) {
try output(context, float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]);
return;
@ -798,13 +798,13 @@ pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) !T {
}
test "fmt.parseInt" {
assert((parseInt(i32, "-10", 10) catch unreachable) == -10);
assert((parseInt(i32, "+10", 10) catch unreachable) == 10);
assert(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidCharacter);
assert(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidCharacter);
assert(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidCharacter);
assert((parseInt(u8, "255", 10) catch unreachable) == 255);
assert(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow);
testing.expect((parseInt(i32, "-10", 10) catch unreachable) == -10);
testing.expect((parseInt(i32, "+10", 10) catch unreachable) == 10);
testing.expect(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidCharacter);
testing.expect(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidCharacter);
testing.expect(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidCharacter);
testing.expect((parseInt(u8, "255", 10) catch unreachable) == 255);
testing.expect(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow);
}
const ParseUnsignedError = error{
@ -829,30 +829,30 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseUnsigned
}
test "parseUnsigned" {
assert((try parseUnsigned(u16, "050124", 10)) == 50124);
assert((try parseUnsigned(u16, "65535", 10)) == 65535);
assertError(parseUnsigned(u16, "65536", 10), error.Overflow);
testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10));
assert((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff);
assertError(parseUnsigned(u64, "10000000000000000", 16), error.Overflow);
testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff);
testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16));
assert((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF);
testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF);
assert((try parseUnsigned(u7, "1", 10)) == 1);
assert((try parseUnsigned(u7, "1000", 2)) == 8);
testing.expect((try parseUnsigned(u7, "1", 10)) == 1);
testing.expect((try parseUnsigned(u7, "1000", 2)) == 8);
assertError(parseUnsigned(u32, "f", 10), error.InvalidCharacter);
assertError(parseUnsigned(u8, "109", 8), error.InvalidCharacter);
testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10));
testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8));
assert((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747);
testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747);
// these numbers should fit even though the radix itself doesn't fit in the destination type
assert((try parseUnsigned(u1, "0", 10)) == 0);
assert((try parseUnsigned(u1, "1", 10)) == 1);
assertError(parseUnsigned(u1, "2", 10), error.Overflow);
assert((try parseUnsigned(u1, "001", 16)) == 1);
assert((try parseUnsigned(u2, "3", 16)) == 3);
assertError(parseUnsigned(u2, "4", 16), error.Overflow);
testing.expect((try parseUnsigned(u1, "0", 10)) == 0);
testing.expect((try parseUnsigned(u1, "1", 10)) == 1);
testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10));
testing.expect((try parseUnsigned(u1, "001", 16)) == 1);
testing.expect((try parseUnsigned(u2, "3", 16)) == 3);
testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16));
}
pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
@ -910,19 +910,19 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) {
test "buf print int" {
var buffer: [max_int_digits]u8 = undefined;
const buf = buffer[0..];
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(12345678), 10, true, 0), "12345678"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(12345678), 10, true, 0), "12345678"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, 6), "000666"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, 6), "000666"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(42), 10, false, 3), "+42"));
assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-42), 10, false, 3), "-42"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(42), 10, false, 3), "+42"));
testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-42), 10, false, 3), "-42"));
}
fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: usize) []u8 {
@ -939,7 +939,7 @@ test "parse u64 digit too big" {
test "parse unsigned comptime" {
comptime {
assert((try parseUnsigned(usize, "2", 10)) == 2);
testing.expect((try parseUnsigned(usize, "2", 10)) == 2);
}
}
@ -977,17 +977,17 @@ test "fmt.format" {
var context = BufPrintContext{ .remaining = buf1[0..] };
try formatType(1234, "", &context, error{BufferTooSmall}, bufPrintWrite);
var res = buf1[0 .. buf1.len - context.remaining.len];
assert(mem.eql(u8, res, "1234"));
testing.expect(mem.eql(u8, res, "1234"));
context = BufPrintContext{ .remaining = buf1[0..] };
try formatType('a', "c", &context, error{BufferTooSmall}, bufPrintWrite);
res = buf1[0 .. buf1.len - context.remaining.len];
assert(mem.eql(u8, res, "a"));
testing.expect(mem.eql(u8, res, "a"));
context = BufPrintContext{ .remaining = buf1[0..] };
try formatType(0b1100, "b", &context, error{BufferTooSmall}, bufPrintWrite);
res = buf1[0 .. buf1.len - context.remaining.len];
assert(mem.eql(u8, res, "1100"));
testing.expect(mem.eql(u8, res, "1100"));
}
{
const value: [3]u8 = "abc";
@ -1053,19 +1053,19 @@ test "fmt.format" {
var buf1: [32]u8 = undefined;
const value: f32 = 1.34;
const result = try bufPrint(buf1[0..], "f32: {e}\n", value);
assert(mem.eql(u8, result, "f32: 1.34000003e+00\n"));
testing.expect(mem.eql(u8, result, "f32: 1.34000003e+00\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f32 = 12.34;
const result = try bufPrint(buf1[0..], "f32: {e}\n", value);
assert(mem.eql(u8, result, "f32: 1.23400001e+01\n"));
testing.expect(mem.eql(u8, result, "f32: 1.23400001e+01\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = -12.34e10;
const result = try bufPrint(buf1[0..], "f64: {e}\n", value);
assert(mem.eql(u8, result, "f64: -1.234e+11\n"));
testing.expect(mem.eql(u8, result, "f64: -1.234e+11\n"));
}
{
// This fails on release due to a minor rounding difference.
@ -1075,26 +1075,26 @@ test "fmt.format" {
var buf1: [32]u8 = undefined;
const value: f64 = 9.999960e-40;
const result = try bufPrint(buf1[0..], "f64: {e}\n", value);
assert(mem.eql(u8, result, "f64: 9.99996e-40\n"));
testing.expect(mem.eql(u8, result, "f64: 9.99996e-40\n"));
}
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 1.409706e-42;
const result = try bufPrint(buf1[0..], "f64: {e5}\n", value);
assert(mem.eql(u8, result, "f64: 1.40971e-42\n"));
testing.expect(mem.eql(u8, result, "f64: 1.40971e-42\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = @bitCast(f32, u32(814313563));
const result = try bufPrint(buf1[0..], "f64: {e5}\n", value);
assert(mem.eql(u8, result, "f64: 1.00000e-09\n"));
testing.expect(mem.eql(u8, result, "f64: 1.00000e-09\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = @bitCast(f32, u32(1006632960));
const result = try bufPrint(buf1[0..], "f64: {e5}\n", value);
assert(mem.eql(u8, result, "f64: 7.81250e-03\n"));
testing.expect(mem.eql(u8, result, "f64: 7.81250e-03\n"));
}
{
// libc rounds 1.000005e+05 to 1.00000e+05 but zig does 1.00001e+05.
@ -1102,47 +1102,47 @@ test "fmt.format" {
var buf1: [32]u8 = undefined;
const value: f64 = @bitCast(f32, u32(1203982400));
const result = try bufPrint(buf1[0..], "f64: {e5}\n", value);
assert(mem.eql(u8, result, "f64: 1.00001e+05\n"));
testing.expect(mem.eql(u8, result, "f64: 1.00001e+05\n"));
}
{
var buf1: [32]u8 = undefined;
const result = try bufPrint(buf1[0..], "f64: {}\n", math.nan_f64);
assert(mem.eql(u8, result, "f64: nan\n"));
testing.expect(mem.eql(u8, result, "f64: nan\n"));
}
if (builtin.arch != builtin.Arch.armv8) {
// negative nan is not defined by IEE 754,
// and ARM thus normalizes it to positive nan
var buf1: [32]u8 = undefined;
const result = try bufPrint(buf1[0..], "f64: {}\n", -math.nan_f64);
assert(mem.eql(u8, result, "f64: -nan\n"));
testing.expect(mem.eql(u8, result, "f64: -nan\n"));
}
{
var buf1: [32]u8 = undefined;
const result = try bufPrint(buf1[0..], "f64: {}\n", math.inf_f64);
assert(mem.eql(u8, result, "f64: inf\n"));
testing.expect(mem.eql(u8, result, "f64: inf\n"));
}
{
var buf1: [32]u8 = undefined;
const result = try bufPrint(buf1[0..], "f64: {}\n", -math.inf_f64);
assert(mem.eql(u8, result, "f64: -inf\n"));
testing.expect(mem.eql(u8, result, "f64: -inf\n"));
}
{
var buf1: [64]u8 = undefined;
const value: f64 = 1.52314e+29;
const result = try bufPrint(buf1[0..], "f64: {.}\n", value);
assert(mem.eql(u8, result, "f64: 152314000000000000000000000000\n"));
testing.expect(mem.eql(u8, result, "f64: 152314000000000000000000000000\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f32 = 1.1234;
const result = try bufPrint(buf1[0..], "f32: {.1}\n", value);
assert(mem.eql(u8, result, "f32: 1.1\n"));
testing.expect(mem.eql(u8, result, "f32: 1.1\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f32 = 1234.567;
const result = try bufPrint(buf1[0..], "f32: {.2}\n", value);
assert(mem.eql(u8, result, "f32: 1234.57\n"));
testing.expect(mem.eql(u8, result, "f32: 1234.57\n"));
}
{
var buf1: [32]u8 = undefined;
@ -1150,92 +1150,92 @@ test "fmt.format" {
const result = try bufPrint(buf1[0..], "f32: {.4}\n", value);
// -11.1234 is converted to f64 -11.12339... internally (errol3() function takes f64).
// -11.12339... is rounded back up to -11.1234
assert(mem.eql(u8, result, "f32: -11.1234\n"));
testing.expect(mem.eql(u8, result, "f32: -11.1234\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f32 = 91.12345;
const result = try bufPrint(buf1[0..], "f32: {.5}\n", value);
assert(mem.eql(u8, result, "f32: 91.12345\n"));
testing.expect(mem.eql(u8, result, "f32: 91.12345\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 91.12345678901235;
const result = try bufPrint(buf1[0..], "f64: {.10}\n", value);
assert(mem.eql(u8, result, "f64: 91.1234567890\n"));
testing.expect(mem.eql(u8, result, "f64: 91.1234567890\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 0.0;
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 0.00000\n"));
testing.expect(mem.eql(u8, result, "f64: 0.00000\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 5.700;
const result = try bufPrint(buf1[0..], "f64: {.0}\n", value);
assert(mem.eql(u8, result, "f64: 6\n"));
testing.expect(mem.eql(u8, result, "f64: 6\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 9.999;
const result = try bufPrint(buf1[0..], "f64: {.1}\n", value);
assert(mem.eql(u8, result, "f64: 10.0\n"));
testing.expect(mem.eql(u8, result, "f64: 10.0\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 1.0;
const result = try bufPrint(buf1[0..], "f64: {.3}\n", value);
assert(mem.eql(u8, result, "f64: 1.000\n"));
testing.expect(mem.eql(u8, result, "f64: 1.000\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 0.0003;
const result = try bufPrint(buf1[0..], "f64: {.8}\n", value);
assert(mem.eql(u8, result, "f64: 0.00030000\n"));
testing.expect(mem.eql(u8, result, "f64: 0.00030000\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 1.40130e-45;
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 0.00000\n"));
testing.expect(mem.eql(u8, result, "f64: 0.00000\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = 9.999960e-40;
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 0.00000\n"));
testing.expect(mem.eql(u8, result, "f64: 0.00000\n"));
}
// libc checks
{
var buf1: [32]u8 = undefined;
const value: f64 = f64(@bitCast(f32, u32(916964781)));
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 0.00001\n"));
testing.expect(mem.eql(u8, result, "f64: 0.00001\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = f64(@bitCast(f32, u32(925353389)));
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 0.00001\n"));
testing.expect(mem.eql(u8, result, "f64: 0.00001\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = f64(@bitCast(f32, u32(1036831278)));
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 0.10000\n"));
testing.expect(mem.eql(u8, result, "f64: 0.10000\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = f64(@bitCast(f32, u32(1065353133)));
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 1.00000\n"));
testing.expect(mem.eql(u8, result, "f64: 1.00000\n"));
}
{
var buf1: [32]u8 = undefined;
const value: f64 = f64(@bitCast(f32, u32(1092616192)));
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 10.00000\n"));
testing.expect(mem.eql(u8, result, "f64: 10.00000\n"));
}
// libc differences
{
@ -1245,7 +1245,7 @@ test "fmt.format" {
// floats of the form x.yyyy25 on a precision point.
const value: f64 = f64(@bitCast(f32, u32(1015021568)));
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 0.01563\n"));
testing.expect(mem.eql(u8, result, "f64: 0.01563\n"));
}
// std-windows-x86_64-Debug-bare test case fails
{
@ -1255,7 +1255,7 @@ test "fmt.format" {
var buf1: [32]u8 = undefined;
const value: f64 = f64(@bitCast(f32, u32(1518338049)));
const result = try bufPrint(buf1[0..], "f64: {.5}\n", value);
assert(mem.eql(u8, result, "f64: 18014400656965630.00000\n"));
testing.expect(mem.eql(u8, result, "f64: 18014400656965630.00000\n"));
}
//custom type format
{
@ -1336,10 +1336,10 @@ test "fmt.format" {
var buf: [100]u8 = undefined;
const uu_result = try bufPrint(buf[0..], "{}", uu_inst);
debug.assert(mem.eql(u8, uu_result[0..3], "UU@"));
testing.expect(mem.eql(u8, uu_result[0..3], "UU@"));
const eu_result = try bufPrint(buf[0..], "{}", eu_inst);
debug.assert(mem.eql(u8, uu_result[0..3], "EU@"));
testing.expect(mem.eql(u8, uu_result[0..3], "EU@"));
}
//enum format
{
@ -1398,11 +1398,11 @@ pub fn trim(buf: []const u8) []const u8 {
}
test "fmt.trim" {
assert(mem.eql(u8, "abc", trim("\n abc \t")));
assert(mem.eql(u8, "", trim(" ")));
assert(mem.eql(u8, "", trim("")));
assert(mem.eql(u8, "abc", trim(" abc")));
assert(mem.eql(u8, "abc", trim("abc ")));
testing.expect(mem.eql(u8, "abc", trim("\n abc \t")));
testing.expect(mem.eql(u8, "", trim(" ")));
testing.expect(mem.eql(u8, "", trim("")));
testing.expect(mem.eql(u8, "abc", trim(" abc")));
testing.expect(mem.eql(u8, "abc", trim("abc ")));
}
pub fn isWhiteSpace(byte: u8) bool {

View File

@ -4,7 +4,7 @@
// https://github.com/madler/zlib/blob/master/adler32.c
const std = @import("../index.zig");
const debug = std.debug;
const testing = std.testing;
pub const Adler32 = struct {
const base = 65521;
@ -89,19 +89,19 @@ pub const Adler32 = struct {
};
test "adler32 sanity" {
debug.assert(Adler32.hash("a") == 0x620062);
debug.assert(Adler32.hash("example") == 0xbc002ed);
testing.expect(Adler32.hash("a") == 0x620062);
testing.expect(Adler32.hash("example") == 0xbc002ed);
}
test "adler32 long" {
const long1 = []u8{1} ** 1024;
debug.assert(Adler32.hash(long1[0..]) == 0x06780401);
testing.expect(Adler32.hash(long1[0..]) == 0x06780401);
const long2 = []u8{1} ** 1025;
debug.assert(Adler32.hash(long2[0..]) == 0x0a7a0402);
testing.expect(Adler32.hash(long2[0..]) == 0x0a7a0402);
}
test "adler32 very long" {
const long = []u8{1} ** 5553;
debug.assert(Adler32.hash(long[0..]) == 0x707f15b2);
testing.expect(Adler32.hash(long[0..]) == 0x707f15b2);
}

View File

@ -7,6 +7,7 @@
const std = @import("../index.zig");
const debug = std.debug;
const testing = std.testing;
pub const Polynomial = struct {
const IEEE = 0xedb88320;
@ -101,17 +102,17 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
test "crc32 ieee" {
const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE);
debug.assert(Crc32Ieee.hash("") == 0x00000000);
debug.assert(Crc32Ieee.hash("a") == 0xe8b7be43);
debug.assert(Crc32Ieee.hash("abc") == 0x352441c2);
testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
testing.expect(Crc32Ieee.hash("abc") == 0x352441c2);
}
test "crc32 castagnoli" {
const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli);
debug.assert(Crc32Castagnoli.hash("") == 0x00000000);
debug.assert(Crc32Castagnoli.hash("a") == 0xc1d04330);
debug.assert(Crc32Castagnoli.hash("abc") == 0x364b3fb7);
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
testing.expect(Crc32Castagnoli.hash("abc") == 0x364b3fb7);
}
// half-byte lookup table implementation.
@ -165,15 +166,15 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type {
test "small crc32 ieee" {
const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE);
debug.assert(Crc32Ieee.hash("") == 0x00000000);
debug.assert(Crc32Ieee.hash("a") == 0xe8b7be43);
debug.assert(Crc32Ieee.hash("abc") == 0x352441c2);
testing.expect(Crc32Ieee.hash("") == 0x00000000);
testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43);
testing.expect(Crc32Ieee.hash("abc") == 0x352441c2);
}
test "small crc32 castagnoli" {
const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli);
debug.assert(Crc32Castagnoli.hash("") == 0x00000000);
debug.assert(Crc32Castagnoli.hash("a") == 0xc1d04330);
debug.assert(Crc32Castagnoli.hash("abc") == 0x364b3fb7);
testing.expect(Crc32Castagnoli.hash("") == 0x00000000);
testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330);
testing.expect(Crc32Castagnoli.hash("abc") == 0x364b3fb7);
}

View File

@ -5,7 +5,7 @@
// https://tools.ietf.org/html/draft-eastlake-fnv-14
const std = @import("../index.zig");
const debug = std.debug;
const testing = std.testing;
pub const Fnv1a_32 = Fnv1a(u32, 0x01000193, 0x811c9dc5);
pub const Fnv1a_64 = Fnv1a(u64, 0x100000001b3, 0xcbf29ce484222325);
@ -41,18 +41,18 @@ fn Fnv1a(comptime T: type, comptime prime: T, comptime offset: T) type {
}
test "fnv1a-32" {
debug.assert(Fnv1a_32.hash("") == 0x811c9dc5);
debug.assert(Fnv1a_32.hash("a") == 0xe40c292c);
debug.assert(Fnv1a_32.hash("foobar") == 0xbf9cf968);
testing.expect(Fnv1a_32.hash("") == 0x811c9dc5);
testing.expect(Fnv1a_32.hash("a") == 0xe40c292c);
testing.expect(Fnv1a_32.hash("foobar") == 0xbf9cf968);
}
test "fnv1a-64" {
debug.assert(Fnv1a_64.hash("") == 0xcbf29ce484222325);
debug.assert(Fnv1a_64.hash("a") == 0xaf63dc4c8601ec8c);
debug.assert(Fnv1a_64.hash("foobar") == 0x85944171f73967e8);
testing.expect(Fnv1a_64.hash("") == 0xcbf29ce484222325);
testing.expect(Fnv1a_64.hash("a") == 0xaf63dc4c8601ec8c);
testing.expect(Fnv1a_64.hash("foobar") == 0x85944171f73967e8);
}
test "fnv1a-128" {
debug.assert(Fnv1a_128.hash("") == 0x6c62272e07bb014262b821756295c58d);
debug.assert(Fnv1a_128.hash("a") == 0xd228cb696f1a8caf78912b704e4a8964);
testing.expect(Fnv1a_128.hash("") == 0x6c62272e07bb014262b821756295c58d);
testing.expect(Fnv1a_128.hash("a") == 0xd228cb696f1a8caf78912b704e4a8964);
}

View File

@ -6,7 +6,8 @@
// https://131002.net/siphash/
const std = @import("../index.zig");
const debug = std.debug;
const assert = std.debug.assert;
const testing = std.testing;
const math = std.math;
const mem = std.mem;
@ -21,8 +22,8 @@ pub fn SipHash128(comptime c_rounds: usize, comptime d_rounds: usize) type {
}
fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type {
debug.assert(T == u64 or T == u128);
debug.assert(c_rounds > 0 and d_rounds > 0);
assert(T == u64 or T == u128);
assert(c_rounds > 0 and d_rounds > 0);
return struct {
const Self = @This();
@ -40,7 +41,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
msg_len: u8,
pub fn init(key: []const u8) Self {
debug.assert(key.len >= 16);
assert(key.len >= 16);
const k0 = mem.readIntSliceLittle(u64, key[0..8]);
const k1 = mem.readIntSliceLittle(u64, key[8..16]);
@ -119,7 +120,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
}
fn round(d: *Self, b: []const u8) void {
debug.assert(b.len == 8);
assert(b.len == 8);
const m = mem.readIntSliceLittle(u64, b[0..]);
d.v3 ^= m;
@ -236,7 +237,7 @@ test "siphash64-2-4 sanity" {
buffer[i] = @intCast(u8, i);
const expected = mem.readIntLittle(u64, &vector);
debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);
testing.expect(siphash.hash(test_key, buffer[0..i]) == expected);
}
}
@ -315,6 +316,6 @@ test "siphash128-2-4 sanity" {
buffer[i] = @intCast(u8, i);
const expected = mem.readIntLittle(u128, &vector);
debug.assert(siphash.hash(test_key, buffer[0..i]) == expected);
testing.expect(siphash.hash(test_key, buffer[0..i]) == expected);
}
}

View File

@ -1,6 +1,7 @@
const std = @import("index.zig");
const debug = std.debug;
const assert = debug.assert;
const testing = std.testing;
const math = std.math;
const mem = std.mem;
const Allocator = mem.Allocator;
@ -342,37 +343,37 @@ test "basic hash map usage" {
var map = AutoHashMap(i32, i32).init(&direct_allocator.allocator);
defer map.deinit();
assert((try map.put(1, 11)) == null);
assert((try map.put(2, 22)) == null);
assert((try map.put(3, 33)) == null);
assert((try map.put(4, 44)) == null);
assert((try map.put(5, 55)) == null);
testing.expect((try map.put(1, 11)) == null);
testing.expect((try map.put(2, 22)) == null);
testing.expect((try map.put(3, 33)) == null);
testing.expect((try map.put(4, 44)) == null);
testing.expect((try map.put(5, 55)) == null);
assert((try map.put(5, 66)).?.value == 55);
assert((try map.put(5, 55)).?.value == 66);
testing.expect((try map.put(5, 66)).?.value == 55);
testing.expect((try map.put(5, 55)).?.value == 66);
const gop1 = try map.getOrPut(5);
assert(gop1.found_existing == true);
assert(gop1.kv.value == 55);
testing.expect(gop1.found_existing == true);
testing.expect(gop1.kv.value == 55);
gop1.kv.value = 77;
assert(map.get(5).?.value == 77);
testing.expect(map.get(5).?.value == 77);
const gop2 = try map.getOrPut(99);
assert(gop2.found_existing == false);
testing.expect(gop2.found_existing == false);
gop2.kv.value = 42;
assert(map.get(99).?.value == 42);
testing.expect(map.get(99).?.value == 42);
const gop3 = try map.getOrPutValue(5, 5);
assert(gop3.value == 77);
testing.expect(gop3.value == 77);
const gop4 = try map.getOrPutValue(100, 41);
assert(gop4.value == 41);
testing.expect(gop4.value == 41);
assert(map.contains(2));
assert(map.get(2).?.value == 22);
testing.expect(map.contains(2));
testing.expect(map.get(2).?.value == 22);
_ = map.remove(2);
assert(map.remove(2) == null);
assert(map.get(2) == null);
testing.expect(map.remove(2) == null);
testing.expect(map.get(2) == null);
}
test "iterator hash map" {
@ -382,9 +383,9 @@ test "iterator hash map" {
var reset_map = AutoHashMap(i32, i32).init(&direct_allocator.allocator);
defer reset_map.deinit();
assert((try reset_map.put(1, 11)) == null);
assert((try reset_map.put(2, 22)) == null);
assert((try reset_map.put(3, 33)) == null);
testing.expect((try reset_map.put(1, 11)) == null);
testing.expect((try reset_map.put(2, 22)) == null);
testing.expect((try reset_map.put(3, 33)) == null);
var keys = []i32{
3,
@ -400,26 +401,26 @@ test "iterator hash map" {
var it = reset_map.iterator();
var count: usize = 0;
while (it.next()) |next| {
assert(next.key == keys[count]);
assert(next.value == values[count]);
testing.expect(next.key == keys[count]);
testing.expect(next.value == values[count]);
count += 1;
}
assert(count == 3);
assert(it.next() == null);
testing.expect(count == 3);
testing.expect(it.next() == null);
it.reset();
count = 0;
while (it.next()) |next| {
assert(next.key == keys[count]);
assert(next.value == values[count]);
testing.expect(next.key == keys[count]);
testing.expect(next.value == values[count]);
count += 1;
if (count == 2) break;
}
it.reset();
var entry = it.next().?;
assert(entry.key == keys[0]);
assert(entry.value == values[0]);
testing.expect(entry.key == keys[0]);
testing.expect(entry.value == values[0]);
}
pub fn getHashPtrAddrFn(comptime K: type) (fn (K) u32) {

View File

@ -1,6 +1,7 @@
const std = @import("index.zig");
const debug = std.debug;
const assert = debug.assert;
const testing = std.testing;
const mem = std.mem;
const os = std.os;
const builtin = @import("builtin");
@ -487,11 +488,11 @@ test "FixedBufferAllocator Reuse memory on realloc" {
var fixed_buffer_allocator = FixedBufferAllocator.init(small_fixed_buffer[0..]);
var slice0 = try fixed_buffer_allocator.allocator.alloc(u8, 5);
assert(slice0.len == 5);
testing.expect(slice0.len == 5);
var slice1 = try fixed_buffer_allocator.allocator.realloc(u8, slice0, 10);
assert(slice1.ptr == slice0.ptr);
assert(slice1.len == 10);
debug.assertError(fixed_buffer_allocator.allocator.realloc(u8, slice1, 11), error.OutOfMemory);
testing.expect(slice1.ptr == slice0.ptr);
testing.expect(slice1.len == 10);
testing.expectError(error.OutOfMemory, fixed_buffer_allocator.allocator.realloc(u8, slice1, 11));
}
// check that we don't re-use the memory if it's not the most recent block
{
@ -502,10 +503,10 @@ test "FixedBufferAllocator Reuse memory on realloc" {
slice0[1] = 2;
var slice1 = try fixed_buffer_allocator.allocator.alloc(u8, 2);
var slice2 = try fixed_buffer_allocator.allocator.realloc(u8, slice0, 4);
assert(slice0.ptr != slice2.ptr);
assert(slice1.ptr != slice2.ptr);
assert(slice2[0] == 1);
assert(slice2[1] == 2);
testing.expect(slice0.ptr != slice2.ptr);
testing.expect(slice1.ptr != slice2.ptr);
testing.expect(slice2[0] == 1);
testing.expect(slice2[1] == 2);
}
}
@ -519,28 +520,28 @@ test "ThreadSafeFixedBufferAllocator" {
fn testAllocator(allocator: *mem.Allocator) !void {
var slice = try allocator.alloc(*i32, 100);
assert(slice.len == 100);
testing.expect(slice.len == 100);
for (slice) |*item, i| {
item.* = try allocator.create(i32);
item.*.* = @intCast(i32, i);
}
slice = try allocator.realloc(*i32, slice, 20000);
assert(slice.len == 20000);
testing.expect(slice.len == 20000);
for (slice[0..100]) |item, i| {
assert(item.* == @intCast(i32, i));
testing.expect(item.* == @intCast(i32, i));
allocator.destroy(item);
}
slice = try allocator.realloc(*i32, slice, 50);
assert(slice.len == 50);
testing.expect(slice.len == 50);
slice = try allocator.realloc(*i32, slice, 25);
assert(slice.len == 25);
testing.expect(slice.len == 25);
slice = try allocator.realloc(*i32, slice, 0);
assert(slice.len == 0);
testing.expect(slice.len == 0);
slice = try allocator.realloc(*i32, slice, 10);
assert(slice.len == 10);
testing.expect(slice.len == 10);
allocator.free(slice);
}
@ -548,25 +549,25 @@ fn testAllocator(allocator: *mem.Allocator) !void {
fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !void {
// initial
var slice = try allocator.alignedAlloc(u8, alignment, 10);
assert(slice.len == 10);
testing.expect(slice.len == 10);
// grow
slice = try allocator.alignedRealloc(u8, alignment, slice, 100);
assert(slice.len == 100);
testing.expect(slice.len == 100);
// shrink
slice = try allocator.alignedRealloc(u8, alignment, slice, 10);
assert(slice.len == 10);
testing.expect(slice.len == 10);
// go to zero
slice = try allocator.alignedRealloc(u8, alignment, slice, 0);
assert(slice.len == 0);
testing.expect(slice.len == 0);
// realloc from zero
slice = try allocator.alignedRealloc(u8, alignment, slice, 100);
assert(slice.len == 100);
testing.expect(slice.len == 100);
// shrink with shrink
slice = allocator.alignedShrink(u8, alignment, slice, 10);
assert(slice.len == 10);
testing.expect(slice.len == 10);
// shrink to zero
slice = allocator.alignedShrink(u8, alignment, slice, 0);
assert(slice.len == 0);
testing.expect(slice.len == 0);
}
fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void {
@ -581,19 +582,19 @@ fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!vo
_ = @shlWithOverflow(usize, ~usize(0), USizeShift(@ctz(large_align)), &align_mask);
var slice = try allocator.allocFn(allocator, 500, large_align);
debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
slice = try allocator.reallocFn(allocator, slice, 100, large_align);
debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
slice = try allocator.reallocFn(allocator, slice, 5000, large_align);
debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
slice = try allocator.reallocFn(allocator, slice, 10, large_align);
debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
slice = try allocator.reallocFn(allocator, slice, 20000, large_align);
debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr));
allocator.free(slice);
}

View File

@ -31,6 +31,7 @@ pub const hash_map = @import("hash_map.zig");
pub const heap = @import("heap.zig");
pub const io = @import("io.zig");
pub const json = @import("json.zig");
pub const lazyInit = @import("lazy_init.zig").lazyInit;
pub const macho = @import("macho.zig");
pub const math = @import("math/index.zig");
pub const mem = @import("mem.zig");
@ -41,11 +42,10 @@ pub const pdb = @import("pdb.zig");
pub const rand = @import("rand/index.zig");
pub const rb = @import("rb.zig");
pub const sort = @import("sort.zig");
pub const testing = @import("testing.zig");
pub const unicode = @import("unicode.zig");
pub const zig = @import("zig/index.zig");
pub const lazyInit = @import("lazy_init.zig").lazyInit;
test "std" {
// run tests from these
_ = @import("array_list.zig");
@ -60,7 +60,6 @@ test "std" {
_ = @import("segmented_list.zig");
_ = @import("spinlock.zig");
_ = @import("dynamic_library.zig");
_ = @import("base64.zig");
_ = @import("build.zig");
_ = @import("c/index.zig");
@ -69,24 +68,26 @@ test "std" {
_ = @import("cstr.zig");
_ = @import("debug/index.zig");
_ = @import("dwarf.zig");
_ = @import("dynamic_library.zig");
_ = @import("elf.zig");
_ = @import("empty.zig");
_ = @import("event.zig");
_ = @import("fmt/index.zig");
_ = @import("hash/index.zig");
_ = @import("heap.zig");
_ = @import("io.zig");
_ = @import("json.zig");
_ = @import("lazy_init.zig");
_ = @import("macho.zig");
_ = @import("math/index.zig");
_ = @import("meta/index.zig");
_ = @import("mem.zig");
_ = @import("meta/index.zig");
_ = @import("net.zig");
_ = @import("heap.zig");
_ = @import("os/index.zig");
_ = @import("rand/index.zig");
_ = @import("pdb.zig");
_ = @import("rand/index.zig");
_ = @import("sort.zig");
_ = @import("testing.zig");
_ = @import("unicode.zig");
_ = @import("zig/index.zig");
_ = @import("lazy_init.zig");
}

View File

@ -13,6 +13,7 @@ const trait = meta.trait;
const Buffer = std.Buffer;
const fmt = std.fmt;
const File = std.os.File;
const testing = std.testing;
const is_posix = builtin.os != builtin.Os.windows;
const is_windows = builtin.os == builtin.Os.windows;
@ -664,7 +665,7 @@ test "io.SliceOutStream" {
const stream = &slice_stream.stream;
try stream.print("{}{}!", "Hello", "World");
debug.assertOrPanic(mem.eql(u8, "HelloWorld!", slice_stream.getWritten()));
testing.expectEqualSlices(u8, "HelloWorld!", slice_stream.getWritten());
}
var null_out_stream_state = NullOutStream.init();
@ -726,7 +727,7 @@ test "io.CountingOutStream" {
const bytes = "yay" ** 10000;
stream.write(bytes) catch unreachable;
debug.assertOrPanic(counting_stream.bytes_written == bytes.len);
testing.expect(counting_stream.bytes_written == bytes.len);
}
pub fn BufferedOutStream(comptime Error: type) type {
@ -1014,10 +1015,10 @@ test "io.readLineFrom" {
);
const stream = &mem_stream.stream;
debug.assertOrPanic(mem.eql(u8, "Line 1", try readLineFrom(stream, &buf)));
debug.assertOrPanic(mem.eql(u8, "Line 22", try readLineFrom(stream, &buf)));
debug.assertError(readLineFrom(stream, &buf), error.EndOfStream);
debug.assertOrPanic(mem.eql(u8, buf.toSlice(), "Line 1Line 22Line 333"));
testing.expectEqualSlices(u8, "Line 1", try readLineFrom(stream, &buf));
testing.expectEqualSlices(u8, "Line 22", try readLineFrom(stream, &buf));
testing.expectError(error.EndOfStream, readLineFrom(stream, &buf));
testing.expectEqualSlices(u8, "Line 1Line 22Line 333", buf.toSlice());
}
pub fn readLineSlice(slice: []u8) ![]u8 {
@ -1045,8 +1046,8 @@ test "io.readLineSliceFrom" {
);
const stream = &mem_stream.stream;
debug.assertOrPanic(mem.eql(u8, "Line 1", try readLineSliceFrom(stream, buf[0..])));
debug.assertError(readLineSliceFrom(stream, buf[0..]), error.OutOfMemory);
testing.expectEqualSlices(u8, "Line 1", try readLineSliceFrom(stream, buf[0..]));
testing.expectError(error.OutOfMemory, readLineSliceFrom(stream, buf[0..]));
}
/// Creates a deserializer that deserializes types from any stream.

View File

@ -3,8 +3,8 @@ const io = std.io;
const meta = std.meta;
const trait = std.trait;
const DefaultPrng = std.rand.DefaultPrng;
const assert = std.debug.assert;
const assertError = std.debug.assertError;
const expect = std.testing.expect;
const expectError = std.testing.expectError;
const mem = std.mem;
const os = std.os;
const builtin = @import("builtin");
@ -35,7 +35,7 @@ test "write a file, read it, then delete it" {
const file_size = try file.getEndPos();
const expected_file_size = "begin".len + data.len + "end".len;
assert(file_size == expected_file_size);
expect(file_size == expected_file_size);
var file_in_stream = file.inStream();
var buf_stream = io.BufferedInStream(os.File.ReadError).init(&file_in_stream.stream);
@ -43,9 +43,9 @@ test "write a file, read it, then delete it" {
const contents = try st.readAllAlloc(allocator, 2 * 1024);
defer allocator.free(contents);
assert(mem.eql(u8, contents[0.."begin".len], "begin"));
assert(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data));
assert(mem.eql(u8, contents[contents.len - "end".len ..], "end"));
expect(mem.eql(u8, contents[0.."begin".len], "begin"));
expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data));
expect(mem.eql(u8, contents[contents.len - "end".len ..], "end"));
}
try os.deleteFile(tmp_file_name);
}
@ -61,7 +61,7 @@ test "BufferOutStream" {
const y: i32 = 1234;
try buf_stream.print("x: {}\ny: {}\n", x, y);
assert(mem.eql(u8, buffer.toSlice(), "x: 42\ny: 1234\n"));
expect(mem.eql(u8, buffer.toSlice(), "x: 42\ny: 1234\n"));
}
test "SliceInStream" {
@ -71,15 +71,15 @@ test "SliceInStream" {
var dest: [4]u8 = undefined;
var read = try ss.stream.read(dest[0..4]);
assert(read == 4);
assert(mem.eql(u8, dest[0..4], bytes[0..4]));
expect(read == 4);
expect(mem.eql(u8, dest[0..4], bytes[0..4]));
read = try ss.stream.read(dest[0..4]);
assert(read == 3);
assert(mem.eql(u8, dest[0..3], bytes[4..7]));
expect(read == 3);
expect(mem.eql(u8, dest[0..3], bytes[4..7]));
read = try ss.stream.read(dest[0..4]);
assert(read == 0);
expect(read == 0);
}
test "PeekStream" {
@ -93,26 +93,26 @@ test "PeekStream" {
ps.putBackByte(10);
var read = try ps.stream.read(dest[0..4]);
assert(read == 4);
assert(dest[0] == 10);
assert(dest[1] == 9);
assert(mem.eql(u8, dest[2..4], bytes[0..2]));
expect(read == 4);
expect(dest[0] == 10);
expect(dest[1] == 9);
expect(mem.eql(u8, dest[2..4], bytes[0..2]));
read = try ps.stream.read(dest[0..4]);
assert(read == 4);
assert(mem.eql(u8, dest[0..4], bytes[2..6]));
expect(read == 4);
expect(mem.eql(u8, dest[0..4], bytes[2..6]));
read = try ps.stream.read(dest[0..4]);
assert(read == 2);
assert(mem.eql(u8, dest[0..2], bytes[6..8]));
expect(read == 2);
expect(mem.eql(u8, dest[0..2], bytes[6..8]));
ps.putBackByte(11);
ps.putBackByte(12);
read = try ps.stream.read(dest[0..4]);
assert(read == 2);
assert(dest[0] == 12);
assert(dest[1] == 11);
expect(read == 2);
expect(dest[0] == 12);
expect(dest[1] == 11);
}
test "SliceOutStream" {
@ -120,19 +120,19 @@ test "SliceOutStream" {
var ss = io.SliceOutStream.init(buffer[0..]);
try ss.stream.write("Hello");
assert(mem.eql(u8, ss.getWritten(), "Hello"));
expect(mem.eql(u8, ss.getWritten(), "Hello"));
try ss.stream.write("world");
assert(mem.eql(u8, ss.getWritten(), "Helloworld"));
expect(mem.eql(u8, ss.getWritten(), "Helloworld"));
assertError(ss.stream.write("!"), error.OutOfSpace);
assert(mem.eql(u8, ss.getWritten(), "Helloworld"));
expectError(error.OutOfSpace, ss.stream.write("!"));
expect(mem.eql(u8, ss.getWritten(), "Helloworld"));
ss.reset();
assert(ss.getWritten().len == 0);
expect(ss.getWritten().len == 0);
assertError(ss.stream.write("Hello world!"), error.OutOfSpace);
assert(mem.eql(u8, ss.getWritten(), "Hello worl"));
expectError(error.OutOfSpace, ss.stream.write("Hello world!"));
expect(mem.eql(u8, ss.getWritten(), "Hello worl"));
}
test "BitInStream" {
@ -145,66 +145,66 @@ test "BitInStream" {
var out_bits: usize = undefined;
assert(1 == try bit_stream_be.readBits(u2, 1, &out_bits));
assert(out_bits == 1);
assert(2 == try bit_stream_be.readBits(u5, 2, &out_bits));
assert(out_bits == 2);
assert(3 == try bit_stream_be.readBits(u128, 3, &out_bits));
assert(out_bits == 3);
assert(4 == try bit_stream_be.readBits(u8, 4, &out_bits));
assert(out_bits == 4);
assert(5 == try bit_stream_be.readBits(u9, 5, &out_bits));
assert(out_bits == 5);
assert(1 == try bit_stream_be.readBits(u1, 1, &out_bits));
assert(out_bits == 1);
expect(1 == try bit_stream_be.readBits(u2, 1, &out_bits));
expect(out_bits == 1);
expect(2 == try bit_stream_be.readBits(u5, 2, &out_bits));
expect(out_bits == 2);
expect(3 == try bit_stream_be.readBits(u128, 3, &out_bits));
expect(out_bits == 3);
expect(4 == try bit_stream_be.readBits(u8, 4, &out_bits));
expect(out_bits == 4);
expect(5 == try bit_stream_be.readBits(u9, 5, &out_bits));
expect(out_bits == 5);
expect(1 == try bit_stream_be.readBits(u1, 1, &out_bits));
expect(out_bits == 1);
mem_in_be.pos = 0;
bit_stream_be.bit_count = 0;
assert(0b110011010000101 == try bit_stream_be.readBits(u15, 15, &out_bits));
assert(out_bits == 15);
expect(0b110011010000101 == try bit_stream_be.readBits(u15, 15, &out_bits));
expect(out_bits == 15);
mem_in_be.pos = 0;
bit_stream_be.bit_count = 0;
assert(0b1100110100001011 == try bit_stream_be.readBits(u16, 16, &out_bits));
assert(out_bits == 16);
expect(0b1100110100001011 == try bit_stream_be.readBits(u16, 16, &out_bits));
expect(out_bits == 16);
_ = try bit_stream_be.readBits(u0, 0, &out_bits);
assert(0 == try bit_stream_be.readBits(u1, 1, &out_bits));
assert(out_bits == 0);
assertError(bit_stream_be.readBitsNoEof(u1, 1), error.EndOfStream);
expect(0 == try bit_stream_be.readBits(u1, 1, &out_bits));
expect(out_bits == 0);
expectError(error.EndOfStream, bit_stream_be.readBitsNoEof(u1, 1));
var mem_in_le = io.SliceInStream.init(mem_le[0..]);
var bit_stream_le = io.BitInStream(builtin.Endian.Little, InError).init(&mem_in_le.stream);
assert(1 == try bit_stream_le.readBits(u2, 1, &out_bits));
assert(out_bits == 1);
assert(2 == try bit_stream_le.readBits(u5, 2, &out_bits));
assert(out_bits == 2);
assert(3 == try bit_stream_le.readBits(u128, 3, &out_bits));
assert(out_bits == 3);
assert(4 == try bit_stream_le.readBits(u8, 4, &out_bits));
assert(out_bits == 4);
assert(5 == try bit_stream_le.readBits(u9, 5, &out_bits));
assert(out_bits == 5);
assert(1 == try bit_stream_le.readBits(u1, 1, &out_bits));
assert(out_bits == 1);
expect(1 == try bit_stream_le.readBits(u2, 1, &out_bits));
expect(out_bits == 1);
expect(2 == try bit_stream_le.readBits(u5, 2, &out_bits));
expect(out_bits == 2);
expect(3 == try bit_stream_le.readBits(u128, 3, &out_bits));
expect(out_bits == 3);
expect(4 == try bit_stream_le.readBits(u8, 4, &out_bits));
expect(out_bits == 4);
expect(5 == try bit_stream_le.readBits(u9, 5, &out_bits));
expect(out_bits == 5);
expect(1 == try bit_stream_le.readBits(u1, 1, &out_bits));
expect(out_bits == 1);
mem_in_le.pos = 0;
bit_stream_le.bit_count = 0;
assert(0b001010100011101 == try bit_stream_le.readBits(u15, 15, &out_bits));
assert(out_bits == 15);
expect(0b001010100011101 == try bit_stream_le.readBits(u15, 15, &out_bits));
expect(out_bits == 15);
mem_in_le.pos = 0;
bit_stream_le.bit_count = 0;
assert(0b1001010100011101 == try bit_stream_le.readBits(u16, 16, &out_bits));
assert(out_bits == 16);
expect(0b1001010100011101 == try bit_stream_le.readBits(u16, 16, &out_bits));
expect(out_bits == 16);
_ = try bit_stream_le.readBits(u0, 0, &out_bits);
assert(0 == try bit_stream_le.readBits(u1, 1, &out_bits));
assert(out_bits == 0);
assertError(bit_stream_le.readBitsNoEof(u1, 1), error.EndOfStream);
expect(0 == try bit_stream_le.readBits(u1, 1, &out_bits));
expect(out_bits == 0);
expectError(error.EndOfStream, bit_stream_le.readBitsNoEof(u1, 1));
}
test "BitOutStream" {
@ -222,17 +222,17 @@ test "BitOutStream" {
try bit_stream_be.writeBits(u9(5), 5);
try bit_stream_be.writeBits(u1(1), 1);
assert(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001011);
expect(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001011);
mem_out_be.pos = 0;
try bit_stream_be.writeBits(u15(0b110011010000101), 15);
try bit_stream_be.flushBits();
assert(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001010);
expect(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001010);
mem_out_be.pos = 0;
try bit_stream_be.writeBits(u32(0b110011010000101), 16);
assert(mem_be[0] == 0b01100110 and mem_be[1] == 0b10000101);
expect(mem_be[0] == 0b01100110 and mem_be[1] == 0b10000101);
try bit_stream_be.writeBits(u0(0), 0);
@ -246,16 +246,16 @@ test "BitOutStream" {
try bit_stream_le.writeBits(u9(5), 5);
try bit_stream_le.writeBits(u1(1), 1);
assert(mem_le[0] == 0b00011101 and mem_le[1] == 0b10010101);
expect(mem_le[0] == 0b00011101 and mem_le[1] == 0b10010101);
mem_out_le.pos = 0;
try bit_stream_le.writeBits(u15(0b110011010000101), 15);
try bit_stream_le.flushBits();
assert(mem_le[0] == 0b10000101 and mem_le[1] == 0b01100110);
expect(mem_le[0] == 0b10000101 and mem_le[1] == 0b01100110);
mem_out_le.pos = 0;
try bit_stream_le.writeBits(u32(0b1100110100001011), 16);
assert(mem_le[0] == 0b00001011 and mem_le[1] == 0b11001101);
expect(mem_le[0] == 0b00001011 and mem_le[1] == 0b11001101);
try bit_stream_le.writeBits(u0(0), 0);
}
@ -290,20 +290,20 @@ test "BitStreams with File Stream" {
var out_bits: usize = undefined;
assert(1 == try bit_stream.readBits(u2, 1, &out_bits));
assert(out_bits == 1);
assert(2 == try bit_stream.readBits(u5, 2, &out_bits));
assert(out_bits == 2);
assert(3 == try bit_stream.readBits(u128, 3, &out_bits));
assert(out_bits == 3);
assert(4 == try bit_stream.readBits(u8, 4, &out_bits));
assert(out_bits == 4);
assert(5 == try bit_stream.readBits(u9, 5, &out_bits));
assert(out_bits == 5);
assert(1 == try bit_stream.readBits(u1, 1, &out_bits));
assert(out_bits == 1);
expect(1 == try bit_stream.readBits(u2, 1, &out_bits));
expect(out_bits == 1);
expect(2 == try bit_stream.readBits(u5, 2, &out_bits));
expect(out_bits == 2);
expect(3 == try bit_stream.readBits(u128, 3, &out_bits));
expect(out_bits == 3);
expect(4 == try bit_stream.readBits(u8, 4, &out_bits));
expect(out_bits == 4);
expect(5 == try bit_stream.readBits(u9, 5, &out_bits));
expect(out_bits == 5);
expect(1 == try bit_stream.readBits(u1, 1, &out_bits));
expect(out_bits == 1);
assertError(bit_stream.readBitsNoEof(u1, 1), error.EndOfStream);
expectError(error.EndOfStream, bit_stream.readBitsNoEof(u1, 1));
}
try os.deleteFile(tmp_file_name);
}
@ -345,8 +345,8 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa
const S = @IntType(true, i);
const x = try deserializer.deserializeInt(U);
const y = try deserializer.deserializeInt(S);
assert(x == U(i));
if (i != 0) assert(y == S(-1)) else assert(y == 0);
expect(x == U(i));
if (i != 0) expect(y == S(-1)) else expect(y == 0);
}
const u8_bit_count = comptime meta.bitCount(u8);
@ -356,7 +356,7 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa
const extra_packed_byte = @boolToInt(total_bits % u8_bit_count > 0);
const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte;
assert(in.pos == if (is_packed) total_packed_bytes else total_bytes);
expect(in.pos == if (is_packed) total_packed_bytes else total_bytes);
//Verify that empty error set works with serializer.
//deserializer is covered by SliceInStream
@ -408,14 +408,14 @@ fn testIntSerializerDeserializerInfNaN(comptime endian: builtin.Endian,
const inf_check_f64 = try deserializer.deserialize(f64);
//const nan_check_f128 = try deserializer.deserialize(f128);
//const inf_check_f128 = try deserializer.deserialize(f128);
assert(std.math.isNan(nan_check_f16));
assert(std.math.isInf(inf_check_f16));
assert(std.math.isNan(nan_check_f32));
assert(std.math.isInf(inf_check_f32));
assert(std.math.isNan(nan_check_f64));
assert(std.math.isInf(inf_check_f64));
//assert(std.math.isNan(nan_check_f128));
//assert(std.math.isInf(inf_check_f128));
expect(std.math.isNan(nan_check_f16));
expect(std.math.isInf(inf_check_f16));
expect(std.math.isNan(nan_check_f32));
expect(std.math.isInf(inf_check_f32));
expect(std.math.isNan(nan_check_f64));
expect(std.math.isInf(inf_check_f64));
//expect(std.math.isNan(nan_check_f128));
//expect(std.math.isInf(inf_check_f128));
}
test "Serializer/Deserializer Int: Inf/NaN" {
@ -528,7 +528,7 @@ fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packe
try serializer.serialize(my_inst);
const my_copy = try deserializer.deserialize(MyStruct);
assert(meta.eql(my_copy, my_inst));
expect(meta.eql(my_copy, my_inst));
}
test "Serializer/Deserializer generic" {
@ -565,11 +565,11 @@ fn testBadData(comptime endian: builtin.Endian, comptime is_packed: bool) !void
var deserializer = io.Deserializer(endian, is_packed, InError).init(in_stream);
try serializer.serialize(u14(3));
assertError(deserializer.deserialize(A), error.InvalidEnumTag);
expectError(error.InvalidEnumTag, deserializer.deserialize(A));
out.pos = 0;
try serializer.serialize(u14(3));
try serializer.serialize(u14(88));
assertError(deserializer.deserialize(C), error.InvalidEnumTag);
expectError(error.InvalidEnumTag, deserializer.deserialize(C));
}
test "Deserializer bad data" {

View File

@ -4,6 +4,7 @@
const std = @import("index.zig");
const debug = std.debug;
const testing = std.testing;
const mem = std.mem;
const maxInt = std.math.maxInt;
@ -960,7 +961,7 @@ test "json.token" {
checkNext(&p, Token.Id.ObjectEnd);
checkNext(&p, Token.Id.ObjectEnd);
debug.assert((try p.next()) == null);
testing.expect((try p.next()) == null);
}
// Validate a JSON string. This does not limit number precision so a decoder may not necessarily
@ -981,7 +982,7 @@ pub fn validate(s: []const u8) bool {
}
test "json.validate" {
debug.assert(validate("{}"));
testing.expect(validate("{}"));
}
const Allocator = std.mem.Allocator;
@ -1378,20 +1379,20 @@ test "json.parser.dynamic" {
var image = root.Object.get("Image").?.value;
const width = image.Object.get("Width").?.value;
debug.assert(width.Integer == 800);
testing.expect(width.Integer == 800);
const height = image.Object.get("Height").?.value;
debug.assert(height.Integer == 600);
testing.expect(height.Integer == 600);
const title = image.Object.get("Title").?.value;
debug.assert(mem.eql(u8, title.String, "View from 15th Floor"));
testing.expect(mem.eql(u8, title.String, "View from 15th Floor"));
const animated = image.Object.get("Animated").?.value;
debug.assert(animated.Bool == false);
testing.expect(animated.Bool == false);
const array_of_object = image.Object.get("ArrayOfObject").?.value;
debug.assert(array_of_object.Array.len == 1);
testing.expect(array_of_object.Array.len == 1);
const obj0 = array_of_object.Array.at(0).Object.get("n").?.value;
debug.assert(mem.eql(u8, obj0.String, "m"));
testing.expect(mem.eql(u8, obj0.String, "m"));
}

View File

@ -6,15 +6,15 @@
const std = @import("index.zig");
fn ok(comptime s: []const u8) void {
std.debug.assert(std.json.validate(s));
std.testing.expect(std.json.validate(s));
}
fn err(comptime s: []const u8) void {
std.debug.assert(!std.json.validate(s));
std.testing.expect(!std.json.validate(s));
}
fn any(comptime s: []const u8) void {
std.debug.assert(true);
std.testing.expect(true);
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,6 +1,7 @@
const std = @import("index.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const testing = std.testing;
const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
@ -63,12 +64,12 @@ test "std.lazyInit" {
global_number.resolve();
}
if (global_number.get()) |x| {
assert(x.* == 1234);
testing.expect(x.* == 1234);
} else {
@panic("bad");
}
if (global_number.get()) |x| {
assert(x.* == 1234);
testing.expect(x.* == 1234);
} else {
@panic("bad");
}
@ -80,6 +81,6 @@ test "std.lazyInit(void)" {
if (global_void.get()) |_| @panic("bad") else {
global_void.resolve();
}
assert(global_void.get() != null);
assert(global_void.get() != null);
testing.expect(global_void.get() != null);
testing.expect(global_void.get() != null);
}

View File

@ -1,6 +1,7 @@
const std = @import("index.zig");
const debug = std.debug;
const assert = debug.assert;
const testing = std.testing;
const mem = std.mem;
const Allocator = mem.Allocator;
@ -246,7 +247,7 @@ test "basic linked list test" {
var it = list.first;
var index: u32 = 1;
while (it) |node| : (it = node.next) {
assert(node.data == index);
testing.expect(node.data == index);
index += 1;
}
}
@ -256,7 +257,7 @@ test "basic linked list test" {
var it = list.last;
var index: u32 = 1;
while (it) |node| : (it = node.prev) {
assert(node.data == (6 - index));
testing.expect(node.data == (6 - index));
index += 1;
}
}
@ -265,9 +266,9 @@ test "basic linked list test" {
var last = list.pop(); // {2, 3, 4}
list.remove(three); // {2, 4}
assert(list.first.?.data == 2);
assert(list.last.?.data == 4);
assert(list.len == 2);
testing.expect(list.first.?.data == 2);
testing.expect(list.last.?.data == 4);
testing.expect(list.len == 2);
}
test "linked list concatenation" {
@ -294,18 +295,18 @@ test "linked list concatenation" {
list1.concatByMoving(&list2);
assert(list1.last == five);
assert(list1.len == 5);
assert(list2.first == null);
assert(list2.last == null);
assert(list2.len == 0);
testing.expect(list1.last == five);
testing.expect(list1.len == 5);
testing.expect(list2.first == null);
testing.expect(list2.last == null);
testing.expect(list2.len == 0);
// Traverse forwards.
{
var it = list1.first;
var index: u32 = 1;
while (it) |node| : (it = node.next) {
assert(node.data == index);
testing.expect(node.data == index);
index += 1;
}
}
@ -315,7 +316,7 @@ test "linked list concatenation" {
var it = list1.last;
var index: u32 = 1;
while (it) |node| : (it = node.prev) {
assert(node.data == (6 - index));
testing.expect(node.data == (6 - index));
index += 1;
}
}
@ -328,7 +329,7 @@ test "linked list concatenation" {
var it = list2.first;
var index: u32 = 1;
while (it) |node| : (it = node.next) {
assert(node.data == index);
testing.expect(node.data == index);
index += 1;
}
}
@ -338,7 +339,7 @@ test "linked list concatenation" {
var it = list2.last;
var index: u32 = 1;
while (it) |node| : (it = node.prev) {
assert(node.data == (6 - index));
testing.expect(node.data == (6 - index));
index += 1;
}
}

View File

@ -4,7 +4,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn acos(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -143,38 +143,38 @@ fn acos64(x: f64) f64 {
}
test "math.acos" {
assert(acos(f32(0.0)) == acos32(0.0));
assert(acos(f64(0.0)) == acos64(0.0));
expect(acos(f32(0.0)) == acos32(0.0));
expect(acos(f64(0.0)) == acos64(0.0));
}
test "math.acos32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, acos32(0.0), 1.570796, epsilon));
assert(math.approxEq(f32, acos32(0.2), 1.369438, epsilon));
assert(math.approxEq(f32, acos32(0.3434), 1.220262, epsilon));
assert(math.approxEq(f32, acos32(0.5), 1.047198, epsilon));
assert(math.approxEq(f32, acos32(0.8923), 0.468382, epsilon));
assert(math.approxEq(f32, acos32(-0.2), 1.772154, epsilon));
expect(math.approxEq(f32, acos32(0.0), 1.570796, epsilon));
expect(math.approxEq(f32, acos32(0.2), 1.369438, epsilon));
expect(math.approxEq(f32, acos32(0.3434), 1.220262, epsilon));
expect(math.approxEq(f32, acos32(0.5), 1.047198, epsilon));
expect(math.approxEq(f32, acos32(0.8923), 0.468382, epsilon));
expect(math.approxEq(f32, acos32(-0.2), 1.772154, epsilon));
}
test "math.acos64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, acos64(0.0), 1.570796, epsilon));
assert(math.approxEq(f64, acos64(0.2), 1.369438, epsilon));
assert(math.approxEq(f64, acos64(0.3434), 1.220262, epsilon));
assert(math.approxEq(f64, acos64(0.5), 1.047198, epsilon));
assert(math.approxEq(f64, acos64(0.8923), 0.468382, epsilon));
assert(math.approxEq(f64, acos64(-0.2), 1.772154, epsilon));
expect(math.approxEq(f64, acos64(0.0), 1.570796, epsilon));
expect(math.approxEq(f64, acos64(0.2), 1.369438, epsilon));
expect(math.approxEq(f64, acos64(0.3434), 1.220262, epsilon));
expect(math.approxEq(f64, acos64(0.5), 1.047198, epsilon));
expect(math.approxEq(f64, acos64(0.8923), 0.468382, epsilon));
expect(math.approxEq(f64, acos64(-0.2), 1.772154, epsilon));
}
test "math.acos32.special" {
assert(math.isNan(acos32(-2)));
assert(math.isNan(acos32(1.5)));
expect(math.isNan(acos32(-2)));
expect(math.isNan(acos32(1.5)));
}
test "math.acos64.special" {
assert(math.isNan(acos64(-2)));
assert(math.isNan(acos64(1.5)));
expect(math.isNan(acos64(-2)));
expect(math.isNan(acos64(1.5)));
}

View File

@ -6,7 +6,7 @@
const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn acosh(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -55,34 +55,34 @@ fn acosh64(x: f64) f64 {
}
test "math.acosh" {
assert(acosh(f32(1.5)) == acosh32(1.5));
assert(acosh(f64(1.5)) == acosh64(1.5));
expect(acosh(f32(1.5)) == acosh32(1.5));
expect(acosh(f64(1.5)) == acosh64(1.5));
}
test "math.acosh32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, acosh32(1.5), 0.962424, epsilon));
assert(math.approxEq(f32, acosh32(37.45), 4.315976, epsilon));
assert(math.approxEq(f32, acosh32(89.123), 5.183133, epsilon));
assert(math.approxEq(f32, acosh32(123123.234375), 12.414088, epsilon));
expect(math.approxEq(f32, acosh32(1.5), 0.962424, epsilon));
expect(math.approxEq(f32, acosh32(37.45), 4.315976, epsilon));
expect(math.approxEq(f32, acosh32(89.123), 5.183133, epsilon));
expect(math.approxEq(f32, acosh32(123123.234375), 12.414088, epsilon));
}
test "math.acosh64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, acosh64(1.5), 0.962424, epsilon));
assert(math.approxEq(f64, acosh64(37.45), 4.315976, epsilon));
assert(math.approxEq(f64, acosh64(89.123), 5.183133, epsilon));
assert(math.approxEq(f64, acosh64(123123.234375), 12.414088, epsilon));
expect(math.approxEq(f64, acosh64(1.5), 0.962424, epsilon));
expect(math.approxEq(f64, acosh64(37.45), 4.315976, epsilon));
expect(math.approxEq(f64, acosh64(89.123), 5.183133, epsilon));
expect(math.approxEq(f64, acosh64(123123.234375), 12.414088, epsilon));
}
test "math.acosh32.special" {
assert(math.isNan(acosh32(math.nan(f32))));
assert(math.isSignalNan(acosh32(0.5)));
expect(math.isNan(acosh32(math.nan(f32))));
expect(math.isSignalNan(acosh32(0.5)));
}
test "math.acosh64.special" {
assert(math.isNan(acosh64(math.nan(f64))));
assert(math.isSignalNan(acosh64(0.5)));
expect(math.isNan(acosh64(math.nan(f64))));
expect(math.isSignalNan(acosh64(0.5)));
}

View File

@ -5,7 +5,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn asin(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -136,42 +136,42 @@ fn asin64(x: f64) f64 {
}
test "math.asin" {
assert(asin(f32(0.0)) == asin32(0.0));
assert(asin(f64(0.0)) == asin64(0.0));
expect(asin(f32(0.0)) == asin32(0.0));
expect(asin(f64(0.0)) == asin64(0.0));
}
test "math.asin32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, asin32(0.0), 0.0, epsilon));
assert(math.approxEq(f32, asin32(0.2), 0.201358, epsilon));
assert(math.approxEq(f32, asin32(-0.2), -0.201358, epsilon));
assert(math.approxEq(f32, asin32(0.3434), 0.350535, epsilon));
assert(math.approxEq(f32, asin32(0.5), 0.523599, epsilon));
assert(math.approxEq(f32, asin32(0.8923), 1.102415, epsilon));
expect(math.approxEq(f32, asin32(0.0), 0.0, epsilon));
expect(math.approxEq(f32, asin32(0.2), 0.201358, epsilon));
expect(math.approxEq(f32, asin32(-0.2), -0.201358, epsilon));
expect(math.approxEq(f32, asin32(0.3434), 0.350535, epsilon));
expect(math.approxEq(f32, asin32(0.5), 0.523599, epsilon));
expect(math.approxEq(f32, asin32(0.8923), 1.102415, epsilon));
}
test "math.asin64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, asin64(0.0), 0.0, epsilon));
assert(math.approxEq(f64, asin64(0.2), 0.201358, epsilon));
assert(math.approxEq(f64, asin64(-0.2), -0.201358, epsilon));
assert(math.approxEq(f64, asin64(0.3434), 0.350535, epsilon));
assert(math.approxEq(f64, asin64(0.5), 0.523599, epsilon));
assert(math.approxEq(f64, asin64(0.8923), 1.102415, epsilon));
expect(math.approxEq(f64, asin64(0.0), 0.0, epsilon));
expect(math.approxEq(f64, asin64(0.2), 0.201358, epsilon));
expect(math.approxEq(f64, asin64(-0.2), -0.201358, epsilon));
expect(math.approxEq(f64, asin64(0.3434), 0.350535, epsilon));
expect(math.approxEq(f64, asin64(0.5), 0.523599, epsilon));
expect(math.approxEq(f64, asin64(0.8923), 1.102415, epsilon));
}
test "math.asin32.special" {
assert(asin32(0.0) == 0.0);
assert(asin32(-0.0) == -0.0);
assert(math.isNan(asin32(-2)));
assert(math.isNan(asin32(1.5)));
expect(asin32(0.0) == 0.0);
expect(asin32(-0.0) == -0.0);
expect(math.isNan(asin32(-2)));
expect(math.isNan(asin32(1.5)));
}
test "math.asin64.special" {
assert(asin64(0.0) == 0.0);
assert(asin64(-0.0) == -0.0);
assert(math.isNan(asin64(-2)));
assert(math.isNan(asin64(1.5)));
expect(asin64(0.0) == 0.0);
expect(asin64(-0.0) == -0.0);
expect(math.isNan(asin64(-2)));
expect(math.isNan(asin64(1.5)));
}

View File

@ -6,7 +6,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn asinh(x: var) @typeOf(x) {
@ -83,46 +83,46 @@ fn asinh64(x: f64) f64 {
}
test "math.asinh" {
assert(asinh(f32(0.0)) == asinh32(0.0));
assert(asinh(f64(0.0)) == asinh64(0.0));
expect(asinh(f32(0.0)) == asinh32(0.0));
expect(asinh(f64(0.0)) == asinh64(0.0));
}
test "math.asinh32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, asinh32(0.0), 0.0, epsilon));
assert(math.approxEq(f32, asinh32(0.2), 0.198690, epsilon));
assert(math.approxEq(f32, asinh32(0.8923), 0.803133, epsilon));
assert(math.approxEq(f32, asinh32(1.5), 1.194763, epsilon));
assert(math.approxEq(f32, asinh32(37.45), 4.316332, epsilon));
assert(math.approxEq(f32, asinh32(89.123), 5.183196, epsilon));
assert(math.approxEq(f32, asinh32(123123.234375), 12.414088, epsilon));
expect(math.approxEq(f32, asinh32(0.0), 0.0, epsilon));
expect(math.approxEq(f32, asinh32(0.2), 0.198690, epsilon));
expect(math.approxEq(f32, asinh32(0.8923), 0.803133, epsilon));
expect(math.approxEq(f32, asinh32(1.5), 1.194763, epsilon));
expect(math.approxEq(f32, asinh32(37.45), 4.316332, epsilon));
expect(math.approxEq(f32, asinh32(89.123), 5.183196, epsilon));
expect(math.approxEq(f32, asinh32(123123.234375), 12.414088, epsilon));
}
test "math.asinh64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, asinh64(0.0), 0.0, epsilon));
assert(math.approxEq(f64, asinh64(0.2), 0.198690, epsilon));
assert(math.approxEq(f64, asinh64(0.8923), 0.803133, epsilon));
assert(math.approxEq(f64, asinh64(1.5), 1.194763, epsilon));
assert(math.approxEq(f64, asinh64(37.45), 4.316332, epsilon));
assert(math.approxEq(f64, asinh64(89.123), 5.183196, epsilon));
assert(math.approxEq(f64, asinh64(123123.234375), 12.414088, epsilon));
expect(math.approxEq(f64, asinh64(0.0), 0.0, epsilon));
expect(math.approxEq(f64, asinh64(0.2), 0.198690, epsilon));
expect(math.approxEq(f64, asinh64(0.8923), 0.803133, epsilon));
expect(math.approxEq(f64, asinh64(1.5), 1.194763, epsilon));
expect(math.approxEq(f64, asinh64(37.45), 4.316332, epsilon));
expect(math.approxEq(f64, asinh64(89.123), 5.183196, epsilon));
expect(math.approxEq(f64, asinh64(123123.234375), 12.414088, epsilon));
}
test "math.asinh32.special" {
assert(asinh32(0.0) == 0.0);
assert(asinh32(-0.0) == -0.0);
assert(math.isPositiveInf(asinh32(math.inf(f32))));
assert(math.isNegativeInf(asinh32(-math.inf(f32))));
assert(math.isNan(asinh32(math.nan(f32))));
expect(asinh32(0.0) == 0.0);
expect(asinh32(-0.0) == -0.0);
expect(math.isPositiveInf(asinh32(math.inf(f32))));
expect(math.isNegativeInf(asinh32(-math.inf(f32))));
expect(math.isNan(asinh32(math.nan(f32))));
}
test "math.asinh64.special" {
assert(asinh64(0.0) == 0.0);
assert(asinh64(-0.0) == -0.0);
assert(math.isPositiveInf(asinh64(math.inf(f64))));
assert(math.isNegativeInf(asinh64(-math.inf(f64))));
assert(math.isNan(asinh64(math.nan(f64))));
expect(asinh64(0.0) == 0.0);
expect(asinh64(-0.0) == -0.0);
expect(math.isPositiveInf(asinh64(math.inf(f64))));
expect(math.isNegativeInf(asinh64(-math.inf(f64))));
expect(math.isNan(asinh64(math.nan(f64))));
}

View File

@ -5,7 +5,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn atan(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -206,44 +206,44 @@ fn atan64(x_: f64) f64 {
}
test "math.atan" {
assert(@bitCast(u32, atan(f32(0.2))) == @bitCast(u32, atan32(0.2)));
assert(atan(f64(0.2)) == atan64(0.2));
expect(@bitCast(u32, atan(f32(0.2))) == @bitCast(u32, atan32(0.2)));
expect(atan(f64(0.2)) == atan64(0.2));
}
test "math.atan32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, atan32(0.2), 0.197396, epsilon));
assert(math.approxEq(f32, atan32(-0.2), -0.197396, epsilon));
assert(math.approxEq(f32, atan32(0.3434), 0.330783, epsilon));
assert(math.approxEq(f32, atan32(0.8923), 0.728545, epsilon));
assert(math.approxEq(f32, atan32(1.5), 0.982794, epsilon));
expect(math.approxEq(f32, atan32(0.2), 0.197396, epsilon));
expect(math.approxEq(f32, atan32(-0.2), -0.197396, epsilon));
expect(math.approxEq(f32, atan32(0.3434), 0.330783, epsilon));
expect(math.approxEq(f32, atan32(0.8923), 0.728545, epsilon));
expect(math.approxEq(f32, atan32(1.5), 0.982794, epsilon));
}
test "math.atan64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, atan64(0.2), 0.197396, epsilon));
assert(math.approxEq(f64, atan64(-0.2), -0.197396, epsilon));
assert(math.approxEq(f64, atan64(0.3434), 0.330783, epsilon));
assert(math.approxEq(f64, atan64(0.8923), 0.728545, epsilon));
assert(math.approxEq(f64, atan64(1.5), 0.982794, epsilon));
expect(math.approxEq(f64, atan64(0.2), 0.197396, epsilon));
expect(math.approxEq(f64, atan64(-0.2), -0.197396, epsilon));
expect(math.approxEq(f64, atan64(0.3434), 0.330783, epsilon));
expect(math.approxEq(f64, atan64(0.8923), 0.728545, epsilon));
expect(math.approxEq(f64, atan64(1.5), 0.982794, epsilon));
}
test "math.atan32.special" {
const epsilon = 0.000001;
assert(atan32(0.0) == 0.0);
assert(atan32(-0.0) == -0.0);
assert(math.approxEq(f32, atan32(math.inf(f32)), math.pi / 2.0, epsilon));
assert(math.approxEq(f32, atan32(-math.inf(f32)), -math.pi / 2.0, epsilon));
expect(atan32(0.0) == 0.0);
expect(atan32(-0.0) == -0.0);
expect(math.approxEq(f32, atan32(math.inf(f32)), math.pi / 2.0, epsilon));
expect(math.approxEq(f32, atan32(-math.inf(f32)), -math.pi / 2.0, epsilon));
}
test "math.atan64.special" {
const epsilon = 0.000001;
assert(atan64(0.0) == 0.0);
assert(atan64(-0.0) == -0.0);
assert(math.approxEq(f64, atan64(math.inf(f64)), math.pi / 2.0, epsilon));
assert(math.approxEq(f64, atan64(-math.inf(f64)), -math.pi / 2.0, epsilon));
expect(atan64(0.0) == 0.0);
expect(atan64(-0.0) == -0.0);
expect(math.approxEq(f64, atan64(math.inf(f64)), math.pi / 2.0, epsilon));
expect(math.approxEq(f64, atan64(-math.inf(f64)), -math.pi / 2.0, epsilon));
}

View File

@ -20,7 +20,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn atan2(comptime T: type, y: T, x: T) T {
return switch (T) {
@ -206,78 +206,78 @@ fn atan2_64(y: f64, x: f64) f64 {
}
test "math.atan2" {
assert(atan2(f32, 0.2, 0.21) == atan2_32(0.2, 0.21));
assert(atan2(f64, 0.2, 0.21) == atan2_64(0.2, 0.21));
expect(atan2(f32, 0.2, 0.21) == atan2_32(0.2, 0.21));
expect(atan2(f64, 0.2, 0.21) == atan2_64(0.2, 0.21));
}
test "math.atan2_32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, atan2_32(0.0, 0.0), 0.0, epsilon));
assert(math.approxEq(f32, atan2_32(0.2, 0.2), 0.785398, epsilon));
assert(math.approxEq(f32, atan2_32(-0.2, 0.2), -0.785398, epsilon));
assert(math.approxEq(f32, atan2_32(0.2, -0.2), 2.356194, epsilon));
assert(math.approxEq(f32, atan2_32(-0.2, -0.2), -2.356194, epsilon));
assert(math.approxEq(f32, atan2_32(0.34, -0.4), 2.437099, epsilon));
assert(math.approxEq(f32, atan2_32(0.34, 1.243), 0.267001, epsilon));
expect(math.approxEq(f32, atan2_32(0.0, 0.0), 0.0, epsilon));
expect(math.approxEq(f32, atan2_32(0.2, 0.2), 0.785398, epsilon));
expect(math.approxEq(f32, atan2_32(-0.2, 0.2), -0.785398, epsilon));
expect(math.approxEq(f32, atan2_32(0.2, -0.2), 2.356194, epsilon));
expect(math.approxEq(f32, atan2_32(-0.2, -0.2), -2.356194, epsilon));
expect(math.approxEq(f32, atan2_32(0.34, -0.4), 2.437099, epsilon));
expect(math.approxEq(f32, atan2_32(0.34, 1.243), 0.267001, epsilon));
}
test "math.atan2_64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, atan2_64(0.0, 0.0), 0.0, epsilon));
assert(math.approxEq(f64, atan2_64(0.2, 0.2), 0.785398, epsilon));
assert(math.approxEq(f64, atan2_64(-0.2, 0.2), -0.785398, epsilon));
assert(math.approxEq(f64, atan2_64(0.2, -0.2), 2.356194, epsilon));
assert(math.approxEq(f64, atan2_64(-0.2, -0.2), -2.356194, epsilon));
assert(math.approxEq(f64, atan2_64(0.34, -0.4), 2.437099, epsilon));
assert(math.approxEq(f64, atan2_64(0.34, 1.243), 0.267001, epsilon));
expect(math.approxEq(f64, atan2_64(0.0, 0.0), 0.0, epsilon));
expect(math.approxEq(f64, atan2_64(0.2, 0.2), 0.785398, epsilon));
expect(math.approxEq(f64, atan2_64(-0.2, 0.2), -0.785398, epsilon));
expect(math.approxEq(f64, atan2_64(0.2, -0.2), 2.356194, epsilon));
expect(math.approxEq(f64, atan2_64(-0.2, -0.2), -2.356194, epsilon));
expect(math.approxEq(f64, atan2_64(0.34, -0.4), 2.437099, epsilon));
expect(math.approxEq(f64, atan2_64(0.34, 1.243), 0.267001, epsilon));
}
test "math.atan2_32.special" {
const epsilon = 0.000001;
assert(math.isNan(atan2_32(1.0, math.nan(f32))));
assert(math.isNan(atan2_32(math.nan(f32), 1.0)));
assert(atan2_32(0.0, 5.0) == 0.0);
assert(atan2_32(-0.0, 5.0) == -0.0);
assert(math.approxEq(f32, atan2_32(0.0, -5.0), math.pi, epsilon));
//assert(math.approxEq(f32, atan2_32(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero?
assert(math.approxEq(f32, atan2_32(1.0, 0.0), math.pi / 2.0, epsilon));
assert(math.approxEq(f32, atan2_32(1.0, -0.0), math.pi / 2.0, epsilon));
assert(math.approxEq(f32, atan2_32(-1.0, 0.0), -math.pi / 2.0, epsilon));
assert(math.approxEq(f32, atan2_32(-1.0, -0.0), -math.pi / 2.0, epsilon));
assert(math.approxEq(f32, atan2_32(math.inf(f32), math.inf(f32)), math.pi / 4.0, epsilon));
assert(math.approxEq(f32, atan2_32(-math.inf(f32), math.inf(f32)), -math.pi / 4.0, epsilon));
assert(math.approxEq(f32, atan2_32(math.inf(f32), -math.inf(f32)), 3.0 * math.pi / 4.0, epsilon));
assert(math.approxEq(f32, atan2_32(-math.inf(f32), -math.inf(f32)), -3.0 * math.pi / 4.0, epsilon));
assert(atan2_32(1.0, math.inf(f32)) == 0.0);
assert(math.approxEq(f32, atan2_32(1.0, -math.inf(f32)), math.pi, epsilon));
assert(math.approxEq(f32, atan2_32(-1.0, -math.inf(f32)), -math.pi, epsilon));
assert(math.approxEq(f32, atan2_32(math.inf(f32), 1.0), math.pi / 2.0, epsilon));
assert(math.approxEq(f32, atan2_32(-math.inf(f32), 1.0), -math.pi / 2.0, epsilon));
expect(math.isNan(atan2_32(1.0, math.nan(f32))));
expect(math.isNan(atan2_32(math.nan(f32), 1.0)));
expect(atan2_32(0.0, 5.0) == 0.0);
expect(atan2_32(-0.0, 5.0) == -0.0);
expect(math.approxEq(f32, atan2_32(0.0, -5.0), math.pi, epsilon));
//expect(math.approxEq(f32, atan2_32(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero?
expect(math.approxEq(f32, atan2_32(1.0, 0.0), math.pi / 2.0, epsilon));
expect(math.approxEq(f32, atan2_32(1.0, -0.0), math.pi / 2.0, epsilon));
expect(math.approxEq(f32, atan2_32(-1.0, 0.0), -math.pi / 2.0, epsilon));
expect(math.approxEq(f32, atan2_32(-1.0, -0.0), -math.pi / 2.0, epsilon));
expect(math.approxEq(f32, atan2_32(math.inf(f32), math.inf(f32)), math.pi / 4.0, epsilon));
expect(math.approxEq(f32, atan2_32(-math.inf(f32), math.inf(f32)), -math.pi / 4.0, epsilon));
expect(math.approxEq(f32, atan2_32(math.inf(f32), -math.inf(f32)), 3.0 * math.pi / 4.0, epsilon));
expect(math.approxEq(f32, atan2_32(-math.inf(f32), -math.inf(f32)), -3.0 * math.pi / 4.0, epsilon));
expect(atan2_32(1.0, math.inf(f32)) == 0.0);
expect(math.approxEq(f32, atan2_32(1.0, -math.inf(f32)), math.pi, epsilon));
expect(math.approxEq(f32, atan2_32(-1.0, -math.inf(f32)), -math.pi, epsilon));
expect(math.approxEq(f32, atan2_32(math.inf(f32), 1.0), math.pi / 2.0, epsilon));
expect(math.approxEq(f32, atan2_32(-math.inf(f32), 1.0), -math.pi / 2.0, epsilon));
}
test "math.atan2_64.special" {
const epsilon = 0.000001;
assert(math.isNan(atan2_64(1.0, math.nan(f64))));
assert(math.isNan(atan2_64(math.nan(f64), 1.0)));
assert(atan2_64(0.0, 5.0) == 0.0);
assert(atan2_64(-0.0, 5.0) == -0.0);
assert(math.approxEq(f64, atan2_64(0.0, -5.0), math.pi, epsilon));
//assert(math.approxEq(f64, atan2_64(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero?
assert(math.approxEq(f64, atan2_64(1.0, 0.0), math.pi / 2.0, epsilon));
assert(math.approxEq(f64, atan2_64(1.0, -0.0), math.pi / 2.0, epsilon));
assert(math.approxEq(f64, atan2_64(-1.0, 0.0), -math.pi / 2.0, epsilon));
assert(math.approxEq(f64, atan2_64(-1.0, -0.0), -math.pi / 2.0, epsilon));
assert(math.approxEq(f64, atan2_64(math.inf(f64), math.inf(f64)), math.pi / 4.0, epsilon));
assert(math.approxEq(f64, atan2_64(-math.inf(f64), math.inf(f64)), -math.pi / 4.0, epsilon));
assert(math.approxEq(f64, atan2_64(math.inf(f64), -math.inf(f64)), 3.0 * math.pi / 4.0, epsilon));
assert(math.approxEq(f64, atan2_64(-math.inf(f64), -math.inf(f64)), -3.0 * math.pi / 4.0, epsilon));
assert(atan2_64(1.0, math.inf(f64)) == 0.0);
assert(math.approxEq(f64, atan2_64(1.0, -math.inf(f64)), math.pi, epsilon));
assert(math.approxEq(f64, atan2_64(-1.0, -math.inf(f64)), -math.pi, epsilon));
assert(math.approxEq(f64, atan2_64(math.inf(f64), 1.0), math.pi / 2.0, epsilon));
assert(math.approxEq(f64, atan2_64(-math.inf(f64), 1.0), -math.pi / 2.0, epsilon));
expect(math.isNan(atan2_64(1.0, math.nan(f64))));
expect(math.isNan(atan2_64(math.nan(f64), 1.0)));
expect(atan2_64(0.0, 5.0) == 0.0);
expect(atan2_64(-0.0, 5.0) == -0.0);
expect(math.approxEq(f64, atan2_64(0.0, -5.0), math.pi, epsilon));
//expect(math.approxEq(f64, atan2_64(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero?
expect(math.approxEq(f64, atan2_64(1.0, 0.0), math.pi / 2.0, epsilon));
expect(math.approxEq(f64, atan2_64(1.0, -0.0), math.pi / 2.0, epsilon));
expect(math.approxEq(f64, atan2_64(-1.0, 0.0), -math.pi / 2.0, epsilon));
expect(math.approxEq(f64, atan2_64(-1.0, -0.0), -math.pi / 2.0, epsilon));
expect(math.approxEq(f64, atan2_64(math.inf(f64), math.inf(f64)), math.pi / 4.0, epsilon));
expect(math.approxEq(f64, atan2_64(-math.inf(f64), math.inf(f64)), -math.pi / 4.0, epsilon));
expect(math.approxEq(f64, atan2_64(math.inf(f64), -math.inf(f64)), 3.0 * math.pi / 4.0, epsilon));
expect(math.approxEq(f64, atan2_64(-math.inf(f64), -math.inf(f64)), -3.0 * math.pi / 4.0, epsilon));
expect(atan2_64(1.0, math.inf(f64)) == 0.0);
expect(math.approxEq(f64, atan2_64(1.0, -math.inf(f64)), math.pi, epsilon));
expect(math.approxEq(f64, atan2_64(-1.0, -math.inf(f64)), -math.pi, epsilon));
expect(math.approxEq(f64, atan2_64(math.inf(f64), 1.0), math.pi / 2.0, epsilon));
expect(math.approxEq(f64, atan2_64(-math.inf(f64), 1.0), -math.pi / 2.0, epsilon));
}

View File

@ -6,7 +6,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn atanh(x: var) @typeOf(x) {
@ -78,38 +78,38 @@ fn atanh_64(x: f64) f64 {
}
test "math.atanh" {
assert(atanh(f32(0.0)) == atanh_32(0.0));
assert(atanh(f64(0.0)) == atanh_64(0.0));
expect(atanh(f32(0.0)) == atanh_32(0.0));
expect(atanh(f64(0.0)) == atanh_64(0.0));
}
test "math.atanh_32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, atanh_32(0.0), 0.0, epsilon));
assert(math.approxEq(f32, atanh_32(0.2), 0.202733, epsilon));
assert(math.approxEq(f32, atanh_32(0.8923), 1.433099, epsilon));
expect(math.approxEq(f32, atanh_32(0.0), 0.0, epsilon));
expect(math.approxEq(f32, atanh_32(0.2), 0.202733, epsilon));
expect(math.approxEq(f32, atanh_32(0.8923), 1.433099, epsilon));
}
test "math.atanh_64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, atanh_64(0.0), 0.0, epsilon));
assert(math.approxEq(f64, atanh_64(0.2), 0.202733, epsilon));
assert(math.approxEq(f64, atanh_64(0.8923), 1.433099, epsilon));
expect(math.approxEq(f64, atanh_64(0.0), 0.0, epsilon));
expect(math.approxEq(f64, atanh_64(0.2), 0.202733, epsilon));
expect(math.approxEq(f64, atanh_64(0.8923), 1.433099, epsilon));
}
test "math.atanh32.special" {
assert(math.isPositiveInf(atanh_32(1)));
assert(math.isNegativeInf(atanh_32(-1)));
assert(math.isSignalNan(atanh_32(1.5)));
assert(math.isSignalNan(atanh_32(-1.5)));
assert(math.isNan(atanh_32(math.nan(f32))));
expect(math.isPositiveInf(atanh_32(1)));
expect(math.isNegativeInf(atanh_32(-1)));
expect(math.isSignalNan(atanh_32(1.5)));
expect(math.isSignalNan(atanh_32(-1.5)));
expect(math.isNan(atanh_32(math.nan(f32))));
}
test "math.atanh64.special" {
assert(math.isPositiveInf(atanh_64(1)));
assert(math.isNegativeInf(atanh_64(-1)));
assert(math.isSignalNan(atanh_64(1.5)));
assert(math.isSignalNan(atanh_64(-1.5)));
assert(math.isNan(atanh_64(math.nan(f64))));
expect(math.isPositiveInf(atanh_64(1)));
expect(math.isNegativeInf(atanh_64(-1)));
expect(math.isSignalNan(atanh_64(1.5)));
expect(math.isSignalNan(atanh_64(-1.5)));
expect(math.isNan(atanh_64(math.nan(f64))));
}

View File

@ -1,6 +1,7 @@
const std = @import("../../index.zig");
const builtin = @import("builtin");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const mem = std.mem;
const Allocator = mem.Allocator;
@ -1086,44 +1087,40 @@ test "big.int comptime_int set" {
const result = Limb(s & maxInt(Limb));
s >>= Limb.bit_count / 2;
s >>= Limb.bit_count / 2;
debug.assert(a.limbs[i] == result);
testing.expect(a.limbs[i] == result);
}
}
test "big.int comptime_int set negative" {
var a = try Int.initSet(al, -10);
debug.assert(a.limbs[0] == 10);
debug.assert(a.positive == false);
testing.expect(a.limbs[0] == 10);
testing.expect(a.positive == false);
}
test "big.int int set unaligned small" {
var a = try Int.initSet(al, u7(45));
debug.assert(a.limbs[0] == 45);
debug.assert(a.positive == true);
testing.expect(a.limbs[0] == 45);
testing.expect(a.positive == true);
}
test "big.int comptime_int to" {
const a = try Int.initSet(al, 0xefffffff00000001eeeeeeefaaaaaaab);
debug.assert((try a.to(u128)) == 0xefffffff00000001eeeeeeefaaaaaaab);
testing.expect((try a.to(u128)) == 0xefffffff00000001eeeeeeefaaaaaaab);
}
test "big.int sub-limb to" {
const a = try Int.initSet(al, 10);
debug.assert((try a.to(u8)) == 10);
testing.expect((try a.to(u8)) == 10);
}
test "big.int to target too small error" {
const a = try Int.initSet(al, 0xffffffff);
if (a.to(u8)) |_| {
unreachable;
} else |err| {
debug.assert(err == error.TargetTooSmall);
}
testing.expectError(error.TargetTooSmall, a.to(u8));
}
test "big.int norm1" {
@ -1135,22 +1132,22 @@ test "big.int norm1" {
a.limbs[2] = 3;
a.limbs[3] = 0;
a.norm1(4);
debug.assert(a.len == 3);
testing.expect(a.len == 3);
a.limbs[0] = 1;
a.limbs[1] = 2;
a.limbs[2] = 3;
a.norm1(3);
debug.assert(a.len == 3);
testing.expect(a.len == 3);
a.limbs[0] = 0;
a.limbs[1] = 0;
a.norm1(2);
debug.assert(a.len == 1);
testing.expect(a.len == 1);
a.limbs[0] = 0;
a.norm1(1);
debug.assert(a.len == 1);
testing.expect(a.len == 1);
}
test "big.int normN" {
@ -1162,144 +1159,144 @@ test "big.int normN" {
a.limbs[2] = 0;
a.limbs[3] = 0;
a.normN(4);
debug.assert(a.len == 2);
testing.expect(a.len == 2);
a.limbs[0] = 1;
a.limbs[1] = 2;
a.limbs[2] = 3;
a.normN(3);
debug.assert(a.len == 3);
testing.expect(a.len == 3);
a.limbs[0] = 0;
a.limbs[1] = 0;
a.limbs[2] = 0;
a.limbs[3] = 0;
a.normN(4);
debug.assert(a.len == 1);
testing.expect(a.len == 1);
a.limbs[0] = 0;
a.normN(1);
debug.assert(a.len == 1);
testing.expect(a.len == 1);
}
test "big.int parity" {
var a = try Int.init(al);
try a.set(0);
debug.assert(a.isEven());
debug.assert(!a.isOdd());
testing.expect(a.isEven());
testing.expect(!a.isOdd());
try a.set(7);
debug.assert(!a.isEven());
debug.assert(a.isOdd());
testing.expect(!a.isEven());
testing.expect(a.isOdd());
}
test "big.int bitcount + sizeInBase" {
var a = try Int.init(al);
try a.set(0b100);
debug.assert(a.bitCountAbs() == 3);
debug.assert(a.sizeInBase(2) >= 3);
debug.assert(a.sizeInBase(10) >= 1);
testing.expect(a.bitCountAbs() == 3);
testing.expect(a.sizeInBase(2) >= 3);
testing.expect(a.sizeInBase(10) >= 1);
a.negate();
debug.assert(a.bitCountAbs() == 3);
debug.assert(a.sizeInBase(2) >= 4);
debug.assert(a.sizeInBase(10) >= 2);
testing.expect(a.bitCountAbs() == 3);
testing.expect(a.sizeInBase(2) >= 4);
testing.expect(a.sizeInBase(10) >= 2);
try a.set(0xffffffff);
debug.assert(a.bitCountAbs() == 32);
debug.assert(a.sizeInBase(2) >= 32);
debug.assert(a.sizeInBase(10) >= 10);
testing.expect(a.bitCountAbs() == 32);
testing.expect(a.sizeInBase(2) >= 32);
testing.expect(a.sizeInBase(10) >= 10);
try a.shiftLeft(a, 5000);
debug.assert(a.bitCountAbs() == 5032);
debug.assert(a.sizeInBase(2) >= 5032);
testing.expect(a.bitCountAbs() == 5032);
testing.expect(a.sizeInBase(2) >= 5032);
a.positive = false;
debug.assert(a.bitCountAbs() == 5032);
debug.assert(a.sizeInBase(2) >= 5033);
testing.expect(a.bitCountAbs() == 5032);
testing.expect(a.sizeInBase(2) >= 5033);
}
test "big.int bitcount/to" {
var a = try Int.init(al);
try a.set(0);
debug.assert(a.bitCountTwosComp() == 0);
testing.expect(a.bitCountTwosComp() == 0);
// TODO: stack smashing
// debug.assert((try a.to(u0)) == 0);
// testing.expect((try a.to(u0)) == 0);
// TODO: sigsegv
// debug.assert((try a.to(i0)) == 0);
// testing.expect((try a.to(i0)) == 0);
try a.set(-1);
debug.assert(a.bitCountTwosComp() == 1);
debug.assert((try a.to(i1)) == -1);
testing.expect(a.bitCountTwosComp() == 1);
testing.expect((try a.to(i1)) == -1);
try a.set(-8);
debug.assert(a.bitCountTwosComp() == 4);
debug.assert((try a.to(i4)) == -8);
testing.expect(a.bitCountTwosComp() == 4);
testing.expect((try a.to(i4)) == -8);
try a.set(127);
debug.assert(a.bitCountTwosComp() == 7);
debug.assert((try a.to(u7)) == 127);
testing.expect(a.bitCountTwosComp() == 7);
testing.expect((try a.to(u7)) == 127);
try a.set(-128);
debug.assert(a.bitCountTwosComp() == 8);
debug.assert((try a.to(i8)) == -128);
testing.expect(a.bitCountTwosComp() == 8);
testing.expect((try a.to(i8)) == -128);
try a.set(-129);
debug.assert(a.bitCountTwosComp() == 9);
debug.assert((try a.to(i9)) == -129);
testing.expect(a.bitCountTwosComp() == 9);
testing.expect((try a.to(i9)) == -129);
}
test "big.int fits" {
var a = try Int.init(al);
try a.set(0);
debug.assert(a.fits(u0));
debug.assert(a.fits(i0));
testing.expect(a.fits(u0));
testing.expect(a.fits(i0));
try a.set(255);
debug.assert(!a.fits(u0));
debug.assert(!a.fits(u1));
debug.assert(!a.fits(i8));
debug.assert(a.fits(u8));
debug.assert(a.fits(u9));
debug.assert(a.fits(i9));
testing.expect(!a.fits(u0));
testing.expect(!a.fits(u1));
testing.expect(!a.fits(i8));
testing.expect(a.fits(u8));
testing.expect(a.fits(u9));
testing.expect(a.fits(i9));
try a.set(-128);
debug.assert(!a.fits(i7));
debug.assert(a.fits(i8));
debug.assert(a.fits(i9));
debug.assert(!a.fits(u9));
testing.expect(!a.fits(i7));
testing.expect(a.fits(i8));
testing.expect(a.fits(i9));
testing.expect(!a.fits(u9));
try a.set(0x1ffffffffeeeeeeee);
debug.assert(!a.fits(u32));
debug.assert(!a.fits(u64));
debug.assert(a.fits(u65));
testing.expect(!a.fits(u32));
testing.expect(!a.fits(u64));
testing.expect(a.fits(u65));
}
test "big.int string set" {
var a = try Int.init(al);
try a.setString(10, "120317241209124781241290847124");
debug.assert((try a.to(u128)) == 120317241209124781241290847124);
testing.expect((try a.to(u128)) == 120317241209124781241290847124);
}
test "big.int string negative" {
var a = try Int.init(al);
try a.setString(10, "-1023");
debug.assert((try a.to(i32)) == -1023);
testing.expect((try a.to(i32)) == -1023);
}
test "big.int string set bad char error" {
var a = try Int.init(al);
a.setString(10, "x") catch |err| debug.assert(err == error.InvalidCharForDigit);
testing.expectError(error.InvalidCharForDigit, a.setString(10, "x"));
}
test "big.int string set bad base error" {
var a = try Int.init(al);
a.setString(45, "10") catch |err| debug.assert(err == error.InvalidBase);
testing.expectError(error.InvalidBase, a.setString(45, "10"));
}
test "big.int string to" {
@ -1308,17 +1305,13 @@ test "big.int string to" {
const as = try a.toString(al, 10);
const es = "120317241209124781241290847124";
debug.assert(mem.eql(u8, as, es));
testing.expect(mem.eql(u8, as, es));
}
test "big.int string to base base error" {
const a = try Int.initSet(al, 0xffffffff);
if (a.toString(al, 45)) |_| {
unreachable;
} else |err| {
debug.assert(err == error.InvalidBase);
}
testing.expectError(error.InvalidBase, a.toString(al, 45));
}
test "big.int string to base 2" {
@ -1327,7 +1320,7 @@ test "big.int string to base 2" {
const as = try a.toString(al, 2);
const es = "-1011";
debug.assert(mem.eql(u8, as, es));
testing.expect(mem.eql(u8, as, es));
}
test "big.int string to base 16" {
@ -1336,7 +1329,7 @@ test "big.int string to base 16" {
const as = try a.toString(al, 16);
const es = "efffffff00000001eeeeeeefaaaaaaab";
debug.assert(mem.eql(u8, as, es));
testing.expect(mem.eql(u8, as, es));
}
test "big.int neg string to" {
@ -1345,7 +1338,7 @@ test "big.int neg string to" {
const as = try a.toString(al, 10);
const es = "-123907434";
debug.assert(mem.eql(u8, as, es));
testing.expect(mem.eql(u8, as, es));
}
test "big.int zero string to" {
@ -1354,98 +1347,98 @@ test "big.int zero string to" {
const as = try a.toString(al, 10);
const es = "0";
debug.assert(mem.eql(u8, as, es));
testing.expect(mem.eql(u8, as, es));
}
test "big.int clone" {
var a = try Int.initSet(al, 1234);
const b = try a.clone();
debug.assert((try a.to(u32)) == 1234);
debug.assert((try b.to(u32)) == 1234);
testing.expect((try a.to(u32)) == 1234);
testing.expect((try b.to(u32)) == 1234);
try a.set(77);
debug.assert((try a.to(u32)) == 77);
debug.assert((try b.to(u32)) == 1234);
testing.expect((try a.to(u32)) == 77);
testing.expect((try b.to(u32)) == 1234);
}
test "big.int swap" {
var a = try Int.initSet(al, 1234);
var b = try Int.initSet(al, 5678);
debug.assert((try a.to(u32)) == 1234);
debug.assert((try b.to(u32)) == 5678);
testing.expect((try a.to(u32)) == 1234);
testing.expect((try b.to(u32)) == 5678);
a.swap(&b);
debug.assert((try a.to(u32)) == 5678);
debug.assert((try b.to(u32)) == 1234);
testing.expect((try a.to(u32)) == 5678);
testing.expect((try b.to(u32)) == 1234);
}
test "big.int to negative" {
var a = try Int.initSet(al, -10);
debug.assert((try a.to(i32)) == -10);
testing.expect((try a.to(i32)) == -10);
}
test "big.int compare" {
var a = try Int.initSet(al, -11);
var b = try Int.initSet(al, 10);
debug.assert(a.cmpAbs(b) == 1);
debug.assert(a.cmp(b) == -1);
testing.expect(a.cmpAbs(b) == 1);
testing.expect(a.cmp(b) == -1);
}
test "big.int compare similar" {
var a = try Int.initSet(al, 0xffffffffeeeeeeeeffffffffeeeeeeee);
var b = try Int.initSet(al, 0xffffffffeeeeeeeeffffffffeeeeeeef);
debug.assert(a.cmpAbs(b) == -1);
debug.assert(b.cmpAbs(a) == 1);
testing.expect(a.cmpAbs(b) == -1);
testing.expect(b.cmpAbs(a) == 1);
}
test "big.int compare different limb size" {
var a = try Int.initSet(al, maxInt(Limb) + 1);
var b = try Int.initSet(al, 1);
debug.assert(a.cmpAbs(b) == 1);
debug.assert(b.cmpAbs(a) == -1);
testing.expect(a.cmpAbs(b) == 1);
testing.expect(b.cmpAbs(a) == -1);
}
test "big.int compare multi-limb" {
var a = try Int.initSet(al, -0x7777777799999999ffffeeeeffffeeeeffffeeeef);
var b = try Int.initSet(al, 0x7777777799999999ffffeeeeffffeeeeffffeeeee);
debug.assert(a.cmpAbs(b) == 1);
debug.assert(a.cmp(b) == -1);
testing.expect(a.cmpAbs(b) == 1);
testing.expect(a.cmp(b) == -1);
}
test "big.int equality" {
var a = try Int.initSet(al, 0xffffffff1);
var b = try Int.initSet(al, -0xffffffff1);
debug.assert(a.eqAbs(b));
debug.assert(!a.eq(b));
testing.expect(a.eqAbs(b));
testing.expect(!a.eq(b));
}
test "big.int abs" {
var a = try Int.initSet(al, -5);
a.abs();
debug.assert((try a.to(u32)) == 5);
testing.expect((try a.to(u32)) == 5);
a.abs();
debug.assert((try a.to(u32)) == 5);
testing.expect((try a.to(u32)) == 5);
}
test "big.int negate" {
var a = try Int.initSet(al, 5);
a.negate();
debug.assert((try a.to(i32)) == -5);
testing.expect((try a.to(i32)) == -5);
a.negate();
debug.assert((try a.to(i32)) == 5);
testing.expect((try a.to(i32)) == 5);
}
test "big.int add single-single" {
@ -1455,7 +1448,7 @@ test "big.int add single-single" {
var c = try Int.init(al);
try c.add(a, b);
debug.assert((try c.to(u32)) == 55);
testing.expect((try c.to(u32)) == 55);
}
test "big.int add multi-single" {
@ -1465,10 +1458,10 @@ test "big.int add multi-single" {
var c = try Int.init(al);
try c.add(a, b);
debug.assert((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
try c.add(b, a);
debug.assert((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2);
}
test "big.int add multi-multi" {
@ -1480,7 +1473,7 @@ test "big.int add multi-multi" {
var c = try Int.init(al);
try c.add(a, b);
debug.assert((try c.to(u128)) == op1 + op2);
testing.expect((try c.to(u128)) == op1 + op2);
}
test "big.int add zero-zero" {
@ -1490,7 +1483,7 @@ test "big.int add zero-zero" {
var c = try Int.init(al);
try c.add(a, b);
debug.assert((try c.to(u32)) == 0);
testing.expect((try c.to(u32)) == 0);
}
test "big.int add alias multi-limb nonzero-zero" {
@ -1500,7 +1493,7 @@ test "big.int add alias multi-limb nonzero-zero" {
try a.add(a, b);
debug.assert((try a.to(u128)) == op1);
testing.expect((try a.to(u128)) == op1);
}
test "big.int add sign" {
@ -1512,16 +1505,16 @@ test "big.int add sign" {
const neg_two = try Int.initSet(al, -2);
try a.add(one, two);
debug.assert((try a.to(i32)) == 3);
testing.expect((try a.to(i32)) == 3);
try a.add(neg_one, two);
debug.assert((try a.to(i32)) == 1);
testing.expect((try a.to(i32)) == 1);
try a.add(one, neg_two);
debug.assert((try a.to(i32)) == -1);
testing.expect((try a.to(i32)) == -1);
try a.add(neg_one, neg_two);
debug.assert((try a.to(i32)) == -3);
testing.expect((try a.to(i32)) == -3);
}
test "big.int sub single-single" {
@ -1531,7 +1524,7 @@ test "big.int sub single-single" {
var c = try Int.init(al);
try c.sub(a, b);
debug.assert((try c.to(u32)) == 45);
testing.expect((try c.to(u32)) == 45);
}
test "big.int sub multi-single" {
@ -1541,7 +1534,7 @@ test "big.int sub multi-single" {
var c = try Int.init(al);
try c.sub(a, b);
debug.assert((try c.to(Limb)) == maxInt(Limb));
testing.expect((try c.to(Limb)) == maxInt(Limb));
}
test "big.int sub multi-multi" {
@ -1554,7 +1547,7 @@ test "big.int sub multi-multi" {
var c = try Int.init(al);
try c.sub(a, b);
debug.assert((try c.to(u128)) == op1 - op2);
testing.expect((try c.to(u128)) == op1 - op2);
}
test "big.int sub equal" {
@ -1564,7 +1557,7 @@ test "big.int sub equal" {
var c = try Int.init(al);
try c.sub(a, b);
debug.assert((try c.to(u32)) == 0);
testing.expect((try c.to(u32)) == 0);
}
test "big.int sub sign" {
@ -1576,19 +1569,19 @@ test "big.int sub sign" {
const neg_two = try Int.initSet(al, -2);
try a.sub(one, two);
debug.assert((try a.to(i32)) == -1);
testing.expect((try a.to(i32)) == -1);
try a.sub(neg_one, two);
debug.assert((try a.to(i32)) == -3);
testing.expect((try a.to(i32)) == -3);
try a.sub(one, neg_two);
debug.assert((try a.to(i32)) == 3);
testing.expect((try a.to(i32)) == 3);
try a.sub(neg_one, neg_two);
debug.assert((try a.to(i32)) == 1);
testing.expect((try a.to(i32)) == 1);
try a.sub(neg_two, neg_one);
debug.assert((try a.to(i32)) == -1);
testing.expect((try a.to(i32)) == -1);
}
test "big.int mul single-single" {
@ -1598,7 +1591,7 @@ test "big.int mul single-single" {
var c = try Int.init(al);
try c.mul(a, b);
debug.assert((try c.to(u64)) == 250);
testing.expect((try c.to(u64)) == 250);
}
test "big.int mul multi-single" {
@ -1608,7 +1601,7 @@ test "big.int mul multi-single" {
var c = try Int.init(al);
try c.mul(a, b);
debug.assert((try c.to(DoubleLimb)) == 2 * maxInt(Limb));
testing.expect((try c.to(DoubleLimb)) == 2 * maxInt(Limb));
}
test "big.int mul multi-multi" {
@ -1620,7 +1613,7 @@ test "big.int mul multi-multi" {
var c = try Int.init(al);
try c.mul(a, b);
debug.assert((try c.to(u256)) == op1 * op2);
testing.expect((try c.to(u256)) == op1 * op2);
}
test "big.int mul alias r with a" {
@ -1629,7 +1622,7 @@ test "big.int mul alias r with a" {
try a.mul(a, b);
debug.assert((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
}
test "big.int mul alias r with b" {
@ -1638,7 +1631,7 @@ test "big.int mul alias r with b" {
try a.mul(b, a);
debug.assert((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb));
}
test "big.int mul alias r with a and b" {
@ -1646,7 +1639,7 @@ test "big.int mul alias r with a and b" {
try a.mul(a, a);
debug.assert((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb));
testing.expect((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb));
}
test "big.int mul a*0" {
@ -1656,7 +1649,7 @@ test "big.int mul a*0" {
var c = try Int.init(al);
try c.mul(a, b);
debug.assert((try c.to(u32)) == 0);
testing.expect((try c.to(u32)) == 0);
}
test "big.int mul 0*0" {
@ -1666,7 +1659,7 @@ test "big.int mul 0*0" {
var c = try Int.init(al);
try c.mul(a, b);
debug.assert((try c.to(u32)) == 0);
testing.expect((try c.to(u32)) == 0);
}
test "big.int div single-single no rem" {
@ -1677,8 +1670,8 @@ test "big.int div single-single no rem" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u32)) == 10);
debug.assert((try r.to(u32)) == 0);
testing.expect((try q.to(u32)) == 10);
testing.expect((try r.to(u32)) == 0);
}
test "big.int div single-single with rem" {
@ -1689,8 +1682,8 @@ test "big.int div single-single with rem" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u32)) == 9);
debug.assert((try r.to(u32)) == 4);
testing.expect((try q.to(u32)) == 9);
testing.expect((try r.to(u32)) == 4);
}
test "big.int div multi-single no rem" {
@ -1704,8 +1697,8 @@ test "big.int div multi-single no rem" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u64)) == op1 / op2);
debug.assert((try r.to(u64)) == 0);
testing.expect((try q.to(u64)) == op1 / op2);
testing.expect((try r.to(u64)) == 0);
}
test "big.int div multi-single with rem" {
@ -1719,8 +1712,8 @@ test "big.int div multi-single with rem" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u64)) == op1 / op2);
debug.assert((try r.to(u64)) == 3);
testing.expect((try q.to(u64)) == op1 / op2);
testing.expect((try r.to(u64)) == 3);
}
test "big.int div multi>2-single" {
@ -1734,8 +1727,8 @@ test "big.int div multi>2-single" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u128)) == op1 / op2);
debug.assert((try r.to(u32)) == 0x3e4e);
testing.expect((try q.to(u128)) == op1 / op2);
testing.expect((try r.to(u32)) == 0x3e4e);
}
test "big.int div single-single q < r" {
@ -1746,8 +1739,8 @@ test "big.int div single-single q < r" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u64)) == 0);
debug.assert((try r.to(u64)) == 0x0078f432);
testing.expect((try q.to(u64)) == 0);
testing.expect((try r.to(u64)) == 0x0078f432);
}
test "big.int div single-single q == r" {
@ -1758,8 +1751,8 @@ test "big.int div single-single q == r" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u64)) == 1);
debug.assert((try r.to(u64)) == 0);
testing.expect((try q.to(u64)) == 1);
testing.expect((try r.to(u64)) == 0);
}
test "big.int div q=0 alias" {
@ -1768,8 +1761,8 @@ test "big.int div q=0 alias" {
try Int.divTrunc(&a, &b, a, b);
debug.assert((try a.to(u64)) == 0);
debug.assert((try b.to(u64)) == 3);
testing.expect((try a.to(u64)) == 0);
testing.expect((try b.to(u64)) == 3);
}
test "big.int div multi-multi q < r" {
@ -1782,8 +1775,8 @@ test "big.int div multi-multi q < r" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u128)) == 0);
debug.assert((try r.to(u128)) == op1);
testing.expect((try q.to(u128)) == 0);
testing.expect((try r.to(u128)) == op1);
}
test "big.int div trunc single-single +/+" {
@ -1802,8 +1795,8 @@ test "big.int div trunc single-single +/+" {
const eq = @divTrunc(u, v);
const er = @mod(u, v);
debug.assert((try q.to(i32)) == eq);
debug.assert((try r.to(i32)) == er);
testing.expect((try q.to(i32)) == eq);
testing.expect((try r.to(i32)) == er);
}
test "big.int div trunc single-single -/+" {
@ -1822,8 +1815,8 @@ test "big.int div trunc single-single -/+" {
const eq = -1;
const er = -2;
debug.assert((try q.to(i32)) == eq);
debug.assert((try r.to(i32)) == er);
testing.expect((try q.to(i32)) == eq);
testing.expect((try r.to(i32)) == er);
}
test "big.int div trunc single-single +/-" {
@ -1842,8 +1835,8 @@ test "big.int div trunc single-single +/-" {
const eq = -1;
const er = 2;
debug.assert((try q.to(i32)) == eq);
debug.assert((try r.to(i32)) == er);
testing.expect((try q.to(i32)) == eq);
testing.expect((try r.to(i32)) == er);
}
test "big.int div trunc single-single -/-" {
@ -1862,8 +1855,8 @@ test "big.int div trunc single-single -/-" {
const eq = 1;
const er = -2;
debug.assert((try q.to(i32)) == eq);
debug.assert((try r.to(i32)) == er);
testing.expect((try q.to(i32)) == eq);
testing.expect((try r.to(i32)) == er);
}
test "big.int div floor single-single +/+" {
@ -1882,8 +1875,8 @@ test "big.int div floor single-single +/+" {
const eq = 1;
const er = 2;
debug.assert((try q.to(i32)) == eq);
debug.assert((try r.to(i32)) == er);
testing.expect((try q.to(i32)) == eq);
testing.expect((try r.to(i32)) == er);
}
test "big.int div floor single-single -/+" {
@ -1902,8 +1895,8 @@ test "big.int div floor single-single -/+" {
const eq = -2;
const er = 1;
debug.assert((try q.to(i32)) == eq);
debug.assert((try r.to(i32)) == er);
testing.expect((try q.to(i32)) == eq);
testing.expect((try r.to(i32)) == er);
}
test "big.int div floor single-single +/-" {
@ -1922,8 +1915,8 @@ test "big.int div floor single-single +/-" {
const eq = -2;
const er = -1;
debug.assert((try q.to(i32)) == eq);
debug.assert((try r.to(i32)) == er);
testing.expect((try q.to(i32)) == eq);
testing.expect((try r.to(i32)) == er);
}
test "big.int div floor single-single -/-" {
@ -1942,8 +1935,8 @@ test "big.int div floor single-single -/-" {
const eq = 1;
const er = -2;
debug.assert((try q.to(i32)) == eq);
debug.assert((try r.to(i32)) == er);
testing.expect((try q.to(i32)) == eq);
testing.expect((try r.to(i32)) == er);
}
test "big.int div multi-multi with rem" {
@ -1954,8 +1947,8 @@ test "big.int div multi-multi with rem" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
debug.assert((try r.to(u128)) == 0x28de0acacd806823638);
testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
testing.expect((try r.to(u128)) == 0x28de0acacd806823638);
}
test "big.int div multi-multi no rem" {
@ -1966,8 +1959,8 @@ test "big.int div multi-multi no rem" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
debug.assert((try r.to(u128)) == 0);
testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b);
testing.expect((try r.to(u128)) == 0);
}
test "big.int div multi-multi (2 branch)" {
@ -1978,8 +1971,8 @@ test "big.int div multi-multi (2 branch)" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u128)) == 0x10000000000000000);
debug.assert((try r.to(u128)) == 0x44444443444444431111111111111111);
testing.expect((try q.to(u128)) == 0x10000000000000000);
testing.expect((try r.to(u128)) == 0x44444443444444431111111111111111);
}
test "big.int div multi-multi (3.1/3.3 branch)" {
@ -1990,53 +1983,53 @@ test "big.int div multi-multi (3.1/3.3 branch)" {
var r = try Int.init(al);
try Int.divTrunc(&q, &r, a, b);
debug.assert((try q.to(u128)) == 0xfffffffffffffffffff);
debug.assert((try r.to(u256)) == 0x1111111111111111111110b12222222222222222282);
testing.expect((try q.to(u128)) == 0xfffffffffffffffffff);
testing.expect((try r.to(u256)) == 0x1111111111111111111110b12222222222222222282);
}
test "big.int shift-right single" {
var a = try Int.initSet(al, 0xffff0000);
try a.shiftRight(a, 16);
debug.assert((try a.to(u32)) == 0xffff);
testing.expect((try a.to(u32)) == 0xffff);
}
test "big.int shift-right multi" {
var a = try Int.initSet(al, 0xffff0000eeee1111dddd2222cccc3333);
try a.shiftRight(a, 67);
debug.assert((try a.to(u64)) == 0x1fffe0001dddc222);
testing.expect((try a.to(u64)) == 0x1fffe0001dddc222);
}
test "big.int shift-left single" {
var a = try Int.initSet(al, 0xffff);
try a.shiftLeft(a, 16);
debug.assert((try a.to(u64)) == 0xffff0000);
testing.expect((try a.to(u64)) == 0xffff0000);
}
test "big.int shift-left multi" {
var a = try Int.initSet(al, 0x1fffe0001dddc222);
try a.shiftLeft(a, 67);
debug.assert((try a.to(u128)) == 0xffff0000eeee11100000000000000000);
testing.expect((try a.to(u128)) == 0xffff0000eeee11100000000000000000);
}
test "big.int shift-right negative" {
var a = try Int.init(al);
try a.shiftRight(try Int.initSet(al, -20), 2);
debug.assert((try a.to(i32)) == -20 >> 2);
testing.expect((try a.to(i32)) == -20 >> 2);
try a.shiftRight(try Int.initSet(al, -5), 10);
debug.assert((try a.to(i32)) == -5 >> 10);
testing.expect((try a.to(i32)) == -5 >> 10);
}
test "big.int shift-left negative" {
var a = try Int.init(al);
try a.shiftRight(try Int.initSet(al, -10), 1232);
debug.assert((try a.to(i32)) == -10 >> 1232);
testing.expect((try a.to(i32)) == -10 >> 1232);
}
test "big.int bitwise and simple" {
@ -2045,7 +2038,7 @@ test "big.int bitwise and simple" {
try a.bitAnd(a, b);
debug.assert((try a.to(u64)) == 0xeeeeeeee00000000);
testing.expect((try a.to(u64)) == 0xeeeeeeee00000000);
}
test "big.int bitwise and multi-limb" {
@ -2054,7 +2047,7 @@ test "big.int bitwise and multi-limb" {
try a.bitAnd(a, b);
debug.assert((try a.to(u128)) == 0);
testing.expect((try a.to(u128)) == 0);
}
test "big.int bitwise xor simple" {
@ -2063,7 +2056,7 @@ test "big.int bitwise xor simple" {
try a.bitXor(a, b);
debug.assert((try a.to(u64)) == 0x1111111133333333);
testing.expect((try a.to(u64)) == 0x1111111133333333);
}
test "big.int bitwise xor multi-limb" {
@ -2072,7 +2065,7 @@ test "big.int bitwise xor multi-limb" {
try a.bitXor(a, b);
debug.assert((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb));
testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb));
}
test "big.int bitwise or simple" {
@ -2081,7 +2074,7 @@ test "big.int bitwise or simple" {
try a.bitOr(a, b);
debug.assert((try a.to(u64)) == 0xffffffff33333333);
testing.expect((try a.to(u64)) == 0xffffffff33333333);
}
test "big.int bitwise or multi-limb" {
@ -2091,15 +2084,15 @@ test "big.int bitwise or multi-limb" {
try a.bitOr(a, b);
// TODO: big.int.cpp or is wrong on multi-limb.
debug.assert((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb));
testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb));
}
test "big.int var args" {
var a = try Int.initSet(al, 5);
try a.add(a, try Int.initSet(al, 6));
debug.assert((try a.to(u64)) == 11);
testing.expect((try a.to(u64)) == 11);
debug.assert(a.cmp(try Int.initSet(al, 11)) == 0);
debug.assert(a.cmp(try Int.initSet(al, 14)) <= 0);
testing.expect(a.cmp(try Int.initSet(al, 11)) == 0);
testing.expect(a.cmp(try Int.initSet(al, 14)) <= 0);
}

View File

@ -6,7 +6,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn cbrt(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -114,44 +114,44 @@ fn cbrt64(x: f64) f64 {
}
test "math.cbrt" {
assert(cbrt(f32(0.0)) == cbrt32(0.0));
assert(cbrt(f64(0.0)) == cbrt64(0.0));
expect(cbrt(f32(0.0)) == cbrt32(0.0));
expect(cbrt(f64(0.0)) == cbrt64(0.0));
}
test "math.cbrt32" {
const epsilon = 0.000001;
assert(cbrt32(0.0) == 0.0);
assert(math.approxEq(f32, cbrt32(0.2), 0.584804, epsilon));
assert(math.approxEq(f32, cbrt32(0.8923), 0.962728, epsilon));
assert(math.approxEq(f32, cbrt32(1.5), 1.144714, epsilon));
assert(math.approxEq(f32, cbrt32(37.45), 3.345676, epsilon));
assert(math.approxEq(f32, cbrt32(123123.234375), 49.748501, epsilon));
expect(cbrt32(0.0) == 0.0);
expect(math.approxEq(f32, cbrt32(0.2), 0.584804, epsilon));
expect(math.approxEq(f32, cbrt32(0.8923), 0.962728, epsilon));
expect(math.approxEq(f32, cbrt32(1.5), 1.144714, epsilon));
expect(math.approxEq(f32, cbrt32(37.45), 3.345676, epsilon));
expect(math.approxEq(f32, cbrt32(123123.234375), 49.748501, epsilon));
}
test "math.cbrt64" {
const epsilon = 0.000001;
assert(cbrt64(0.0) == 0.0);
assert(math.approxEq(f64, cbrt64(0.2), 0.584804, epsilon));
assert(math.approxEq(f64, cbrt64(0.8923), 0.962728, epsilon));
assert(math.approxEq(f64, cbrt64(1.5), 1.144714, epsilon));
assert(math.approxEq(f64, cbrt64(37.45), 3.345676, epsilon));
assert(math.approxEq(f64, cbrt64(123123.234375), 49.748501, epsilon));
expect(cbrt64(0.0) == 0.0);
expect(math.approxEq(f64, cbrt64(0.2), 0.584804, epsilon));
expect(math.approxEq(f64, cbrt64(0.8923), 0.962728, epsilon));
expect(math.approxEq(f64, cbrt64(1.5), 1.144714, epsilon));
expect(math.approxEq(f64, cbrt64(37.45), 3.345676, epsilon));
expect(math.approxEq(f64, cbrt64(123123.234375), 49.748501, epsilon));
}
test "math.cbrt.special" {
assert(cbrt32(0.0) == 0.0);
assert(cbrt32(-0.0) == -0.0);
assert(math.isPositiveInf(cbrt32(math.inf(f32))));
assert(math.isNegativeInf(cbrt32(-math.inf(f32))));
assert(math.isNan(cbrt32(math.nan(f32))));
expect(cbrt32(0.0) == 0.0);
expect(cbrt32(-0.0) == -0.0);
expect(math.isPositiveInf(cbrt32(math.inf(f32))));
expect(math.isNegativeInf(cbrt32(-math.inf(f32))));
expect(math.isNan(cbrt32(math.nan(f32))));
}
test "math.cbrt64.special" {
assert(cbrt64(0.0) == 0.0);
assert(cbrt64(-0.0) == -0.0);
assert(math.isPositiveInf(cbrt64(math.inf(f64))));
assert(math.isNegativeInf(cbrt64(-math.inf(f64))));
assert(math.isNan(cbrt64(math.nan(f64))));
expect(cbrt64(0.0) == 0.0);
expect(cbrt64(-0.0) == -0.0);
expect(math.isPositiveInf(cbrt64(math.inf(f64))));
expect(math.isNegativeInf(cbrt64(-math.inf(f64))));
expect(math.isNan(cbrt64(math.nan(f64))));
}

View File

@ -7,7 +7,7 @@
const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn ceil(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -81,34 +81,34 @@ fn ceil64(x: f64) f64 {
}
test "math.ceil" {
assert(ceil(f32(0.0)) == ceil32(0.0));
assert(ceil(f64(0.0)) == ceil64(0.0));
expect(ceil(f32(0.0)) == ceil32(0.0));
expect(ceil(f64(0.0)) == ceil64(0.0));
}
test "math.ceil32" {
assert(ceil32(1.3) == 2.0);
assert(ceil32(-1.3) == -1.0);
assert(ceil32(0.2) == 1.0);
expect(ceil32(1.3) == 2.0);
expect(ceil32(-1.3) == -1.0);
expect(ceil32(0.2) == 1.0);
}
test "math.ceil64" {
assert(ceil64(1.3) == 2.0);
assert(ceil64(-1.3) == -1.0);
assert(ceil64(0.2) == 1.0);
expect(ceil64(1.3) == 2.0);
expect(ceil64(-1.3) == -1.0);
expect(ceil64(0.2) == 1.0);
}
test "math.ceil32.special" {
assert(ceil32(0.0) == 0.0);
assert(ceil32(-0.0) == -0.0);
assert(math.isPositiveInf(ceil32(math.inf(f32))));
assert(math.isNegativeInf(ceil32(-math.inf(f32))));
assert(math.isNan(ceil32(math.nan(f32))));
expect(ceil32(0.0) == 0.0);
expect(ceil32(-0.0) == -0.0);
expect(math.isPositiveInf(ceil32(math.inf(f32))));
expect(math.isNegativeInf(ceil32(-math.inf(f32))));
expect(math.isNan(ceil32(math.nan(f32))));
}
test "math.ceil64.special" {
assert(ceil64(0.0) == 0.0);
assert(ceil64(-0.0) == -0.0);
assert(math.isPositiveInf(ceil64(math.inf(f64))));
assert(math.isNegativeInf(ceil64(-math.inf(f64))));
assert(math.isNan(ceil64(math.nan(f64))));
expect(ceil64(0.0) == 0.0);
expect(ceil64(-0.0) == -0.0);
expect(math.isPositiveInf(ceil64(math.inf(f64))));
expect(math.isNegativeInf(ceil64(-math.inf(f64))));
expect(math.isNan(ceil64(math.nan(f64))));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -14,5 +14,5 @@ const epsilon = 0.0001;
test "complex.cabs" {
const a = Complex(f32).new(5, 3);
const c = abs(a);
debug.assert(math.approxEq(f32, c, 5.83095, epsilon));
testing.expect(math.approxEq(f32, c, 5.83095, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -16,6 +16,6 @@ test "complex.cacos" {
const a = Complex(f32).new(5, 3);
const c = acos(a);
debug.assert(math.approxEq(f32, c.re, 0.546975, epsilon));
debug.assert(math.approxEq(f32, c.im, -2.452914, epsilon));
testing.expect(math.approxEq(f32, c.re, 0.546975, epsilon));
testing.expect(math.approxEq(f32, c.im, -2.452914, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -16,6 +16,6 @@ test "complex.cacosh" {
const a = Complex(f32).new(5, 3);
const c = acosh(a);
debug.assert(math.approxEq(f32, c.re, 2.452914, epsilon));
debug.assert(math.approxEq(f32, c.im, 0.546975, epsilon));
testing.expect(math.approxEq(f32, c.re, 2.452914, epsilon));
testing.expect(math.approxEq(f32, c.im, 0.546975, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -14,5 +14,5 @@ const epsilon = 0.0001;
test "complex.carg" {
const a = Complex(f32).new(5, 3);
const c = arg(a);
debug.assert(math.approxEq(f32, c, 0.540420, epsilon));
testing.expect(math.approxEq(f32, c, 0.540420, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -22,6 +22,6 @@ test "complex.casin" {
const a = Complex(f32).new(5, 3);
const c = asin(a);
debug.assert(math.approxEq(f32, c.re, 1.023822, epsilon));
debug.assert(math.approxEq(f32, c.im, 2.452914, epsilon));
testing.expect(math.approxEq(f32, c.re, 1.023822, epsilon));
testing.expect(math.approxEq(f32, c.im, 2.452914, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -17,6 +17,6 @@ test "complex.casinh" {
const a = Complex(f32).new(5, 3);
const c = asinh(a);
debug.assert(math.approxEq(f32, c.re, 2.459831, epsilon));
debug.assert(math.approxEq(f32, c.im, 0.533999, epsilon));
testing.expect(math.approxEq(f32, c.re, 2.459831, epsilon));
testing.expect(math.approxEq(f32, c.im, 0.533999, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -117,14 +117,14 @@ test "complex.catan32" {
const a = Complex(f32).new(5, 3);
const c = atan(a);
debug.assert(math.approxEq(f32, c.re, 1.423679, epsilon));
debug.assert(math.approxEq(f32, c.im, 0.086569, epsilon));
testing.expect(math.approxEq(f32, c.re, 1.423679, epsilon));
testing.expect(math.approxEq(f32, c.im, 0.086569, epsilon));
}
test "complex.catan64" {
const a = Complex(f64).new(5, 3);
const c = atan(a);
debug.assert(math.approxEq(f64, c.re, 1.423679, epsilon));
debug.assert(math.approxEq(f64, c.im, 0.086569, epsilon));
testing.expect(math.approxEq(f64, c.re, 1.423679, epsilon));
testing.expect(math.approxEq(f64, c.im, 0.086569, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -17,6 +17,6 @@ test "complex.catanh" {
const a = Complex(f32).new(5, 3);
const c = atanh(a);
debug.assert(math.approxEq(f32, c.re, 0.146947, epsilon));
debug.assert(math.approxEq(f32, c.im, 1.480870, epsilon));
testing.expect(math.approxEq(f32, c.re, 0.146947, epsilon));
testing.expect(math.approxEq(f32, c.im, 1.480870, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -13,5 +13,5 @@ test "complex.conj" {
const a = Complex(f32).new(5, 3);
const c = a.conjugate();
debug.assert(c.re == 5 and c.im == -3);
testing.expect(c.re == 5 and c.im == -3);
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -16,6 +16,6 @@ test "complex.ccos" {
const a = Complex(f32).new(5, 3);
const c = cos(a);
debug.assert(math.approxEq(f32, c.re, 2.855815, epsilon));
debug.assert(math.approxEq(f32, c.im, 9.606383, epsilon));
testing.expect(math.approxEq(f32, c.re, 2.855815, epsilon));
testing.expect(math.approxEq(f32, c.im, 9.606383, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -152,14 +152,14 @@ test "complex.ccosh32" {
const a = Complex(f32).new(5, 3);
const c = cosh(a);
debug.assert(math.approxEq(f32, c.re, -73.467300, epsilon));
debug.assert(math.approxEq(f32, c.im, 10.471557, epsilon));
testing.expect(math.approxEq(f32, c.re, -73.467300, epsilon));
testing.expect(math.approxEq(f32, c.im, 10.471557, epsilon));
}
test "complex.ccosh64" {
const a = Complex(f64).new(5, 3);
const c = cosh(a);
debug.assert(math.approxEq(f64, c.re, -73.467300, epsilon));
debug.assert(math.approxEq(f64, c.im, 10.471557, epsilon));
testing.expect(math.approxEq(f64, c.re, -73.467300, epsilon));
testing.expect(math.approxEq(f64, c.im, 10.471557, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -118,14 +118,14 @@ test "complex.cexp32" {
const a = Complex(f32).new(5, 3);
const c = exp(a);
debug.assert(math.approxEq(f32, c.re, -146.927917, epsilon));
debug.assert(math.approxEq(f32, c.im, 20.944065, epsilon));
testing.expect(math.approxEq(f32, c.re, -146.927917, epsilon));
testing.expect(math.approxEq(f32, c.im, 20.944065, epsilon));
}
test "complex.cexp64" {
const a = Complex(f64).new(5, 3);
const c = exp(a);
debug.assert(math.approxEq(f64, c.re, -146.927917, epsilon));
debug.assert(math.approxEq(f64, c.im, 20.944065, epsilon));
testing.expect(math.approxEq(f64, c.re, -146.927917, epsilon));
testing.expect(math.approxEq(f64, c.im, 20.944065, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
pub const abs = @import("abs.zig").abs;
@ -97,7 +97,7 @@ test "complex.add" {
const b = Complex(f32).new(2, 7);
const c = a.add(b);
debug.assert(c.re == 7 and c.im == 10);
testing.expect(c.re == 7 and c.im == 10);
}
test "complex.sub" {
@ -105,7 +105,7 @@ test "complex.sub" {
const b = Complex(f32).new(2, 7);
const c = a.sub(b);
debug.assert(c.re == 3 and c.im == -4);
testing.expect(c.re == 3 and c.im == -4);
}
test "complex.mul" {
@ -113,7 +113,7 @@ test "complex.mul" {
const b = Complex(f32).new(2, 7);
const c = a.mul(b);
debug.assert(c.re == -11 and c.im == 41);
testing.expect(c.re == -11 and c.im == 41);
}
test "complex.div" {
@ -121,7 +121,7 @@ test "complex.div" {
const b = Complex(f32).new(2, 7);
const c = a.div(b);
debug.assert(math.approxEq(f32, c.re, f32(31) / 53, epsilon) and
testing.expect(math.approxEq(f32, c.re, f32(31) / 53, epsilon) and
math.approxEq(f32, c.im, f32(-29) / 53, epsilon));
}
@ -129,14 +129,14 @@ test "complex.conjugate" {
const a = Complex(f32).new(5, 3);
const c = a.conjugate();
debug.assert(c.re == 5 and c.im == -3);
testing.expect(c.re == 5 and c.im == -3);
}
test "complex.reciprocal" {
const a = Complex(f32).new(5, 3);
const c = a.reciprocal();
debug.assert(math.approxEq(f32, c.re, f32(5) / 34, epsilon) and
testing.expect(math.approxEq(f32, c.re, f32(5) / 34, epsilon) and
math.approxEq(f32, c.im, f32(-3) / 34, epsilon));
}
@ -144,7 +144,7 @@ test "complex.magnitude" {
const a = Complex(f32).new(5, 3);
const c = a.magnitude();
debug.assert(math.approxEq(f32, c, 5.83095, epsilon));
testing.expect(math.approxEq(f32, c, 5.83095, epsilon));
}
test "complex.cmath" {

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -18,6 +18,6 @@ test "complex.clog" {
const a = Complex(f32).new(5, 3);
const c = log(a);
debug.assert(math.approxEq(f32, c.re, 1.763180, epsilon));
debug.assert(math.approxEq(f32, c.im, 0.540419, epsilon));
testing.expect(math.approxEq(f32, c.re, 1.763180, epsilon));
testing.expect(math.approxEq(f32, c.im, 0.540419, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -17,6 +17,6 @@ test "complex.cpow" {
const b = Complex(f32).new(2.3, -1.3);
const c = pow(Complex(f32), a, b);
debug.assert(math.approxEq(f32, c.re, 58.049110, epsilon));
debug.assert(math.approxEq(f32, c.im, -101.003433, epsilon));
testing.expect(math.approxEq(f32, c.re, 58.049110, epsilon));
testing.expect(math.approxEq(f32, c.im, -101.003433, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -20,5 +20,5 @@ test "complex.cproj" {
const a = Complex(f32).new(5, 3);
const c = proj(a);
debug.assert(c.re == 5 and c.im == 3);
testing.expect(c.re == 5 and c.im == 3);
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -17,6 +17,6 @@ test "complex.csin" {
const a = Complex(f32).new(5, 3);
const c = sin(a);
debug.assert(math.approxEq(f32, c.re, -9.654126, epsilon));
debug.assert(math.approxEq(f32, c.im, 2.841692, epsilon));
testing.expect(math.approxEq(f32, c.re, -9.654126, epsilon));
testing.expect(math.approxEq(f32, c.im, 2.841692, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -151,14 +151,14 @@ test "complex.csinh32" {
const a = Complex(f32).new(5, 3);
const c = sinh(a);
debug.assert(math.approxEq(f32, c.re, -73.460617, epsilon));
debug.assert(math.approxEq(f32, c.im, 10.472508, epsilon));
testing.expect(math.approxEq(f32, c.re, -73.460617, epsilon));
testing.expect(math.approxEq(f32, c.im, 10.472508, epsilon));
}
test "complex.csinh64" {
const a = Complex(f64).new(5, 3);
const c = sinh(a);
debug.assert(math.approxEq(f64, c.re, -73.460617, epsilon));
debug.assert(math.approxEq(f64, c.im, 10.472508, epsilon));
testing.expect(math.approxEq(f64, c.re, -73.460617, epsilon));
testing.expect(math.approxEq(f64, c.im, 10.472508, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -125,14 +125,14 @@ test "complex.csqrt32" {
const a = Complex(f32).new(5, 3);
const c = sqrt(a);
debug.assert(math.approxEq(f32, c.re, 2.327117, epsilon));
debug.assert(math.approxEq(f32, c.im, 0.644574, epsilon));
testing.expect(math.approxEq(f32, c.re, 2.327117, epsilon));
testing.expect(math.approxEq(f32, c.im, 0.644574, epsilon));
}
test "complex.csqrt64" {
const a = Complex(f64).new(5, 3);
const c = sqrt(a);
debug.assert(math.approxEq(f64, c.re, 2.3271175190399496, epsilon));
debug.assert(math.approxEq(f64, c.im, 0.6445742373246469, epsilon));
testing.expect(math.approxEq(f64, c.re, 2.3271175190399496, epsilon));
testing.expect(math.approxEq(f64, c.im, 0.6445742373246469, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -17,6 +17,6 @@ test "complex.ctan" {
const a = Complex(f32).new(5, 3);
const c = tan(a);
debug.assert(math.approxEq(f32, c.re, -0.002708233, epsilon));
debug.assert(math.approxEq(f32, c.im, 1.004165, epsilon));
testing.expect(math.approxEq(f32, c.re, -0.002708233, epsilon));
testing.expect(math.approxEq(f32, c.im, 1.004165, epsilon));
}

View File

@ -1,5 +1,5 @@
const std = @import("../../index.zig");
const debug = std.debug;
const testing = std.testing;
const math = std.math;
const cmath = math.complex;
const Complex = cmath.Complex;
@ -100,14 +100,14 @@ test "complex.ctanh32" {
const a = Complex(f32).new(5, 3);
const c = tanh(a);
debug.assert(math.approxEq(f32, c.re, 0.999913, epsilon));
debug.assert(math.approxEq(f32, c.im, -0.000025, epsilon));
testing.expect(math.approxEq(f32, c.re, 0.999913, epsilon));
testing.expect(math.approxEq(f32, c.im, -0.000025, epsilon));
}
test "complex.ctanh64" {
const a = Complex(f64).new(5, 3);
const c = tanh(a);
debug.assert(math.approxEq(f64, c.re, 0.999913, epsilon));
debug.assert(math.approxEq(f64, c.im, -0.000025, epsilon));
testing.expect(math.approxEq(f64, c.re, 0.999913, epsilon));
testing.expect(math.approxEq(f64, c.im, -0.000025, epsilon));
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn copysign(comptime T: type, x: T, y: T) T {
@ -40,28 +40,28 @@ fn copysign64(x: f64, y: f64) f64 {
}
test "math.copysign" {
assert(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0));
assert(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0));
assert(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0));
expect(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0));
expect(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0));
expect(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0));
}
test "math.copysign16" {
assert(copysign16(5.0, 1.0) == 5.0);
assert(copysign16(5.0, -1.0) == -5.0);
assert(copysign16(-5.0, -1.0) == -5.0);
assert(copysign16(-5.0, 1.0) == 5.0);
expect(copysign16(5.0, 1.0) == 5.0);
expect(copysign16(5.0, -1.0) == -5.0);
expect(copysign16(-5.0, -1.0) == -5.0);
expect(copysign16(-5.0, 1.0) == 5.0);
}
test "math.copysign32" {
assert(copysign32(5.0, 1.0) == 5.0);
assert(copysign32(5.0, -1.0) == -5.0);
assert(copysign32(-5.0, -1.0) == -5.0);
assert(copysign32(-5.0, 1.0) == 5.0);
expect(copysign32(5.0, 1.0) == 5.0);
expect(copysign32(5.0, -1.0) == -5.0);
expect(copysign32(-5.0, -1.0) == -5.0);
expect(copysign32(-5.0, 1.0) == 5.0);
}
test "math.copysign64" {
assert(copysign64(5.0, 1.0) == 5.0);
assert(copysign64(5.0, -1.0) == -5.0);
assert(copysign64(-5.0, -1.0) == -5.0);
assert(copysign64(-5.0, 1.0) == 5.0);
expect(copysign64(5.0, 1.0) == 5.0);
expect(copysign64(5.0, -1.0) == -5.0);
expect(copysign64(-5.0, -1.0) == -5.0);
expect(copysign64(-5.0, 1.0) == 5.0);
}

View File

@ -6,7 +6,7 @@
const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn cos(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -139,40 +139,40 @@ fn cos64(x_: f64) f64 {
}
test "math.cos" {
assert(cos(f32(0.0)) == cos32(0.0));
assert(cos(f64(0.0)) == cos64(0.0));
expect(cos(f32(0.0)) == cos32(0.0));
expect(cos(f64(0.0)) == cos64(0.0));
}
test "math.cos32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, cos32(0.0), 1.0, epsilon));
assert(math.approxEq(f32, cos32(0.2), 0.980067, epsilon));
assert(math.approxEq(f32, cos32(0.8923), 0.627623, epsilon));
assert(math.approxEq(f32, cos32(1.5), 0.070737, epsilon));
assert(math.approxEq(f32, cos32(37.45), 0.969132, epsilon));
assert(math.approxEq(f32, cos32(89.123), 0.400798, epsilon));
expect(math.approxEq(f32, cos32(0.0), 1.0, epsilon));
expect(math.approxEq(f32, cos32(0.2), 0.980067, epsilon));
expect(math.approxEq(f32, cos32(0.8923), 0.627623, epsilon));
expect(math.approxEq(f32, cos32(1.5), 0.070737, epsilon));
expect(math.approxEq(f32, cos32(37.45), 0.969132, epsilon));
expect(math.approxEq(f32, cos32(89.123), 0.400798, epsilon));
}
test "math.cos64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, cos64(0.0), 1.0, epsilon));
assert(math.approxEq(f64, cos64(0.2), 0.980067, epsilon));
assert(math.approxEq(f64, cos64(0.8923), 0.627623, epsilon));
assert(math.approxEq(f64, cos64(1.5), 0.070737, epsilon));
assert(math.approxEq(f64, cos64(37.45), 0.969132, epsilon));
assert(math.approxEq(f64, cos64(89.123), 0.40080, epsilon));
expect(math.approxEq(f64, cos64(0.0), 1.0, epsilon));
expect(math.approxEq(f64, cos64(0.2), 0.980067, epsilon));
expect(math.approxEq(f64, cos64(0.8923), 0.627623, epsilon));
expect(math.approxEq(f64, cos64(1.5), 0.070737, epsilon));
expect(math.approxEq(f64, cos64(37.45), 0.969132, epsilon));
expect(math.approxEq(f64, cos64(89.123), 0.40080, epsilon));
}
test "math.cos32.special" {
assert(math.isNan(cos32(math.inf(f32))));
assert(math.isNan(cos32(-math.inf(f32))));
assert(math.isNan(cos32(math.nan(f32))));
expect(math.isNan(cos32(math.inf(f32))));
expect(math.isNan(cos32(-math.inf(f32))));
expect(math.isNan(cos32(math.nan(f32))));
}
test "math.cos64.special" {
assert(math.isNan(cos64(math.inf(f64))));
assert(math.isNan(cos64(-math.inf(f64))));
assert(math.isNan(cos64(math.nan(f64))));
expect(math.isNan(cos64(math.inf(f64))));
expect(math.isNan(cos64(-math.inf(f64))));
expect(math.isNan(cos64(math.nan(f64))));
}

View File

@ -8,7 +8,7 @@ const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const expo2 = @import("expo2.zig").expo2;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn cosh(x: var) @typeOf(x) {
@ -82,40 +82,40 @@ fn cosh64(x: f64) f64 {
}
test "math.cosh" {
assert(cosh(f32(1.5)) == cosh32(1.5));
assert(cosh(f64(1.5)) == cosh64(1.5));
expect(cosh(f32(1.5)) == cosh32(1.5));
expect(cosh(f64(1.5)) == cosh64(1.5));
}
test "math.cosh32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, cosh32(0.0), 1.0, epsilon));
assert(math.approxEq(f32, cosh32(0.2), 1.020067, epsilon));
assert(math.approxEq(f32, cosh32(0.8923), 1.425225, epsilon));
assert(math.approxEq(f32, cosh32(1.5), 2.352410, epsilon));
expect(math.approxEq(f32, cosh32(0.0), 1.0, epsilon));
expect(math.approxEq(f32, cosh32(0.2), 1.020067, epsilon));
expect(math.approxEq(f32, cosh32(0.8923), 1.425225, epsilon));
expect(math.approxEq(f32, cosh32(1.5), 2.352410, epsilon));
}
test "math.cosh64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, cosh64(0.0), 1.0, epsilon));
assert(math.approxEq(f64, cosh64(0.2), 1.020067, epsilon));
assert(math.approxEq(f64, cosh64(0.8923), 1.425225, epsilon));
assert(math.approxEq(f64, cosh64(1.5), 2.352410, epsilon));
expect(math.approxEq(f64, cosh64(0.0), 1.0, epsilon));
expect(math.approxEq(f64, cosh64(0.2), 1.020067, epsilon));
expect(math.approxEq(f64, cosh64(0.8923), 1.425225, epsilon));
expect(math.approxEq(f64, cosh64(1.5), 2.352410, epsilon));
}
test "math.cosh32.special" {
assert(cosh32(0.0) == 1.0);
assert(cosh32(-0.0) == 1.0);
assert(math.isPositiveInf(cosh32(math.inf(f32))));
assert(math.isPositiveInf(cosh32(-math.inf(f32))));
assert(math.isNan(cosh32(math.nan(f32))));
expect(cosh32(0.0) == 1.0);
expect(cosh32(-0.0) == 1.0);
expect(math.isPositiveInf(cosh32(math.inf(f32))));
expect(math.isPositiveInf(cosh32(-math.inf(f32))));
expect(math.isNan(cosh32(math.nan(f32))));
}
test "math.cosh64.special" {
assert(cosh64(0.0) == 1.0);
assert(cosh64(-0.0) == 1.0);
assert(math.isPositiveInf(cosh64(math.inf(f64))));
assert(math.isPositiveInf(cosh64(-math.inf(f64))));
assert(math.isNan(cosh64(math.nan(f64))));
expect(cosh64(0.0) == 1.0);
expect(cosh64(-0.0) == 1.0);
expect(math.isPositiveInf(cosh64(math.inf(f64))));
expect(math.isPositiveInf(cosh64(-math.inf(f64))));
expect(math.isNan(cosh64(math.nan(f64))));
}

View File

@ -5,7 +5,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn exp2(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -415,35 +415,35 @@ fn exp2_64(x: f64) f64 {
}
test "math.exp2" {
assert(exp2(f32(0.8923)) == exp2_32(0.8923));
assert(exp2(f64(0.8923)) == exp2_64(0.8923));
expect(exp2(f32(0.8923)) == exp2_32(0.8923));
expect(exp2(f64(0.8923)) == exp2_64(0.8923));
}
test "math.exp2_32" {
const epsilon = 0.000001;
assert(exp2_32(0.0) == 1.0);
assert(math.approxEq(f32, exp2_32(0.2), 1.148698, epsilon));
assert(math.approxEq(f32, exp2_32(0.8923), 1.856133, epsilon));
assert(math.approxEq(f32, exp2_32(1.5), 2.828427, epsilon));
assert(math.approxEq(f32, exp2_32(37.45), 187747237888, epsilon));
expect(exp2_32(0.0) == 1.0);
expect(math.approxEq(f32, exp2_32(0.2), 1.148698, epsilon));
expect(math.approxEq(f32, exp2_32(0.8923), 1.856133, epsilon));
expect(math.approxEq(f32, exp2_32(1.5), 2.828427, epsilon));
expect(math.approxEq(f32, exp2_32(37.45), 187747237888, epsilon));
}
test "math.exp2_64" {
const epsilon = 0.000001;
assert(exp2_64(0.0) == 1.0);
assert(math.approxEq(f64, exp2_64(0.2), 1.148698, epsilon));
assert(math.approxEq(f64, exp2_64(0.8923), 1.856133, epsilon));
assert(math.approxEq(f64, exp2_64(1.5), 2.828427, epsilon));
expect(exp2_64(0.0) == 1.0);
expect(math.approxEq(f64, exp2_64(0.2), 1.148698, epsilon));
expect(math.approxEq(f64, exp2_64(0.8923), 1.856133, epsilon));
expect(math.approxEq(f64, exp2_64(1.5), 2.828427, epsilon));
}
test "math.exp2_32.special" {
assert(math.isPositiveInf(exp2_32(math.inf(f32))));
assert(math.isNan(exp2_32(math.nan(f32))));
expect(math.isPositiveInf(exp2_32(math.inf(f32))));
expect(math.isNan(exp2_32(math.nan(f32))));
}
test "math.exp2_64.special" {
assert(math.isPositiveInf(exp2_64(math.inf(f64))));
assert(math.isNan(exp2_64(math.nan(f64))));
expect(math.isPositiveInf(exp2_64(math.inf(f64))));
expect(math.isNan(exp2_64(math.nan(f64))));
}

View File

@ -7,7 +7,7 @@
const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn expm1(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -278,42 +278,42 @@ fn expm1_64(x_: f64) f64 {
}
test "math.exp1m" {
assert(expm1(f32(0.0)) == expm1_32(0.0));
assert(expm1(f64(0.0)) == expm1_64(0.0));
expect(expm1(f32(0.0)) == expm1_32(0.0));
expect(expm1(f64(0.0)) == expm1_64(0.0));
}
test "math.expm1_32" {
const epsilon = 0.000001;
assert(expm1_32(0.0) == 0.0);
assert(math.approxEq(f32, expm1_32(0.0), 0.0, epsilon));
assert(math.approxEq(f32, expm1_32(0.2), 0.221403, epsilon));
assert(math.approxEq(f32, expm1_32(0.8923), 1.440737, epsilon));
assert(math.approxEq(f32, expm1_32(1.5), 3.481689, epsilon));
expect(expm1_32(0.0) == 0.0);
expect(math.approxEq(f32, expm1_32(0.0), 0.0, epsilon));
expect(math.approxEq(f32, expm1_32(0.2), 0.221403, epsilon));
expect(math.approxEq(f32, expm1_32(0.8923), 1.440737, epsilon));
expect(math.approxEq(f32, expm1_32(1.5), 3.481689, epsilon));
}
test "math.expm1_64" {
const epsilon = 0.000001;
assert(expm1_64(0.0) == 0.0);
assert(math.approxEq(f64, expm1_64(0.0), 0.0, epsilon));
assert(math.approxEq(f64, expm1_64(0.2), 0.221403, epsilon));
assert(math.approxEq(f64, expm1_64(0.8923), 1.440737, epsilon));
assert(math.approxEq(f64, expm1_64(1.5), 3.481689, epsilon));
expect(expm1_64(0.0) == 0.0);
expect(math.approxEq(f64, expm1_64(0.0), 0.0, epsilon));
expect(math.approxEq(f64, expm1_64(0.2), 0.221403, epsilon));
expect(math.approxEq(f64, expm1_64(0.8923), 1.440737, epsilon));
expect(math.approxEq(f64, expm1_64(1.5), 3.481689, epsilon));
}
test "math.expm1_32.special" {
const epsilon = 0.000001;
assert(math.isPositiveInf(expm1_32(math.inf(f32))));
assert(expm1_32(-math.inf(f32)) == -1.0);
assert(math.isNan(expm1_32(math.nan(f32))));
expect(math.isPositiveInf(expm1_32(math.inf(f32))));
expect(expm1_32(-math.inf(f32)) == -1.0);
expect(math.isNan(expm1_32(math.nan(f32))));
}
test "math.expm1_64.special" {
const epsilon = 0.000001;
assert(math.isPositiveInf(expm1_64(math.inf(f64))));
assert(expm1_64(-math.inf(f64)) == -1.0);
assert(math.isNan(expm1_64(math.nan(f64))));
expect(math.isPositiveInf(expm1_64(math.inf(f64))));
expect(expm1_64(-math.inf(f64)) == -1.0);
expect(math.isNan(expm1_64(math.nan(f64))));
}

View File

@ -5,7 +5,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn fabs(x: var) @typeOf(x) {
@ -37,40 +37,40 @@ fn fabs64(x: f64) f64 {
}
test "math.fabs" {
assert(fabs(f16(1.0)) == fabs16(1.0));
assert(fabs(f32(1.0)) == fabs32(1.0));
assert(fabs(f64(1.0)) == fabs64(1.0));
expect(fabs(f16(1.0)) == fabs16(1.0));
expect(fabs(f32(1.0)) == fabs32(1.0));
expect(fabs(f64(1.0)) == fabs64(1.0));
}
test "math.fabs16" {
assert(fabs16(1.0) == 1.0);
assert(fabs16(-1.0) == 1.0);
expect(fabs16(1.0) == 1.0);
expect(fabs16(-1.0) == 1.0);
}
test "math.fabs32" {
assert(fabs32(1.0) == 1.0);
assert(fabs32(-1.0) == 1.0);
expect(fabs32(1.0) == 1.0);
expect(fabs32(-1.0) == 1.0);
}
test "math.fabs64" {
assert(fabs64(1.0) == 1.0);
assert(fabs64(-1.0) == 1.0);
expect(fabs64(1.0) == 1.0);
expect(fabs64(-1.0) == 1.0);
}
test "math.fabs16.special" {
assert(math.isPositiveInf(fabs(math.inf(f16))));
assert(math.isPositiveInf(fabs(-math.inf(f16))));
assert(math.isNan(fabs(math.nan(f16))));
expect(math.isPositiveInf(fabs(math.inf(f16))));
expect(math.isPositiveInf(fabs(-math.inf(f16))));
expect(math.isNan(fabs(math.nan(f16))));
}
test "math.fabs32.special" {
assert(math.isPositiveInf(fabs(math.inf(f32))));
assert(math.isPositiveInf(fabs(-math.inf(f32))));
assert(math.isNan(fabs(math.nan(f32))));
expect(math.isPositiveInf(fabs(math.inf(f32))));
expect(math.isPositiveInf(fabs(-math.inf(f32))));
expect(math.isNan(fabs(math.nan(f32))));
}
test "math.fabs64.special" {
assert(math.isPositiveInf(fabs(math.inf(f64))));
assert(math.isPositiveInf(fabs(-math.inf(f64))));
assert(math.isNan(fabs(math.nan(f64))));
expect(math.isPositiveInf(fabs(math.inf(f64))));
expect(math.isPositiveInf(fabs(-math.inf(f64))));
expect(math.isNan(fabs(math.nan(f64))));
}

View File

@ -5,7 +5,7 @@
// - floor(nan) = nan
const builtin = @import("builtin");
const assert = std.debug.assert;
const expect = std.testing.expect;
const std = @import("../index.zig");
const math = std.math;
@ -117,49 +117,49 @@ fn floor64(x: f64) f64 {
}
test "math.floor" {
assert(floor(f16(1.3)) == floor16(1.3));
assert(floor(f32(1.3)) == floor32(1.3));
assert(floor(f64(1.3)) == floor64(1.3));
expect(floor(f16(1.3)) == floor16(1.3));
expect(floor(f32(1.3)) == floor32(1.3));
expect(floor(f64(1.3)) == floor64(1.3));
}
test "math.floor16" {
assert(floor16(1.3) == 1.0);
assert(floor16(-1.3) == -2.0);
assert(floor16(0.2) == 0.0);
expect(floor16(1.3) == 1.0);
expect(floor16(-1.3) == -2.0);
expect(floor16(0.2) == 0.0);
}
test "math.floor32" {
assert(floor32(1.3) == 1.0);
assert(floor32(-1.3) == -2.0);
assert(floor32(0.2) == 0.0);
expect(floor32(1.3) == 1.0);
expect(floor32(-1.3) == -2.0);
expect(floor32(0.2) == 0.0);
}
test "math.floor64" {
assert(floor64(1.3) == 1.0);
assert(floor64(-1.3) == -2.0);
assert(floor64(0.2) == 0.0);
expect(floor64(1.3) == 1.0);
expect(floor64(-1.3) == -2.0);
expect(floor64(0.2) == 0.0);
}
test "math.floor16.special" {
assert(floor16(0.0) == 0.0);
assert(floor16(-0.0) == -0.0);
assert(math.isPositiveInf(floor16(math.inf(f16))));
assert(math.isNegativeInf(floor16(-math.inf(f16))));
assert(math.isNan(floor16(math.nan(f16))));
expect(floor16(0.0) == 0.0);
expect(floor16(-0.0) == -0.0);
expect(math.isPositiveInf(floor16(math.inf(f16))));
expect(math.isNegativeInf(floor16(-math.inf(f16))));
expect(math.isNan(floor16(math.nan(f16))));
}
test "math.floor32.special" {
assert(floor32(0.0) == 0.0);
assert(floor32(-0.0) == -0.0);
assert(math.isPositiveInf(floor32(math.inf(f32))));
assert(math.isNegativeInf(floor32(-math.inf(f32))));
assert(math.isNan(floor32(math.nan(f32))));
expect(floor32(0.0) == 0.0);
expect(floor32(-0.0) == -0.0);
expect(math.isPositiveInf(floor32(math.inf(f32))));
expect(math.isNegativeInf(floor32(-math.inf(f32))));
expect(math.isNan(floor32(math.nan(f32))));
}
test "math.floor64.special" {
assert(floor64(0.0) == 0.0);
assert(floor64(-0.0) == -0.0);
assert(math.isPositiveInf(floor64(math.inf(f64))));
assert(math.isNegativeInf(floor64(-math.inf(f64))));
assert(math.isNan(floor64(math.nan(f64))));
expect(floor64(0.0) == 0.0);
expect(floor64(-0.0) == -0.0);
expect(math.isPositiveInf(floor64(math.inf(f64))));
expect(math.isNegativeInf(floor64(-math.inf(f64))));
expect(math.isNan(floor64(math.nan(f64))));
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn fma(comptime T: type, x: T, y: T, z: T) T {
return switch (T) {
@ -135,30 +135,30 @@ fn add_and_denorm(a: f64, b: f64, scale: i32) f64 {
}
test "math.fma" {
assert(fma(f32, 0.0, 1.0, 1.0) == fma32(0.0, 1.0, 1.0));
assert(fma(f64, 0.0, 1.0, 1.0) == fma64(0.0, 1.0, 1.0));
expect(fma(f32, 0.0, 1.0, 1.0) == fma32(0.0, 1.0, 1.0));
expect(fma(f64, 0.0, 1.0, 1.0) == fma64(0.0, 1.0, 1.0));
}
test "math.fma32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, fma32(0.0, 5.0, 9.124), 9.124, epsilon));
assert(math.approxEq(f32, fma32(0.2, 5.0, 9.124), 10.124, epsilon));
assert(math.approxEq(f32, fma32(0.8923, 5.0, 9.124), 13.5855, epsilon));
assert(math.approxEq(f32, fma32(1.5, 5.0, 9.124), 16.624, epsilon));
assert(math.approxEq(f32, fma32(37.45, 5.0, 9.124), 196.374004, epsilon));
assert(math.approxEq(f32, fma32(89.123, 5.0, 9.124), 454.739005, epsilon));
assert(math.approxEq(f32, fma32(123123.234375, 5.0, 9.124), 615625.295875, epsilon));
expect(math.approxEq(f32, fma32(0.0, 5.0, 9.124), 9.124, epsilon));
expect(math.approxEq(f32, fma32(0.2, 5.0, 9.124), 10.124, epsilon));
expect(math.approxEq(f32, fma32(0.8923, 5.0, 9.124), 13.5855, epsilon));
expect(math.approxEq(f32, fma32(1.5, 5.0, 9.124), 16.624, epsilon));
expect(math.approxEq(f32, fma32(37.45, 5.0, 9.124), 196.374004, epsilon));
expect(math.approxEq(f32, fma32(89.123, 5.0, 9.124), 454.739005, epsilon));
expect(math.approxEq(f32, fma32(123123.234375, 5.0, 9.124), 615625.295875, epsilon));
}
test "math.fma64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, fma64(0.0, 5.0, 9.124), 9.124, epsilon));
assert(math.approxEq(f64, fma64(0.2, 5.0, 9.124), 10.124, epsilon));
assert(math.approxEq(f64, fma64(0.8923, 5.0, 9.124), 13.5855, epsilon));
assert(math.approxEq(f64, fma64(1.5, 5.0, 9.124), 16.624, epsilon));
assert(math.approxEq(f64, fma64(37.45, 5.0, 9.124), 196.374, epsilon));
assert(math.approxEq(f64, fma64(89.123, 5.0, 9.124), 454.739, epsilon));
assert(math.approxEq(f64, fma64(123123.234375, 5.0, 9.124), 615625.295875, epsilon));
expect(math.approxEq(f64, fma64(0.0, 5.0, 9.124), 9.124, epsilon));
expect(math.approxEq(f64, fma64(0.2, 5.0, 9.124), 10.124, epsilon));
expect(math.approxEq(f64, fma64(0.8923, 5.0, 9.124), 13.5855, epsilon));
expect(math.approxEq(f64, fma64(1.5, 5.0, 9.124), 16.624, epsilon));
expect(math.approxEq(f64, fma64(37.45, 5.0, 9.124), 196.374, epsilon));
expect(math.approxEq(f64, fma64(89.123, 5.0, 9.124), 454.739, epsilon));
expect(math.approxEq(f64, fma64(123123.234375, 5.0, 9.124), 615625.295875, epsilon));
}

View File

@ -6,7 +6,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
fn frexp_result(comptime T: type) type {
return struct {
@ -103,11 +103,11 @@ fn frexp64(x: f64) frexp64_result {
test "math.frexp" {
const a = frexp(f32(1.3));
const b = frexp32(1.3);
assert(a.significand == b.significand and a.exponent == b.exponent);
expect(a.significand == b.significand and a.exponent == b.exponent);
const c = frexp(f64(1.3));
const d = frexp64(1.3);
assert(c.significand == d.significand and c.exponent == d.exponent);
expect(c.significand == d.significand and c.exponent == d.exponent);
}
test "math.frexp32" {
@ -115,10 +115,10 @@ test "math.frexp32" {
var r: frexp32_result = undefined;
r = frexp32(1.3);
assert(math.approxEq(f32, r.significand, 0.65, epsilon) and r.exponent == 1);
expect(math.approxEq(f32, r.significand, 0.65, epsilon) and r.exponent == 1);
r = frexp32(78.0234);
assert(math.approxEq(f32, r.significand, 0.609558, epsilon) and r.exponent == 7);
expect(math.approxEq(f32, r.significand, 0.609558, epsilon) and r.exponent == 7);
}
test "math.frexp64" {
@ -126,46 +126,46 @@ test "math.frexp64" {
var r: frexp64_result = undefined;
r = frexp64(1.3);
assert(math.approxEq(f64, r.significand, 0.65, epsilon) and r.exponent == 1);
expect(math.approxEq(f64, r.significand, 0.65, epsilon) and r.exponent == 1);
r = frexp64(78.0234);
assert(math.approxEq(f64, r.significand, 0.609558, epsilon) and r.exponent == 7);
expect(math.approxEq(f64, r.significand, 0.609558, epsilon) and r.exponent == 7);
}
test "math.frexp32.special" {
var r: frexp32_result = undefined;
r = frexp32(0.0);
assert(r.significand == 0.0 and r.exponent == 0);
expect(r.significand == 0.0 and r.exponent == 0);
r = frexp32(-0.0);
assert(r.significand == -0.0 and r.exponent == 0);
expect(r.significand == -0.0 and r.exponent == 0);
r = frexp32(math.inf(f32));
assert(math.isPositiveInf(r.significand) and r.exponent == 0);
expect(math.isPositiveInf(r.significand) and r.exponent == 0);
r = frexp32(-math.inf(f32));
assert(math.isNegativeInf(r.significand) and r.exponent == 0);
expect(math.isNegativeInf(r.significand) and r.exponent == 0);
r = frexp32(math.nan(f32));
assert(math.isNan(r.significand));
expect(math.isNan(r.significand));
}
test "math.frexp64.special" {
var r: frexp64_result = undefined;
r = frexp64(0.0);
assert(r.significand == 0.0 and r.exponent == 0);
expect(r.significand == 0.0 and r.exponent == 0);
r = frexp64(-0.0);
assert(r.significand == -0.0 and r.exponent == 0);
expect(r.significand == -0.0 and r.exponent == 0);
r = frexp64(math.inf(f64));
assert(math.isPositiveInf(r.significand) and r.exponent == 0);
expect(math.isPositiveInf(r.significand) and r.exponent == 0);
r = frexp64(-math.inf(f64));
assert(math.isNegativeInf(r.significand) and r.exponent == 0);
expect(math.isNegativeInf(r.significand) and r.exponent == 0);
r = frexp64(math.nan(f64));
assert(math.isNan(r.significand));
expect(math.isNan(r.significand));
}

View File

@ -7,7 +7,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn hypot(comptime T: type, x: T, y: T) T {
@ -115,48 +115,48 @@ fn hypot64(x: f64, y: f64) f64 {
}
test "math.hypot" {
assert(hypot(f32, 0.0, -1.2) == hypot32(0.0, -1.2));
assert(hypot(f64, 0.0, -1.2) == hypot64(0.0, -1.2));
expect(hypot(f32, 0.0, -1.2) == hypot32(0.0, -1.2));
expect(hypot(f64, 0.0, -1.2) == hypot64(0.0, -1.2));
}
test "math.hypot32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, hypot32(0.0, -1.2), 1.2, epsilon));
assert(math.approxEq(f32, hypot32(0.2, -0.34), 0.394462, epsilon));
assert(math.approxEq(f32, hypot32(0.8923, 2.636890), 2.783772, epsilon));
assert(math.approxEq(f32, hypot32(1.5, 5.25), 5.460083, epsilon));
assert(math.approxEq(f32, hypot32(37.45, 159.835), 164.163742, epsilon));
assert(math.approxEq(f32, hypot32(89.123, 382.028905), 392.286865, epsilon));
assert(math.approxEq(f32, hypot32(123123.234375, 529428.707813), 543556.875, epsilon));
expect(math.approxEq(f32, hypot32(0.0, -1.2), 1.2, epsilon));
expect(math.approxEq(f32, hypot32(0.2, -0.34), 0.394462, epsilon));
expect(math.approxEq(f32, hypot32(0.8923, 2.636890), 2.783772, epsilon));
expect(math.approxEq(f32, hypot32(1.5, 5.25), 5.460083, epsilon));
expect(math.approxEq(f32, hypot32(37.45, 159.835), 164.163742, epsilon));
expect(math.approxEq(f32, hypot32(89.123, 382.028905), 392.286865, epsilon));
expect(math.approxEq(f32, hypot32(123123.234375, 529428.707813), 543556.875, epsilon));
}
test "math.hypot64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, hypot64(0.0, -1.2), 1.2, epsilon));
assert(math.approxEq(f64, hypot64(0.2, -0.34), 0.394462, epsilon));
assert(math.approxEq(f64, hypot64(0.8923, 2.636890), 2.783772, epsilon));
assert(math.approxEq(f64, hypot64(1.5, 5.25), 5.460082, epsilon));
assert(math.approxEq(f64, hypot64(37.45, 159.835), 164.163728, epsilon));
assert(math.approxEq(f64, hypot64(89.123, 382.028905), 392.286876, epsilon));
assert(math.approxEq(f64, hypot64(123123.234375, 529428.707813), 543556.885247, epsilon));
expect(math.approxEq(f64, hypot64(0.0, -1.2), 1.2, epsilon));
expect(math.approxEq(f64, hypot64(0.2, -0.34), 0.394462, epsilon));
expect(math.approxEq(f64, hypot64(0.8923, 2.636890), 2.783772, epsilon));
expect(math.approxEq(f64, hypot64(1.5, 5.25), 5.460082, epsilon));
expect(math.approxEq(f64, hypot64(37.45, 159.835), 164.163728, epsilon));
expect(math.approxEq(f64, hypot64(89.123, 382.028905), 392.286876, epsilon));
expect(math.approxEq(f64, hypot64(123123.234375, 529428.707813), 543556.885247, epsilon));
}
test "math.hypot32.special" {
assert(math.isPositiveInf(hypot32(math.inf(f32), 0.0)));
assert(math.isPositiveInf(hypot32(-math.inf(f32), 0.0)));
assert(math.isPositiveInf(hypot32(0.0, math.inf(f32))));
assert(math.isPositiveInf(hypot32(0.0, -math.inf(f32))));
assert(math.isNan(hypot32(math.nan(f32), 0.0)));
assert(math.isNan(hypot32(0.0, math.nan(f32))));
expect(math.isPositiveInf(hypot32(math.inf(f32), 0.0)));
expect(math.isPositiveInf(hypot32(-math.inf(f32), 0.0)));
expect(math.isPositiveInf(hypot32(0.0, math.inf(f32))));
expect(math.isPositiveInf(hypot32(0.0, -math.inf(f32))));
expect(math.isNan(hypot32(math.nan(f32), 0.0)));
expect(math.isNan(hypot32(0.0, math.nan(f32))));
}
test "math.hypot64.special" {
assert(math.isPositiveInf(hypot64(math.inf(f64), 0.0)));
assert(math.isPositiveInf(hypot64(-math.inf(f64), 0.0)));
assert(math.isPositiveInf(hypot64(0.0, math.inf(f64))));
assert(math.isPositiveInf(hypot64(0.0, -math.inf(f64))));
assert(math.isNan(hypot64(math.nan(f64), 0.0)));
assert(math.isNan(hypot64(0.0, math.nan(f64))));
expect(math.isPositiveInf(hypot64(math.inf(f64), 0.0)));
expect(math.isPositiveInf(hypot64(-math.inf(f64), 0.0)));
expect(math.isPositiveInf(hypot64(0.0, math.inf(f64))));
expect(math.isPositiveInf(hypot64(0.0, -math.inf(f64))));
expect(math.isNan(hypot64(math.nan(f64), 0.0)));
expect(math.isNan(hypot64(0.0, math.nan(f64))));
}

View File

@ -6,7 +6,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
const minInt = std.math.minInt;
@ -95,38 +95,38 @@ fn ilogb64(x: f64) i32 {
}
test "math.ilogb" {
assert(ilogb(f32(0.2)) == ilogb32(0.2));
assert(ilogb(f64(0.2)) == ilogb64(0.2));
expect(ilogb(f32(0.2)) == ilogb32(0.2));
expect(ilogb(f64(0.2)) == ilogb64(0.2));
}
test "math.ilogb32" {
assert(ilogb32(0.0) == fp_ilogb0);
assert(ilogb32(0.5) == -1);
assert(ilogb32(0.8923) == -1);
assert(ilogb32(10.0) == 3);
assert(ilogb32(-123984) == 16);
assert(ilogb32(2398.23) == 11);
expect(ilogb32(0.0) == fp_ilogb0);
expect(ilogb32(0.5) == -1);
expect(ilogb32(0.8923) == -1);
expect(ilogb32(10.0) == 3);
expect(ilogb32(-123984) == 16);
expect(ilogb32(2398.23) == 11);
}
test "math.ilogb64" {
assert(ilogb64(0.0) == fp_ilogb0);
assert(ilogb64(0.5) == -1);
assert(ilogb64(0.8923) == -1);
assert(ilogb64(10.0) == 3);
assert(ilogb64(-123984) == 16);
assert(ilogb64(2398.23) == 11);
expect(ilogb64(0.0) == fp_ilogb0);
expect(ilogb64(0.5) == -1);
expect(ilogb64(0.8923) == -1);
expect(ilogb64(10.0) == 3);
expect(ilogb64(-123984) == 16);
expect(ilogb64(2398.23) == 11);
}
test "math.ilogb32.special" {
assert(ilogb32(math.inf(f32)) == maxInt(i32));
assert(ilogb32(-math.inf(f32)) == maxInt(i32));
assert(ilogb32(0.0) == minInt(i32));
assert(ilogb32(math.nan(f32)) == maxInt(i32));
expect(ilogb32(math.inf(f32)) == maxInt(i32));
expect(ilogb32(-math.inf(f32)) == maxInt(i32));
expect(ilogb32(0.0) == minInt(i32));
expect(ilogb32(math.nan(f32)) == maxInt(i32));
}
test "math.ilogb64.special" {
assert(ilogb64(math.inf(f64)) == maxInt(i32));
assert(ilogb64(-math.inf(f64)) == maxInt(i32));
assert(ilogb64(0.0) == minInt(i32));
assert(ilogb64(math.nan(f64)) == maxInt(i32));
expect(ilogb64(math.inf(f64)) == maxInt(i32));
expect(ilogb64(-math.inf(f64)) == maxInt(i32));
expect(ilogb64(0.0) == minInt(i32));
expect(ilogb64(math.nan(f64)) == maxInt(i32));
}

View File

@ -2,6 +2,7 @@ const builtin = @import("builtin");
const std = @import("../index.zig");
const TypeId = builtin.TypeId;
const assert = std.debug.assert;
const testing = std.testing;
pub const e = 2.71828182845904523536028747135266249775724709369995;
pub const pi = 3.14159265358979323846264338327950288419716939937510;
@ -240,7 +241,7 @@ pub fn min(x: var, y: var) @typeOf(x + y) {
}
test "math.min" {
assert(min(i32(-1), i32(2)) == -1);
testing.expect(min(i32(-1), i32(2)) == -1);
}
pub fn max(x: var, y: var) @typeOf(x + y) {
@ -248,7 +249,7 @@ pub fn max(x: var, y: var) @typeOf(x + y) {
}
test "math.max" {
assert(max(i32(-1), i32(2)) == 2);
testing.expect(max(i32(-1), i32(2)) == 2);
}
pub fn mul(comptime T: type, a: T, b: T) (error{Overflow}!T) {
@ -293,10 +294,10 @@ pub fn shl(comptime T: type, a: T, shift_amt: var) T {
}
test "math.shl" {
assert(shl(u8, 0b11111111, usize(3)) == 0b11111000);
assert(shl(u8, 0b11111111, usize(8)) == 0);
assert(shl(u8, 0b11111111, usize(9)) == 0);
assert(shl(u8, 0b11111111, isize(-2)) == 0b00111111);
testing.expect(shl(u8, 0b11111111, usize(3)) == 0b11111000);
testing.expect(shl(u8, 0b11111111, usize(8)) == 0);
testing.expect(shl(u8, 0b11111111, usize(9)) == 0);
testing.expect(shl(u8, 0b11111111, isize(-2)) == 0b00111111);
}
/// Shifts right. Overflowed bits are truncated.
@ -317,10 +318,10 @@ pub fn shr(comptime T: type, a: T, shift_amt: var) T {
}
test "math.shr" {
assert(shr(u8, 0b11111111, usize(3)) == 0b00011111);
assert(shr(u8, 0b11111111, usize(8)) == 0);
assert(shr(u8, 0b11111111, usize(9)) == 0);
assert(shr(u8, 0b11111111, isize(-2)) == 0b11111100);
testing.expect(shr(u8, 0b11111111, usize(3)) == 0b00011111);
testing.expect(shr(u8, 0b11111111, usize(8)) == 0);
testing.expect(shr(u8, 0b11111111, usize(9)) == 0);
testing.expect(shr(u8, 0b11111111, isize(-2)) == 0b11111100);
}
/// Rotates right. Only unsigned values can be rotated.
@ -335,11 +336,11 @@ pub fn rotr(comptime T: type, x: T, r: var) T {
}
test "math.rotr" {
assert(rotr(u8, 0b00000001, usize(0)) == 0b00000001);
assert(rotr(u8, 0b00000001, usize(9)) == 0b10000000);
assert(rotr(u8, 0b00000001, usize(8)) == 0b00000001);
assert(rotr(u8, 0b00000001, usize(4)) == 0b00010000);
assert(rotr(u8, 0b00000001, isize(-1)) == 0b00000010);
testing.expect(rotr(u8, 0b00000001, usize(0)) == 0b00000001);
testing.expect(rotr(u8, 0b00000001, usize(9)) == 0b10000000);
testing.expect(rotr(u8, 0b00000001, usize(8)) == 0b00000001);
testing.expect(rotr(u8, 0b00000001, usize(4)) == 0b00010000);
testing.expect(rotr(u8, 0b00000001, isize(-1)) == 0b00000010);
}
/// Rotates left. Only unsigned values can be rotated.
@ -354,11 +355,11 @@ pub fn rotl(comptime T: type, x: T, r: var) T {
}
test "math.rotl" {
assert(rotl(u8, 0b00000001, usize(0)) == 0b00000001);
assert(rotl(u8, 0b00000001, usize(9)) == 0b00000010);
assert(rotl(u8, 0b00000001, usize(8)) == 0b00000001);
assert(rotl(u8, 0b00000001, usize(4)) == 0b00010000);
assert(rotl(u8, 0b00000001, isize(-1)) == 0b10000000);
testing.expect(rotl(u8, 0b00000001, usize(0)) == 0b00000001);
testing.expect(rotl(u8, 0b00000001, usize(9)) == 0b00000010);
testing.expect(rotl(u8, 0b00000001, usize(8)) == 0b00000001);
testing.expect(rotl(u8, 0b00000001, usize(4)) == 0b00010000);
testing.expect(rotl(u8, 0b00000001, isize(-1)) == 0b10000000);
}
pub fn Log2Int(comptime T: type) type {
@ -389,50 +390,50 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t
}
test "math.IntFittingRange" {
assert(IntFittingRange(0, 0) == u0);
assert(IntFittingRange(0, 1) == u1);
assert(IntFittingRange(0, 2) == u2);
assert(IntFittingRange(0, 3) == u2);
assert(IntFittingRange(0, 4) == u3);
assert(IntFittingRange(0, 7) == u3);
assert(IntFittingRange(0, 8) == u4);
assert(IntFittingRange(0, 9) == u4);
assert(IntFittingRange(0, 15) == u4);
assert(IntFittingRange(0, 16) == u5);
assert(IntFittingRange(0, 17) == u5);
assert(IntFittingRange(0, 4095) == u12);
assert(IntFittingRange(2000, 4095) == u12);
assert(IntFittingRange(0, 4096) == u13);
assert(IntFittingRange(2000, 4096) == u13);
assert(IntFittingRange(0, 4097) == u13);
assert(IntFittingRange(2000, 4097) == u13);
assert(IntFittingRange(0, 123456789123456798123456789) == u87);
assert(IntFittingRange(0, 123456789123456798123456789123456789123456798123456789) == u177);
testing.expect(IntFittingRange(0, 0) == u0);
testing.expect(IntFittingRange(0, 1) == u1);
testing.expect(IntFittingRange(0, 2) == u2);
testing.expect(IntFittingRange(0, 3) == u2);
testing.expect(IntFittingRange(0, 4) == u3);
testing.expect(IntFittingRange(0, 7) == u3);
testing.expect(IntFittingRange(0, 8) == u4);
testing.expect(IntFittingRange(0, 9) == u4);
testing.expect(IntFittingRange(0, 15) == u4);
testing.expect(IntFittingRange(0, 16) == u5);
testing.expect(IntFittingRange(0, 17) == u5);
testing.expect(IntFittingRange(0, 4095) == u12);
testing.expect(IntFittingRange(2000, 4095) == u12);
testing.expect(IntFittingRange(0, 4096) == u13);
testing.expect(IntFittingRange(2000, 4096) == u13);
testing.expect(IntFittingRange(0, 4097) == u13);
testing.expect(IntFittingRange(2000, 4097) == u13);
testing.expect(IntFittingRange(0, 123456789123456798123456789) == u87);
testing.expect(IntFittingRange(0, 123456789123456798123456789123456789123456798123456789) == u177);
assert(IntFittingRange(-1, -1) == i1);
assert(IntFittingRange(-1, 0) == i1);
assert(IntFittingRange(-1, 1) == i2);
assert(IntFittingRange(-2, -2) == i2);
assert(IntFittingRange(-2, -1) == i2);
assert(IntFittingRange(-2, 0) == i2);
assert(IntFittingRange(-2, 1) == i2);
assert(IntFittingRange(-2, 2) == i3);
assert(IntFittingRange(-1, 2) == i3);
assert(IntFittingRange(-1, 3) == i3);
assert(IntFittingRange(-1, 4) == i4);
assert(IntFittingRange(-1, 7) == i4);
assert(IntFittingRange(-1, 8) == i5);
assert(IntFittingRange(-1, 9) == i5);
assert(IntFittingRange(-1, 15) == i5);
assert(IntFittingRange(-1, 16) == i6);
assert(IntFittingRange(-1, 17) == i6);
assert(IntFittingRange(-1, 4095) == i13);
assert(IntFittingRange(-4096, 4095) == i13);
assert(IntFittingRange(-1, 4096) == i14);
assert(IntFittingRange(-4097, 4095) == i14);
assert(IntFittingRange(-1, 4097) == i14);
assert(IntFittingRange(-1, 123456789123456798123456789) == i88);
assert(IntFittingRange(-1, 123456789123456798123456789123456789123456798123456789) == i178);
testing.expect(IntFittingRange(-1, -1) == i1);
testing.expect(IntFittingRange(-1, 0) == i1);
testing.expect(IntFittingRange(-1, 1) == i2);
testing.expect(IntFittingRange(-2, -2) == i2);
testing.expect(IntFittingRange(-2, -1) == i2);
testing.expect(IntFittingRange(-2, 0) == i2);
testing.expect(IntFittingRange(-2, 1) == i2);
testing.expect(IntFittingRange(-2, 2) == i3);
testing.expect(IntFittingRange(-1, 2) == i3);
testing.expect(IntFittingRange(-1, 3) == i3);
testing.expect(IntFittingRange(-1, 4) == i4);
testing.expect(IntFittingRange(-1, 7) == i4);
testing.expect(IntFittingRange(-1, 8) == i5);
testing.expect(IntFittingRange(-1, 9) == i5);
testing.expect(IntFittingRange(-1, 15) == i5);
testing.expect(IntFittingRange(-1, 16) == i6);
testing.expect(IntFittingRange(-1, 17) == i6);
testing.expect(IntFittingRange(-1, 4095) == i13);
testing.expect(IntFittingRange(-4096, 4095) == i13);
testing.expect(IntFittingRange(-1, 4096) == i14);
testing.expect(IntFittingRange(-4097, 4095) == i14);
testing.expect(IntFittingRange(-1, 4097) == i14);
testing.expect(IntFittingRange(-1, 123456789123456798123456789) == i88);
testing.expect(IntFittingRange(-1, 123456789123456798123456789123456789123456798123456789) == i178);
}
test "math overflow functions" {
@ -441,10 +442,10 @@ test "math overflow functions" {
}
fn testOverflow() void {
assert((mul(i32, 3, 4) catch unreachable) == 12);
assert((add(i32, 3, 4) catch unreachable) == 7);
assert((sub(i32, 3, 4) catch unreachable) == -1);
assert((shlExact(i32, 0b11, 4) catch unreachable) == 0b110000);
testing.expect((mul(i32, 3, 4) catch unreachable) == 12);
testing.expect((add(i32, 3, 4) catch unreachable) == 7);
testing.expect((sub(i32, 3, 4) catch unreachable) == -1);
testing.expect((shlExact(i32, 0b11, 4) catch unreachable) == 0b110000);
}
pub fn absInt(x: var) !@typeOf(x) {
@ -465,8 +466,8 @@ test "math.absInt" {
comptime testAbsInt();
}
fn testAbsInt() void {
assert((absInt(i32(-10)) catch unreachable) == 10);
assert((absInt(i32(10)) catch unreachable) == 10);
testing.expect((absInt(i32(-10)) catch unreachable) == 10);
testing.expect((absInt(i32(10)) catch unreachable) == 10);
}
pub const absFloat = @import("fabs.zig").fabs;
@ -483,13 +484,13 @@ test "math.divTrunc" {
comptime testDivTrunc();
}
fn testDivTrunc() void {
assert((divTrunc(i32, 5, 3) catch unreachable) == 1);
assert((divTrunc(i32, -5, 3) catch unreachable) == -1);
if (divTrunc(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
if (divTrunc(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow);
testing.expect((divTrunc(i32, 5, 3) catch unreachable) == 1);
testing.expect((divTrunc(i32, -5, 3) catch unreachable) == -1);
testing.expectError(error.DivisionByZero, divTrunc(i8, -5, 0));
testing.expectError(error.Overflow, divTrunc(i8, -128, -1));
assert((divTrunc(f32, 5.0, 3.0) catch unreachable) == 1.0);
assert((divTrunc(f32, -5.0, 3.0) catch unreachable) == -1.0);
testing.expect((divTrunc(f32, 5.0, 3.0) catch unreachable) == 1.0);
testing.expect((divTrunc(f32, -5.0, 3.0) catch unreachable) == -1.0);
}
pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
@ -504,13 +505,13 @@ test "math.divFloor" {
comptime testDivFloor();
}
fn testDivFloor() void {
assert((divFloor(i32, 5, 3) catch unreachable) == 1);
assert((divFloor(i32, -5, 3) catch unreachable) == -2);
if (divFloor(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
if (divFloor(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow);
testing.expect((divFloor(i32, 5, 3) catch unreachable) == 1);
testing.expect((divFloor(i32, -5, 3) catch unreachable) == -2);
testing.expectError(error.DivisionByZero, divFloor(i8, -5, 0));
testing.expectError(error.Overflow, divFloor(i8, -128, -1));
assert((divFloor(f32, 5.0, 3.0) catch unreachable) == 1.0);
assert((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0);
testing.expect((divFloor(f32, 5.0, 3.0) catch unreachable) == 1.0);
testing.expect((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0);
}
pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
@ -527,15 +528,15 @@ test "math.divExact" {
comptime testDivExact();
}
fn testDivExact() void {
assert((divExact(i32, 10, 5) catch unreachable) == 2);
assert((divExact(i32, -10, 5) catch unreachable) == -2);
if (divExact(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
if (divExact(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow);
if (divExact(i32, 5, 2)) |_| unreachable else |err| assert(err == error.UnexpectedRemainder);
testing.expect((divExact(i32, 10, 5) catch unreachable) == 2);
testing.expect((divExact(i32, -10, 5) catch unreachable) == -2);
testing.expectError(error.DivisionByZero, divExact(i8, -5, 0));
testing.expectError(error.Overflow, divExact(i8, -128, -1));
testing.expectError(error.UnexpectedRemainder, divExact(i32, 5, 2));
assert((divExact(f32, 10.0, 5.0) catch unreachable) == 2.0);
assert((divExact(f32, -10.0, 5.0) catch unreachable) == -2.0);
if (divExact(f32, 5.0, 2.0)) |_| unreachable else |err| assert(err == error.UnexpectedRemainder);
testing.expect((divExact(f32, 10.0, 5.0) catch unreachable) == 2.0);
testing.expect((divExact(f32, -10.0, 5.0) catch unreachable) == -2.0);
testing.expectError(error.UnexpectedRemainder, divExact(f32, 5.0, 2.0));
}
pub fn mod(comptime T: type, numerator: T, denominator: T) !T {
@ -550,15 +551,15 @@ test "math.mod" {
comptime testMod();
}
fn testMod() void {
assert((mod(i32, -5, 3) catch unreachable) == 1);
assert((mod(i32, 5, 3) catch unreachable) == 2);
if (mod(i32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator);
if (mod(i32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
testing.expect((mod(i32, -5, 3) catch unreachable) == 1);
testing.expect((mod(i32, 5, 3) catch unreachable) == 2);
testing.expectError(error.NegativeDenominator, mod(i32, 10, -1));
testing.expectError(error.DivisionByZero, mod(i32, 10, 0));
assert((mod(f32, -5, 3) catch unreachable) == 1);
assert((mod(f32, 5, 3) catch unreachable) == 2);
if (mod(f32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator);
if (mod(f32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
testing.expect((mod(f32, -5, 3) catch unreachable) == 1);
testing.expect((mod(f32, 5, 3) catch unreachable) == 2);
testing.expectError(error.NegativeDenominator, mod(f32, 10, -1));
testing.expectError(error.DivisionByZero, mod(f32, 10, 0));
}
pub fn rem(comptime T: type, numerator: T, denominator: T) !T {
@ -573,15 +574,15 @@ test "math.rem" {
comptime testRem();
}
fn testRem() void {
assert((rem(i32, -5, 3) catch unreachable) == -2);
assert((rem(i32, 5, 3) catch unreachable) == 2);
if (rem(i32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator);
if (rem(i32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
testing.expect((rem(i32, -5, 3) catch unreachable) == -2);
testing.expect((rem(i32, 5, 3) catch unreachable) == 2);
testing.expectError(error.NegativeDenominator, rem(i32, 10, -1));
testing.expectError(error.DivisionByZero, rem(i32, 10, 0));
assert((rem(f32, -5, 3) catch unreachable) == -2);
assert((rem(f32, 5, 3) catch unreachable) == 2);
if (rem(f32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator);
if (rem(f32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero);
testing.expect((rem(f32, -5, 3) catch unreachable) == -2);
testing.expect((rem(f32, 5, 3) catch unreachable) == 2);
testing.expectError(error.NegativeDenominator, rem(f32, 10, -1));
testing.expectError(error.DivisionByZero, rem(f32, 10, 0));
}
/// Returns the absolute value of the integer parameter.
@ -594,14 +595,14 @@ pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) {
}
test "math.absCast" {
assert(absCast(i32(-999)) == 999);
assert(@typeOf(absCast(i32(-999))) == u32);
testing.expect(absCast(i32(-999)) == 999);
testing.expect(@typeOf(absCast(i32(-999))) == u32);
assert(absCast(i32(999)) == 999);
assert(@typeOf(absCast(i32(999))) == u32);
testing.expect(absCast(i32(999)) == 999);
testing.expect(@typeOf(absCast(i32(999))) == u32);
assert(absCast(i32(minInt(i32))) == -minInt(i32));
assert(@typeOf(absCast(i32(minInt(i32)))) == u32);
testing.expect(absCast(i32(minInt(i32))) == -minInt(i32));
testing.expect(@typeOf(absCast(i32(minInt(i32)))) == u32);
}
/// Returns the negation of the integer parameter.
@ -618,13 +619,13 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {
}
test "math.negateCast" {
assert((negateCast(u32(999)) catch unreachable) == -999);
assert(@typeOf(negateCast(u32(999)) catch unreachable) == i32);
testing.expect((negateCast(u32(999)) catch unreachable) == -999);
testing.expect(@typeOf(negateCast(u32(999)) catch unreachable) == i32);
assert((negateCast(u32(-minInt(i32))) catch unreachable) == minInt(i32));
assert(@typeOf(negateCast(u32(-minInt(i32))) catch unreachable) == i32);
testing.expect((negateCast(u32(-minInt(i32))) catch unreachable) == minInt(i32));
testing.expect(@typeOf(negateCast(u32(-minInt(i32))) catch unreachable) == i32);
if (negateCast(u32(maxInt(i32) + 10))) |_| unreachable else |err| assert(err == error.Overflow);
testing.expectError(error.Overflow, negateCast(u32(maxInt(i32) + 10)));
}
/// Cast an integer to a different integer type. If the value doesn't fit,
@ -642,13 +643,13 @@ pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
}
test "math.cast" {
if (cast(u8, u32(300))) |_| @panic("fail") else |err| assert(err == error.Overflow);
if (cast(i8, i32(-200))) |_| @panic("fail") else |err| assert(err == error.Overflow);
if (cast(u8, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow);
if (cast(u64, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow);
testing.expectError(error.Overflow, cast(u8, u32(300)));
testing.expectError(error.Overflow, cast(i8, i32(-200)));
testing.expectError(error.Overflow, cast(u8, i8(-1)));
testing.expectError(error.Overflow, cast(u64, i8(-1)));
assert((try cast(u8, u32(255))) == u8(255));
assert(@typeOf(try cast(u8, u32(255))) == u8);
testing.expect((try cast(u8, u32(255))) == u8(255));
testing.expect(@typeOf(try cast(u8, u32(255))) == u8);
}
pub const AlignCastError = error{UnalignedMemory};
@ -692,25 +693,25 @@ pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) {
}
test "std.math.log2_int_ceil" {
assert(log2_int_ceil(u32, 1) == 0);
assert(log2_int_ceil(u32, 2) == 1);
assert(log2_int_ceil(u32, 3) == 2);
assert(log2_int_ceil(u32, 4) == 2);
assert(log2_int_ceil(u32, 5) == 3);
assert(log2_int_ceil(u32, 6) == 3);
assert(log2_int_ceil(u32, 7) == 3);
assert(log2_int_ceil(u32, 8) == 3);
assert(log2_int_ceil(u32, 9) == 4);
assert(log2_int_ceil(u32, 10) == 4);
testing.expect(log2_int_ceil(u32, 1) == 0);
testing.expect(log2_int_ceil(u32, 2) == 1);
testing.expect(log2_int_ceil(u32, 3) == 2);
testing.expect(log2_int_ceil(u32, 4) == 2);
testing.expect(log2_int_ceil(u32, 5) == 3);
testing.expect(log2_int_ceil(u32, 6) == 3);
testing.expect(log2_int_ceil(u32, 7) == 3);
testing.expect(log2_int_ceil(u32, 8) == 3);
testing.expect(log2_int_ceil(u32, 9) == 4);
testing.expect(log2_int_ceil(u32, 10) == 4);
}
fn testFloorPowerOfTwo() void {
assert(floorPowerOfTwo(u32, 63) == 32);
assert(floorPowerOfTwo(u32, 64) == 64);
assert(floorPowerOfTwo(u32, 65) == 64);
assert(floorPowerOfTwo(u4, 7) == 4);
assert(floorPowerOfTwo(u4, 8) == 8);
assert(floorPowerOfTwo(u4, 9) == 8);
testing.expect(floorPowerOfTwo(u32, 63) == 32);
testing.expect(floorPowerOfTwo(u32, 64) == 64);
testing.expect(floorPowerOfTwo(u32, 65) == 64);
testing.expect(floorPowerOfTwo(u4, 7) == 4);
testing.expect(floorPowerOfTwo(u4, 8) == 8);
testing.expect(floorPowerOfTwo(u4, 9) == 8);
}
pub fn lossyCast(comptime T: type, value: var) T {
@ -726,7 +727,7 @@ pub fn lossyCast(comptime T: type, value: var) T {
test "math.f64_min" {
const f64_min_u64 = 0x0010000000000000;
const fmin: f64 = f64_min;
assert(@bitCast(u64, fmin) == f64_min_u64);
testing.expect(@bitCast(u64, fmin) == f64_min_u64);
}
pub fn maxInt(comptime T: type) comptime_int {
@ -745,36 +746,36 @@ pub fn minInt(comptime T: type) comptime_int {
}
test "minInt and maxInt" {
assert(maxInt(u0) == 0);
assert(maxInt(u1) == 1);
assert(maxInt(u8) == 255);
assert(maxInt(u16) == 65535);
assert(maxInt(u32) == 4294967295);
assert(maxInt(u64) == 18446744073709551615);
testing.expect(maxInt(u0) == 0);
testing.expect(maxInt(u1) == 1);
testing.expect(maxInt(u8) == 255);
testing.expect(maxInt(u16) == 65535);
testing.expect(maxInt(u32) == 4294967295);
testing.expect(maxInt(u64) == 18446744073709551615);
assert(maxInt(i0) == 0);
assert(maxInt(i1) == 0);
assert(maxInt(i8) == 127);
assert(maxInt(i16) == 32767);
assert(maxInt(i32) == 2147483647);
assert(maxInt(i63) == 4611686018427387903);
assert(maxInt(i64) == 9223372036854775807);
testing.expect(maxInt(i0) == 0);
testing.expect(maxInt(i1) == 0);
testing.expect(maxInt(i8) == 127);
testing.expect(maxInt(i16) == 32767);
testing.expect(maxInt(i32) == 2147483647);
testing.expect(maxInt(i63) == 4611686018427387903);
testing.expect(maxInt(i64) == 9223372036854775807);
assert(minInt(u0) == 0);
assert(minInt(u1) == 0);
assert(minInt(u8) == 0);
assert(minInt(u16) == 0);
assert(minInt(u32) == 0);
assert(minInt(u63) == 0);
assert(minInt(u64) == 0);
testing.expect(minInt(u0) == 0);
testing.expect(minInt(u1) == 0);
testing.expect(minInt(u8) == 0);
testing.expect(minInt(u16) == 0);
testing.expect(minInt(u32) == 0);
testing.expect(minInt(u63) == 0);
testing.expect(minInt(u64) == 0);
assert(minInt(i0) == 0);
assert(minInt(i1) == -1);
assert(minInt(i8) == -128);
assert(minInt(i16) == -32768);
assert(minInt(i32) == -2147483648);
assert(minInt(i63) == -4611686018427387904);
assert(minInt(i64) == -9223372036854775808);
testing.expect(minInt(i0) == 0);
testing.expect(minInt(i1) == -1);
testing.expect(minInt(i8) == -128);
testing.expect(minInt(i16) == -32768);
testing.expect(minInt(i32) == -2147483648);
testing.expect(minInt(i63) == -4611686018427387904);
testing.expect(minInt(i64) == -9223372036854775808);
}
test "max value type" {
@ -782,5 +783,5 @@ test "max value type" {
// u32 would not work. But since the value is a number literal,
// it works fine.
const x: u32 = maxInt(i32);
assert(x == 2147483647);
testing.expect(x == 2147483647);
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn isFinite(x: var) bool {
@ -25,16 +25,16 @@ pub fn isFinite(x: var) bool {
}
test "math.isFinite" {
assert(isFinite(f16(0.0)));
assert(isFinite(f16(-0.0)));
assert(isFinite(f32(0.0)));
assert(isFinite(f32(-0.0)));
assert(isFinite(f64(0.0)));
assert(isFinite(f64(-0.0)));
assert(!isFinite(math.inf(f16)));
assert(!isFinite(-math.inf(f16)));
assert(!isFinite(math.inf(f32)));
assert(!isFinite(-math.inf(f32)));
assert(!isFinite(math.inf(f64)));
assert(!isFinite(-math.inf(f64)));
expect(isFinite(f16(0.0)));
expect(isFinite(f16(-0.0)));
expect(isFinite(f32(0.0)));
expect(isFinite(f32(-0.0)));
expect(isFinite(f64(0.0)));
expect(isFinite(f64(-0.0)));
expect(!isFinite(math.inf(f16)));
expect(!isFinite(-math.inf(f16)));
expect(!isFinite(math.inf(f32)));
expect(!isFinite(-math.inf(f32)));
expect(!isFinite(math.inf(f64)));
expect(!isFinite(-math.inf(f64)));
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn isInf(x: var) bool {
@ -61,46 +61,46 @@ pub fn isNegativeInf(x: var) bool {
}
test "math.isInf" {
assert(!isInf(f16(0.0)));
assert(!isInf(f16(-0.0)));
assert(!isInf(f32(0.0)));
assert(!isInf(f32(-0.0)));
assert(!isInf(f64(0.0)));
assert(!isInf(f64(-0.0)));
assert(isInf(math.inf(f16)));
assert(isInf(-math.inf(f16)));
assert(isInf(math.inf(f32)));
assert(isInf(-math.inf(f32)));
assert(isInf(math.inf(f64)));
assert(isInf(-math.inf(f64)));
expect(!isInf(f16(0.0)));
expect(!isInf(f16(-0.0)));
expect(!isInf(f32(0.0)));
expect(!isInf(f32(-0.0)));
expect(!isInf(f64(0.0)));
expect(!isInf(f64(-0.0)));
expect(isInf(math.inf(f16)));
expect(isInf(-math.inf(f16)));
expect(isInf(math.inf(f32)));
expect(isInf(-math.inf(f32)));
expect(isInf(math.inf(f64)));
expect(isInf(-math.inf(f64)));
}
test "math.isPositiveInf" {
assert(!isPositiveInf(f16(0.0)));
assert(!isPositiveInf(f16(-0.0)));
assert(!isPositiveInf(f32(0.0)));
assert(!isPositiveInf(f32(-0.0)));
assert(!isPositiveInf(f64(0.0)));
assert(!isPositiveInf(f64(-0.0)));
assert(isPositiveInf(math.inf(f16)));
assert(!isPositiveInf(-math.inf(f16)));
assert(isPositiveInf(math.inf(f32)));
assert(!isPositiveInf(-math.inf(f32)));
assert(isPositiveInf(math.inf(f64)));
assert(!isPositiveInf(-math.inf(f64)));
expect(!isPositiveInf(f16(0.0)));
expect(!isPositiveInf(f16(-0.0)));
expect(!isPositiveInf(f32(0.0)));
expect(!isPositiveInf(f32(-0.0)));
expect(!isPositiveInf(f64(0.0)));
expect(!isPositiveInf(f64(-0.0)));
expect(isPositiveInf(math.inf(f16)));
expect(!isPositiveInf(-math.inf(f16)));
expect(isPositiveInf(math.inf(f32)));
expect(!isPositiveInf(-math.inf(f32)));
expect(isPositiveInf(math.inf(f64)));
expect(!isPositiveInf(-math.inf(f64)));
}
test "math.isNegativeInf" {
assert(!isNegativeInf(f16(0.0)));
assert(!isNegativeInf(f16(-0.0)));
assert(!isNegativeInf(f32(0.0)));
assert(!isNegativeInf(f32(-0.0)));
assert(!isNegativeInf(f64(0.0)));
assert(!isNegativeInf(f64(-0.0)));
assert(!isNegativeInf(math.inf(f16)));
assert(isNegativeInf(-math.inf(f16)));
assert(!isNegativeInf(math.inf(f32)));
assert(isNegativeInf(-math.inf(f32)));
assert(!isNegativeInf(math.inf(f64)));
assert(isNegativeInf(-math.inf(f64)));
expect(!isNegativeInf(f16(0.0)));
expect(!isNegativeInf(f16(-0.0)));
expect(!isNegativeInf(f32(0.0)));
expect(!isNegativeInf(f32(-0.0)));
expect(!isNegativeInf(f64(0.0)));
expect(!isNegativeInf(f64(-0.0)));
expect(!isNegativeInf(math.inf(f16)));
expect(isNegativeInf(-math.inf(f16)));
expect(!isNegativeInf(math.inf(f32)));
expect(isNegativeInf(-math.inf(f32)));
expect(!isNegativeInf(math.inf(f64)));
expect(isNegativeInf(-math.inf(f64)));
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn isNan(x: var) bool {
@ -31,10 +31,10 @@ pub fn isSignalNan(x: var) bool {
}
test "math.isNan" {
assert(isNan(math.nan(f16)));
assert(isNan(math.nan(f32)));
assert(isNan(math.nan(f64)));
assert(!isNan(f16(1.0)));
assert(!isNan(f32(1.0)));
assert(!isNan(f64(1.0)));
expect(isNan(math.nan(f16)));
expect(isNan(math.nan(f32)));
expect(isNan(math.nan(f64)));
expect(!isNan(f16(1.0)));
expect(!isNan(f32(1.0)));
expect(!isNan(f64(1.0)));
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
pub fn isNormal(x: var) bool {
@ -25,13 +25,13 @@ pub fn isNormal(x: var) bool {
}
test "math.isNormal" {
assert(!isNormal(math.nan(f16)));
assert(!isNormal(math.nan(f32)));
assert(!isNormal(math.nan(f64)));
assert(!isNormal(f16(0)));
assert(!isNormal(f32(0)));
assert(!isNormal(f64(0)));
assert(isNormal(f16(1.0)));
assert(isNormal(f32(1.0)));
assert(isNormal(f64(1.0)));
expect(!isNormal(math.nan(f16)));
expect(!isNormal(math.nan(f32)));
expect(!isNormal(math.nan(f64)));
expect(!isNormal(f16(0)));
expect(!isNormal(f32(0)));
expect(!isNormal(f64(0)));
expect(isNormal(f16(1.0)));
expect(isNormal(f32(1.0)));
expect(isNormal(f64(1.0)));
}

View File

@ -7,7 +7,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const builtin = @import("builtin");
const TypeId = builtin.TypeId;
@ -143,42 +143,42 @@ pub fn ln_64(x_: f64) f64 {
}
test "math.ln" {
assert(ln(f32(0.2)) == ln_32(0.2));
assert(ln(f64(0.2)) == ln_64(0.2));
expect(ln(f32(0.2)) == ln_32(0.2));
expect(ln(f64(0.2)) == ln_64(0.2));
}
test "math.ln32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, ln_32(0.2), -1.609438, epsilon));
assert(math.approxEq(f32, ln_32(0.8923), -0.113953, epsilon));
assert(math.approxEq(f32, ln_32(1.5), 0.405465, epsilon));
assert(math.approxEq(f32, ln_32(37.45), 3.623007, epsilon));
assert(math.approxEq(f32, ln_32(89.123), 4.490017, epsilon));
assert(math.approxEq(f32, ln_32(123123.234375), 11.720941, epsilon));
expect(math.approxEq(f32, ln_32(0.2), -1.609438, epsilon));
expect(math.approxEq(f32, ln_32(0.8923), -0.113953, epsilon));
expect(math.approxEq(f32, ln_32(1.5), 0.405465, epsilon));
expect(math.approxEq(f32, ln_32(37.45), 3.623007, epsilon));
expect(math.approxEq(f32, ln_32(89.123), 4.490017, epsilon));
expect(math.approxEq(f32, ln_32(123123.234375), 11.720941, epsilon));
}
test "math.ln64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, ln_64(0.2), -1.609438, epsilon));
assert(math.approxEq(f64, ln_64(0.8923), -0.113953, epsilon));
assert(math.approxEq(f64, ln_64(1.5), 0.405465, epsilon));
assert(math.approxEq(f64, ln_64(37.45), 3.623007, epsilon));
assert(math.approxEq(f64, ln_64(89.123), 4.490017, epsilon));
assert(math.approxEq(f64, ln_64(123123.234375), 11.720941, epsilon));
expect(math.approxEq(f64, ln_64(0.2), -1.609438, epsilon));
expect(math.approxEq(f64, ln_64(0.8923), -0.113953, epsilon));
expect(math.approxEq(f64, ln_64(1.5), 0.405465, epsilon));
expect(math.approxEq(f64, ln_64(37.45), 3.623007, epsilon));
expect(math.approxEq(f64, ln_64(89.123), 4.490017, epsilon));
expect(math.approxEq(f64, ln_64(123123.234375), 11.720941, epsilon));
}
test "math.ln32.special" {
assert(math.isPositiveInf(ln_32(math.inf(f32))));
assert(math.isNegativeInf(ln_32(0.0)));
assert(math.isNan(ln_32(-1.0)));
assert(math.isNan(ln_32(math.nan(f32))));
expect(math.isPositiveInf(ln_32(math.inf(f32))));
expect(math.isNegativeInf(ln_32(0.0)));
expect(math.isNan(ln_32(-1.0)));
expect(math.isNan(ln_32(math.nan(f32))));
}
test "math.ln64.special" {
assert(math.isPositiveInf(ln_64(math.inf(f64))));
assert(math.isNegativeInf(ln_64(0.0)));
assert(math.isNan(ln_64(-1.0)));
assert(math.isNan(ln_64(math.nan(f64))));
expect(math.isPositiveInf(ln_64(math.inf(f64))));
expect(math.isNegativeInf(ln_64(0.0)));
expect(math.isNan(ln_64(-1.0)));
expect(math.isNan(ln_64(math.nan(f64))));
}

View File

@ -2,7 +2,7 @@ const std = @import("../index.zig");
const math = std.math;
const builtin = @import("builtin");
const TypeId = builtin.TypeId;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn log(comptime T: type, base: T, x: T) T {
if (base == 2) {
@ -41,25 +41,25 @@ pub fn log(comptime T: type, base: T, x: T) T {
}
test "math.log integer" {
assert(log(u8, 2, 0x1) == 0);
assert(log(u8, 2, 0x2) == 1);
assert(log(i16, 2, 0x72) == 6);
assert(log(u32, 2, 0xFFFFFF) == 23);
assert(log(u64, 2, 0x7FF0123456789ABC) == 62);
expect(log(u8, 2, 0x1) == 0);
expect(log(u8, 2, 0x2) == 1);
expect(log(i16, 2, 0x72) == 6);
expect(log(u32, 2, 0xFFFFFF) == 23);
expect(log(u64, 2, 0x7FF0123456789ABC) == 62);
}
test "math.log float" {
const epsilon = 0.000001;
assert(math.approxEq(f32, log(f32, 6, 0.23947), -0.797723, epsilon));
assert(math.approxEq(f32, log(f32, 89, 0.23947), -0.318432, epsilon));
assert(math.approxEq(f64, log(f64, 123897, 12389216414), 1.981724596, epsilon));
expect(math.approxEq(f32, log(f32, 6, 0.23947), -0.797723, epsilon));
expect(math.approxEq(f32, log(f32, 89, 0.23947), -0.318432, epsilon));
expect(math.approxEq(f64, log(f64, 123897, 12389216414), 1.981724596, epsilon));
}
test "math.log float_special" {
assert(log(f32, 2, 0.2301974) == math.log2(f32(0.2301974)));
assert(log(f32, 10, 0.2301974) == math.log10(f32(0.2301974)));
expect(log(f32, 2, 0.2301974) == math.log2(f32(0.2301974)));
expect(log(f32, 10, 0.2301974) == math.log10(f32(0.2301974)));
assert(log(f64, 2, 213.23019799993) == math.log2(f64(213.23019799993)));
assert(log(f64, 10, 213.23019799993) == math.log10(f64(213.23019799993)));
expect(log(f64, 2, 213.23019799993) == math.log2(f64(213.23019799993)));
expect(log(f64, 10, 213.23019799993) == math.log10(f64(213.23019799993)));
}

View File

@ -7,7 +7,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const testing = std.testing;
const builtin = @import("builtin");
const TypeId = builtin.TypeId;
const maxInt = std.math.maxInt;
@ -171,42 +171,42 @@ pub fn log10_64(x_: f64) f64 {
}
test "math.log10" {
assert(log10(f32(0.2)) == log10_32(0.2));
assert(log10(f64(0.2)) == log10_64(0.2));
testing.expect(log10(f32(0.2)) == log10_32(0.2));
testing.expect(log10(f64(0.2)) == log10_64(0.2));
}
test "math.log10_32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, log10_32(0.2), -0.698970, epsilon));
assert(math.approxEq(f32, log10_32(0.8923), -0.049489, epsilon));
assert(math.approxEq(f32, log10_32(1.5), 0.176091, epsilon));
assert(math.approxEq(f32, log10_32(37.45), 1.573452, epsilon));
assert(math.approxEq(f32, log10_32(89.123), 1.94999, epsilon));
assert(math.approxEq(f32, log10_32(123123.234375), 5.09034, epsilon));
testing.expect(math.approxEq(f32, log10_32(0.2), -0.698970, epsilon));
testing.expect(math.approxEq(f32, log10_32(0.8923), -0.049489, epsilon));
testing.expect(math.approxEq(f32, log10_32(1.5), 0.176091, epsilon));
testing.expect(math.approxEq(f32, log10_32(37.45), 1.573452, epsilon));
testing.expect(math.approxEq(f32, log10_32(89.123), 1.94999, epsilon));
testing.expect(math.approxEq(f32, log10_32(123123.234375), 5.09034, epsilon));
}
test "math.log10_64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, log10_64(0.2), -0.698970, epsilon));
assert(math.approxEq(f64, log10_64(0.8923), -0.049489, epsilon));
assert(math.approxEq(f64, log10_64(1.5), 0.176091, epsilon));
assert(math.approxEq(f64, log10_64(37.45), 1.573452, epsilon));
assert(math.approxEq(f64, log10_64(89.123), 1.94999, epsilon));
assert(math.approxEq(f64, log10_64(123123.234375), 5.09034, epsilon));
testing.expect(math.approxEq(f64, log10_64(0.2), -0.698970, epsilon));
testing.expect(math.approxEq(f64, log10_64(0.8923), -0.049489, epsilon));
testing.expect(math.approxEq(f64, log10_64(1.5), 0.176091, epsilon));
testing.expect(math.approxEq(f64, log10_64(37.45), 1.573452, epsilon));
testing.expect(math.approxEq(f64, log10_64(89.123), 1.94999, epsilon));
testing.expect(math.approxEq(f64, log10_64(123123.234375), 5.09034, epsilon));
}
test "math.log10_32.special" {
assert(math.isPositiveInf(log10_32(math.inf(f32))));
assert(math.isNegativeInf(log10_32(0.0)));
assert(math.isNan(log10_32(-1.0)));
assert(math.isNan(log10_32(math.nan(f32))));
testing.expect(math.isPositiveInf(log10_32(math.inf(f32))));
testing.expect(math.isNegativeInf(log10_32(0.0)));
testing.expect(math.isNan(log10_32(-1.0)));
testing.expect(math.isNan(log10_32(math.nan(f32))));
}
test "math.log10_64.special" {
assert(math.isPositiveInf(log10_64(math.inf(f64))));
assert(math.isNegativeInf(log10_64(0.0)));
assert(math.isNan(log10_64(-1.0)));
assert(math.isNan(log10_64(math.nan(f64))));
testing.expect(math.isPositiveInf(log10_64(math.inf(f64))));
testing.expect(math.isNegativeInf(log10_64(0.0)));
testing.expect(math.isNan(log10_64(-1.0)));
testing.expect(math.isNan(log10_64(math.nan(f64))));
}

View File

@ -9,7 +9,7 @@
const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn log1p(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -177,48 +177,48 @@ fn log1p_64(x: f64) f64 {
}
test "math.log1p" {
assert(log1p(f32(0.0)) == log1p_32(0.0));
assert(log1p(f64(0.0)) == log1p_64(0.0));
expect(log1p(f32(0.0)) == log1p_32(0.0));
expect(log1p(f64(0.0)) == log1p_64(0.0));
}
test "math.log1p_32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, log1p_32(0.0), 0.0, epsilon));
assert(math.approxEq(f32, log1p_32(0.2), 0.182322, epsilon));
assert(math.approxEq(f32, log1p_32(0.8923), 0.637793, epsilon));
assert(math.approxEq(f32, log1p_32(1.5), 0.916291, epsilon));
assert(math.approxEq(f32, log1p_32(37.45), 3.649359, epsilon));
assert(math.approxEq(f32, log1p_32(89.123), 4.501175, epsilon));
assert(math.approxEq(f32, log1p_32(123123.234375), 11.720949, epsilon));
expect(math.approxEq(f32, log1p_32(0.0), 0.0, epsilon));
expect(math.approxEq(f32, log1p_32(0.2), 0.182322, epsilon));
expect(math.approxEq(f32, log1p_32(0.8923), 0.637793, epsilon));
expect(math.approxEq(f32, log1p_32(1.5), 0.916291, epsilon));
expect(math.approxEq(f32, log1p_32(37.45), 3.649359, epsilon));
expect(math.approxEq(f32, log1p_32(89.123), 4.501175, epsilon));
expect(math.approxEq(f32, log1p_32(123123.234375), 11.720949, epsilon));
}
test "math.log1p_64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, log1p_64(0.0), 0.0, epsilon));
assert(math.approxEq(f64, log1p_64(0.2), 0.182322, epsilon));
assert(math.approxEq(f64, log1p_64(0.8923), 0.637793, epsilon));
assert(math.approxEq(f64, log1p_64(1.5), 0.916291, epsilon));
assert(math.approxEq(f64, log1p_64(37.45), 3.649359, epsilon));
assert(math.approxEq(f64, log1p_64(89.123), 4.501175, epsilon));
assert(math.approxEq(f64, log1p_64(123123.234375), 11.720949, epsilon));
expect(math.approxEq(f64, log1p_64(0.0), 0.0, epsilon));
expect(math.approxEq(f64, log1p_64(0.2), 0.182322, epsilon));
expect(math.approxEq(f64, log1p_64(0.8923), 0.637793, epsilon));
expect(math.approxEq(f64, log1p_64(1.5), 0.916291, epsilon));
expect(math.approxEq(f64, log1p_64(37.45), 3.649359, epsilon));
expect(math.approxEq(f64, log1p_64(89.123), 4.501175, epsilon));
expect(math.approxEq(f64, log1p_64(123123.234375), 11.720949, epsilon));
}
test "math.log1p_32.special" {
assert(math.isPositiveInf(log1p_32(math.inf(f32))));
assert(log1p_32(0.0) == 0.0);
assert(log1p_32(-0.0) == -0.0);
assert(math.isNegativeInf(log1p_32(-1.0)));
assert(math.isNan(log1p_32(-2.0)));
assert(math.isNan(log1p_32(math.nan(f32))));
expect(math.isPositiveInf(log1p_32(math.inf(f32))));
expect(log1p_32(0.0) == 0.0);
expect(log1p_32(-0.0) == -0.0);
expect(math.isNegativeInf(log1p_32(-1.0)));
expect(math.isNan(log1p_32(-2.0)));
expect(math.isNan(log1p_32(math.nan(f32))));
}
test "math.log1p_64.special" {
assert(math.isPositiveInf(log1p_64(math.inf(f64))));
assert(log1p_64(0.0) == 0.0);
assert(log1p_64(-0.0) == -0.0);
assert(math.isNegativeInf(log1p_64(-1.0)));
assert(math.isNan(log1p_64(-2.0)));
assert(math.isNan(log1p_64(math.nan(f64))));
expect(math.isPositiveInf(log1p_64(math.inf(f64))));
expect(log1p_64(0.0) == 0.0);
expect(log1p_64(-0.0) == -0.0);
expect(math.isNegativeInf(log1p_64(-1.0)));
expect(math.isNan(log1p_64(-2.0)));
expect(math.isNan(log1p_64(math.nan(f64))));
}

View File

@ -7,7 +7,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const builtin = @import("builtin");
const TypeId = builtin.TypeId;
const maxInt = std.math.maxInt;
@ -169,40 +169,40 @@ pub fn log2_64(x_: f64) f64 {
}
test "math.log2" {
assert(log2(f32(0.2)) == log2_32(0.2));
assert(log2(f64(0.2)) == log2_64(0.2));
expect(log2(f32(0.2)) == log2_32(0.2));
expect(log2(f64(0.2)) == log2_64(0.2));
}
test "math.log2_32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, log2_32(0.2), -2.321928, epsilon));
assert(math.approxEq(f32, log2_32(0.8923), -0.164399, epsilon));
assert(math.approxEq(f32, log2_32(1.5), 0.584962, epsilon));
assert(math.approxEq(f32, log2_32(37.45), 5.226894, epsilon));
assert(math.approxEq(f32, log2_32(123123.234375), 16.909744, epsilon));
expect(math.approxEq(f32, log2_32(0.2), -2.321928, epsilon));
expect(math.approxEq(f32, log2_32(0.8923), -0.164399, epsilon));
expect(math.approxEq(f32, log2_32(1.5), 0.584962, epsilon));
expect(math.approxEq(f32, log2_32(37.45), 5.226894, epsilon));
expect(math.approxEq(f32, log2_32(123123.234375), 16.909744, epsilon));
}
test "math.log2_64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, log2_64(0.2), -2.321928, epsilon));
assert(math.approxEq(f64, log2_64(0.8923), -0.164399, epsilon));
assert(math.approxEq(f64, log2_64(1.5), 0.584962, epsilon));
assert(math.approxEq(f64, log2_64(37.45), 5.226894, epsilon));
assert(math.approxEq(f64, log2_64(123123.234375), 16.909744, epsilon));
expect(math.approxEq(f64, log2_64(0.2), -2.321928, epsilon));
expect(math.approxEq(f64, log2_64(0.8923), -0.164399, epsilon));
expect(math.approxEq(f64, log2_64(1.5), 0.584962, epsilon));
expect(math.approxEq(f64, log2_64(37.45), 5.226894, epsilon));
expect(math.approxEq(f64, log2_64(123123.234375), 16.909744, epsilon));
}
test "math.log2_32.special" {
assert(math.isPositiveInf(log2_32(math.inf(f32))));
assert(math.isNegativeInf(log2_32(0.0)));
assert(math.isNan(log2_32(-1.0)));
assert(math.isNan(log2_32(math.nan(f32))));
expect(math.isPositiveInf(log2_32(math.inf(f32))));
expect(math.isNegativeInf(log2_32(0.0)));
expect(math.isNan(log2_32(-1.0)));
expect(math.isNan(log2_32(math.nan(f32))));
}
test "math.log2_64.special" {
assert(math.isPositiveInf(log2_64(math.inf(f64))));
assert(math.isNegativeInf(log2_64(0.0)));
assert(math.isNan(log2_64(-1.0)));
assert(math.isNan(log2_64(math.nan(f64))));
expect(math.isPositiveInf(log2_64(math.inf(f64))));
expect(math.isNegativeInf(log2_64(0.0)));
expect(math.isNan(log2_64(-1.0)));
expect(math.isNan(log2_64(math.nan(f64))));
}

View File

@ -5,7 +5,7 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
fn modf_result(comptime T: type) type {
@ -119,11 +119,11 @@ test "math.modf" {
const a = modf(f32(1.0));
const b = modf32(1.0);
// NOTE: No struct comparison on generic return type function? non-named, makes sense, but still.
assert(a.ipart == b.ipart and a.fpart == b.fpart);
expect(a.ipart == b.ipart and a.fpart == b.fpart);
const c = modf(f64(1.0));
const d = modf64(1.0);
assert(a.ipart == b.ipart and a.fpart == b.fpart);
expect(a.ipart == b.ipart and a.fpart == b.fpart);
}
test "math.modf32" {
@ -131,24 +131,24 @@ test "math.modf32" {
var r: modf32_result = undefined;
r = modf32(1.0);
assert(math.approxEq(f32, r.ipart, 1.0, epsilon));
assert(math.approxEq(f32, r.fpart, 0.0, epsilon));
expect(math.approxEq(f32, r.ipart, 1.0, epsilon));
expect(math.approxEq(f32, r.fpart, 0.0, epsilon));
r = modf32(2.545);
assert(math.approxEq(f32, r.ipart, 2.0, epsilon));
assert(math.approxEq(f32, r.fpart, 0.545, epsilon));
expect(math.approxEq(f32, r.ipart, 2.0, epsilon));
expect(math.approxEq(f32, r.fpart, 0.545, epsilon));
r = modf32(3.978123);
assert(math.approxEq(f32, r.ipart, 3.0, epsilon));
assert(math.approxEq(f32, r.fpart, 0.978123, epsilon));
expect(math.approxEq(f32, r.ipart, 3.0, epsilon));
expect(math.approxEq(f32, r.fpart, 0.978123, epsilon));
r = modf32(43874.3);
assert(math.approxEq(f32, r.ipart, 43874, epsilon));
assert(math.approxEq(f32, r.fpart, 0.300781, epsilon));
expect(math.approxEq(f32, r.ipart, 43874, epsilon));
expect(math.approxEq(f32, r.fpart, 0.300781, epsilon));
r = modf32(1234.340780);
assert(math.approxEq(f32, r.ipart, 1234, epsilon));
assert(math.approxEq(f32, r.fpart, 0.340820, epsilon));
expect(math.approxEq(f32, r.ipart, 1234, epsilon));
expect(math.approxEq(f32, r.fpart, 0.340820, epsilon));
}
test "math.modf64" {
@ -156,48 +156,48 @@ test "math.modf64" {
var r: modf64_result = undefined;
r = modf64(1.0);
assert(math.approxEq(f64, r.ipart, 1.0, epsilon));
assert(math.approxEq(f64, r.fpart, 0.0, epsilon));
expect(math.approxEq(f64, r.ipart, 1.0, epsilon));
expect(math.approxEq(f64, r.fpart, 0.0, epsilon));
r = modf64(2.545);
assert(math.approxEq(f64, r.ipart, 2.0, epsilon));
assert(math.approxEq(f64, r.fpart, 0.545, epsilon));
expect(math.approxEq(f64, r.ipart, 2.0, epsilon));
expect(math.approxEq(f64, r.fpart, 0.545, epsilon));
r = modf64(3.978123);
assert(math.approxEq(f64, r.ipart, 3.0, epsilon));
assert(math.approxEq(f64, r.fpart, 0.978123, epsilon));
expect(math.approxEq(f64, r.ipart, 3.0, epsilon));
expect(math.approxEq(f64, r.fpart, 0.978123, epsilon));
r = modf64(43874.3);
assert(math.approxEq(f64, r.ipart, 43874, epsilon));
assert(math.approxEq(f64, r.fpart, 0.3, epsilon));
expect(math.approxEq(f64, r.ipart, 43874, epsilon));
expect(math.approxEq(f64, r.fpart, 0.3, epsilon));
r = modf64(1234.340780);
assert(math.approxEq(f64, r.ipart, 1234, epsilon));
assert(math.approxEq(f64, r.fpart, 0.340780, epsilon));
expect(math.approxEq(f64, r.ipart, 1234, epsilon));
expect(math.approxEq(f64, r.fpart, 0.340780, epsilon));
}
test "math.modf32.special" {
var r: modf32_result = undefined;
r = modf32(math.inf(f32));
assert(math.isPositiveInf(r.ipart) and math.isNan(r.fpart));
expect(math.isPositiveInf(r.ipart) and math.isNan(r.fpart));
r = modf32(-math.inf(f32));
assert(math.isNegativeInf(r.ipart) and math.isNan(r.fpart));
expect(math.isNegativeInf(r.ipart) and math.isNan(r.fpart));
r = modf32(math.nan(f32));
assert(math.isNan(r.ipart) and math.isNan(r.fpart));
expect(math.isNan(r.ipart) and math.isNan(r.fpart));
}
test "math.modf64.special" {
var r: modf64_result = undefined;
r = modf64(math.inf(f64));
assert(math.isPositiveInf(r.ipart) and math.isNan(r.fpart));
expect(math.isPositiveInf(r.ipart) and math.isNan(r.fpart));
r = modf64(-math.inf(f64));
assert(math.isNegativeInf(r.ipart) and math.isNan(r.fpart));
expect(math.isNegativeInf(r.ipart) and math.isNan(r.fpart));
r = modf64(math.nan(f64));
assert(math.isNan(r.ipart) and math.isNan(r.fpart));
expect(math.isNan(r.ipart) and math.isNan(r.fpart));
}

View File

@ -24,7 +24,7 @@
const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
// This implementation is taken from the go stlib, musl is a bit more complex.
pub fn pow(comptime T: type, x: T, y: T) T {
@ -179,56 +179,56 @@ fn isOddInteger(x: f64) bool {
test "math.pow" {
const epsilon = 0.000001;
assert(math.approxEq(f32, pow(f32, 0.0, 3.3), 0.0, epsilon));
assert(math.approxEq(f32, pow(f32, 0.8923, 3.3), 0.686572, epsilon));
assert(math.approxEq(f32, pow(f32, 0.2, 3.3), 0.004936, epsilon));
assert(math.approxEq(f32, pow(f32, 1.5, 3.3), 3.811546, epsilon));
assert(math.approxEq(f32, pow(f32, 37.45, 3.3), 155736.703125, epsilon));
assert(math.approxEq(f32, pow(f32, 89.123, 3.3), 2722489.5, epsilon));
expect(math.approxEq(f32, pow(f32, 0.0, 3.3), 0.0, epsilon));
expect(math.approxEq(f32, pow(f32, 0.8923, 3.3), 0.686572, epsilon));
expect(math.approxEq(f32, pow(f32, 0.2, 3.3), 0.004936, epsilon));
expect(math.approxEq(f32, pow(f32, 1.5, 3.3), 3.811546, epsilon));
expect(math.approxEq(f32, pow(f32, 37.45, 3.3), 155736.703125, epsilon));
expect(math.approxEq(f32, pow(f32, 89.123, 3.3), 2722489.5, epsilon));
assert(math.approxEq(f64, pow(f64, 0.0, 3.3), 0.0, epsilon));
assert(math.approxEq(f64, pow(f64, 0.8923, 3.3), 0.686572, epsilon));
assert(math.approxEq(f64, pow(f64, 0.2, 3.3), 0.004936, epsilon));
assert(math.approxEq(f64, pow(f64, 1.5, 3.3), 3.811546, epsilon));
assert(math.approxEq(f64, pow(f64, 37.45, 3.3), 155736.7160616, epsilon));
assert(math.approxEq(f64, pow(f64, 89.123, 3.3), 2722490.231436, epsilon));
expect(math.approxEq(f64, pow(f64, 0.0, 3.3), 0.0, epsilon));
expect(math.approxEq(f64, pow(f64, 0.8923, 3.3), 0.686572, epsilon));
expect(math.approxEq(f64, pow(f64, 0.2, 3.3), 0.004936, epsilon));
expect(math.approxEq(f64, pow(f64, 1.5, 3.3), 3.811546, epsilon));
expect(math.approxEq(f64, pow(f64, 37.45, 3.3), 155736.7160616, epsilon));
expect(math.approxEq(f64, pow(f64, 89.123, 3.3), 2722490.231436, epsilon));
}
test "math.pow.special" {
const epsilon = 0.000001;
assert(pow(f32, 4, 0.0) == 1.0);
assert(pow(f32, 7, -0.0) == 1.0);
assert(pow(f32, 45, 1.0) == 45);
assert(pow(f32, -45, 1.0) == -45);
assert(math.isNan(pow(f32, math.nan(f32), 5.0)));
assert(math.isNan(pow(f32, 5.0, math.nan(f32))));
assert(math.isPositiveInf(pow(f32, 0.0, -1.0)));
//assert(math.isNegativeInf(pow(f32, -0.0, -3.0))); TODO is this required?
assert(math.isPositiveInf(pow(f32, 0.0, -math.inf(f32))));
assert(math.isPositiveInf(pow(f32, -0.0, -math.inf(f32))));
assert(pow(f32, 0.0, math.inf(f32)) == 0.0);
assert(pow(f32, -0.0, math.inf(f32)) == 0.0);
assert(math.isPositiveInf(pow(f32, 0.0, -2.0)));
assert(math.isPositiveInf(pow(f32, -0.0, -2.0)));
assert(pow(f32, 0.0, 1.0) == 0.0);
assert(pow(f32, -0.0, 1.0) == -0.0);
assert(pow(f32, 0.0, 2.0) == 0.0);
assert(pow(f32, -0.0, 2.0) == 0.0);
assert(math.approxEq(f32, pow(f32, -1.0, math.inf(f32)), 1.0, epsilon));
assert(math.approxEq(f32, pow(f32, -1.0, -math.inf(f32)), 1.0, epsilon));
assert(math.isPositiveInf(pow(f32, 1.2, math.inf(f32))));
assert(math.isPositiveInf(pow(f32, -1.2, math.inf(f32))));
assert(pow(f32, 1.2, -math.inf(f32)) == 0.0);
assert(pow(f32, -1.2, -math.inf(f32)) == 0.0);
assert(pow(f32, 0.2, math.inf(f32)) == 0.0);
assert(pow(f32, -0.2, math.inf(f32)) == 0.0);
assert(math.isPositiveInf(pow(f32, 0.2, -math.inf(f32))));
assert(math.isPositiveInf(pow(f32, -0.2, -math.inf(f32))));
assert(math.isPositiveInf(pow(f32, math.inf(f32), 1.0)));
assert(pow(f32, math.inf(f32), -1.0) == 0.0);
//assert(pow(f32, -math.inf(f32), 5.0) == pow(f32, -0.0, -5.0)); TODO support negative 0?
assert(pow(f32, -math.inf(f32), -5.2) == pow(f32, -0.0, 5.2));
assert(math.isNan(pow(f32, -1.0, 1.2)));
assert(math.isNan(pow(f32, -12.4, 78.5)));
expect(pow(f32, 4, 0.0) == 1.0);
expect(pow(f32, 7, -0.0) == 1.0);
expect(pow(f32, 45, 1.0) == 45);
expect(pow(f32, -45, 1.0) == -45);
expect(math.isNan(pow(f32, math.nan(f32), 5.0)));
expect(math.isNan(pow(f32, 5.0, math.nan(f32))));
expect(math.isPositiveInf(pow(f32, 0.0, -1.0)));
//expect(math.isNegativeInf(pow(f32, -0.0, -3.0))); TODO is this required?
expect(math.isPositiveInf(pow(f32, 0.0, -math.inf(f32))));
expect(math.isPositiveInf(pow(f32, -0.0, -math.inf(f32))));
expect(pow(f32, 0.0, math.inf(f32)) == 0.0);
expect(pow(f32, -0.0, math.inf(f32)) == 0.0);
expect(math.isPositiveInf(pow(f32, 0.0, -2.0)));
expect(math.isPositiveInf(pow(f32, -0.0, -2.0)));
expect(pow(f32, 0.0, 1.0) == 0.0);
expect(pow(f32, -0.0, 1.0) == -0.0);
expect(pow(f32, 0.0, 2.0) == 0.0);
expect(pow(f32, -0.0, 2.0) == 0.0);
expect(math.approxEq(f32, pow(f32, -1.0, math.inf(f32)), 1.0, epsilon));
expect(math.approxEq(f32, pow(f32, -1.0, -math.inf(f32)), 1.0, epsilon));
expect(math.isPositiveInf(pow(f32, 1.2, math.inf(f32))));
expect(math.isPositiveInf(pow(f32, -1.2, math.inf(f32))));
expect(pow(f32, 1.2, -math.inf(f32)) == 0.0);
expect(pow(f32, -1.2, -math.inf(f32)) == 0.0);
expect(pow(f32, 0.2, math.inf(f32)) == 0.0);
expect(pow(f32, -0.2, math.inf(f32)) == 0.0);
expect(math.isPositiveInf(pow(f32, 0.2, -math.inf(f32))));
expect(math.isPositiveInf(pow(f32, -0.2, -math.inf(f32))));
expect(math.isPositiveInf(pow(f32, math.inf(f32), 1.0)));
expect(pow(f32, math.inf(f32), -1.0) == 0.0);
//expect(pow(f32, -math.inf(f32), 5.0) == pow(f32, -0.0, -5.0)); TODO support negative 0?
expect(pow(f32, -math.inf(f32), -5.2) == pow(f32, -0.0, 5.2));
expect(math.isNan(pow(f32, -1.0, 1.2)));
expect(math.isNan(pow(f32, -12.4, 78.5)));
}

View File

@ -12,7 +12,7 @@ const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const assertError = std.debug.assertError;
const testing = std.testing;
// This implementation is based on that from the rust stlib
pub fn powi(comptime T: type, x: T, y: T) (error{
@ -103,75 +103,75 @@ pub fn powi(comptime T: type, x: T, y: T) (error{
}
test "math.powi" {
assertError(powi(i8, -66, 6), error.Underflow);
assertError(powi(i16, -13, 13), error.Underflow);
assertError(powi(i32, -32, 21), error.Underflow);
assertError(powi(i64, -24, 61), error.Underflow);
assertError(powi(i17, -15, 15), error.Underflow);
assertError(powi(i42, -6, 40), error.Underflow);
testing.expectError(error.Underflow, powi(i8, -66, 6));
testing.expectError(error.Underflow, powi(i16, -13, 13));
testing.expectError(error.Underflow, powi(i32, -32, 21));
testing.expectError(error.Underflow, powi(i64, -24, 61));
testing.expectError(error.Underflow, powi(i17, -15, 15));
testing.expectError(error.Underflow, powi(i42, -6, 40));
assert((try powi(i8, -5, 3)) == -125);
assert((try powi(i16, -16, 3)) == -4096);
assert((try powi(i32, -91, 3)) == -753571);
assert((try powi(i64, -36, 6)) == 2176782336);
assert((try powi(i17, -2, 15)) == -32768);
assert((try powi(i42, -5, 7)) == -78125);
testing.expect((try powi(i8, -5, 3)) == -125);
testing.expect((try powi(i16, -16, 3)) == -4096);
testing.expect((try powi(i32, -91, 3)) == -753571);
testing.expect((try powi(i64, -36, 6)) == 2176782336);
testing.expect((try powi(i17, -2, 15)) == -32768);
testing.expect((try powi(i42, -5, 7)) == -78125);
assert((try powi(u8, 6, 2)) == 36);
assert((try powi(u16, 5, 4)) == 625);
assert((try powi(u32, 12, 6)) == 2985984);
assert((try powi(u64, 34, 2)) == 1156);
assert((try powi(u17, 16, 3)) == 4096);
assert((try powi(u42, 34, 6)) == 1544804416);
testing.expect((try powi(u8, 6, 2)) == 36);
testing.expect((try powi(u16, 5, 4)) == 625);
testing.expect((try powi(u32, 12, 6)) == 2985984);
testing.expect((try powi(u64, 34, 2)) == 1156);
testing.expect((try powi(u17, 16, 3)) == 4096);
testing.expect((try powi(u42, 34, 6)) == 1544804416);
assertError(powi(i8, 120, 7), error.Overflow);
assertError(powi(i16, 73, 15), error.Overflow);
assertError(powi(i32, 23, 31), error.Overflow);
assertError(powi(i64, 68, 61), error.Overflow);
assertError(powi(i17, 15, 15), error.Overflow);
assertError(powi(i42, 121312, 41), error.Overflow);
testing.expectError(error.Overflow, powi(i8, 120, 7));
testing.expectError(error.Overflow, powi(i16, 73, 15));
testing.expectError(error.Overflow, powi(i32, 23, 31));
testing.expectError(error.Overflow, powi(i64, 68, 61));
testing.expectError(error.Overflow, powi(i17, 15, 15));
testing.expectError(error.Overflow, powi(i42, 121312, 41));
assertError(powi(u8, 123, 7), error.Overflow);
assertError(powi(u16, 2313, 15), error.Overflow);
assertError(powi(u32, 8968, 31), error.Overflow);
assertError(powi(u64, 2342, 63), error.Overflow);
assertError(powi(u17, 2723, 16), error.Overflow);
assertError(powi(u42, 8234, 41), error.Overflow);
testing.expectError(error.Overflow, powi(u8, 123, 7));
testing.expectError(error.Overflow, powi(u16, 2313, 15));
testing.expectError(error.Overflow, powi(u32, 8968, 31));
testing.expectError(error.Overflow, powi(u64, 2342, 63));
testing.expectError(error.Overflow, powi(u17, 2723, 16));
testing.expectError(error.Overflow, powi(u42, 8234, 41));
}
test "math.powi.special" {
assertError(powi(i8, -2, 8), error.Underflow);
assertError(powi(i16, -2, 16), error.Underflow);
assertError(powi(i32, -2, 32), error.Underflow);
assertError(powi(i64, -2, 64), error.Underflow);
assertError(powi(i17, -2, 17), error.Underflow);
assertError(powi(i42, -2, 42), error.Underflow);
testing.expectError(error.Underflow, powi(i8, -2, 8));
testing.expectError(error.Underflow, powi(i16, -2, 16));
testing.expectError(error.Underflow, powi(i32, -2, 32));
testing.expectError(error.Underflow, powi(i64, -2, 64));
testing.expectError(error.Underflow, powi(i17, -2, 17));
testing.expectError(error.Underflow, powi(i42, -2, 42));
assert((try powi(i8, -1, 3)) == -1);
assert((try powi(i16, -1, 2)) == 1);
assert((try powi(i32, -1, 16)) == 1);
assert((try powi(i64, -1, 6)) == 1);
assert((try powi(i17, -1, 15)) == -1);
assert((try powi(i42, -1, 7)) == -1);
testing.expect((try powi(i8, -1, 3)) == -1);
testing.expect((try powi(i16, -1, 2)) == 1);
testing.expect((try powi(i32, -1, 16)) == 1);
testing.expect((try powi(i64, -1, 6)) == 1);
testing.expect((try powi(i17, -1, 15)) == -1);
testing.expect((try powi(i42, -1, 7)) == -1);
assert((try powi(u8, 1, 2)) == 1);
assert((try powi(u16, 1, 4)) == 1);
assert((try powi(u32, 1, 6)) == 1);
assert((try powi(u64, 1, 2)) == 1);
assert((try powi(u17, 1, 3)) == 1);
assert((try powi(u42, 1, 6)) == 1);
testing.expect((try powi(u8, 1, 2)) == 1);
testing.expect((try powi(u16, 1, 4)) == 1);
testing.expect((try powi(u32, 1, 6)) == 1);
testing.expect((try powi(u64, 1, 2)) == 1);
testing.expect((try powi(u17, 1, 3)) == 1);
testing.expect((try powi(u42, 1, 6)) == 1);
assertError(powi(i8, 2, 7), error.Overflow);
assertError(powi(i16, 2, 15), error.Overflow);
assertError(powi(i32, 2, 31), error.Overflow);
assertError(powi(i64, 2, 63), error.Overflow);
assertError(powi(i17, 2, 16), error.Overflow);
assertError(powi(i42, 2, 41), error.Overflow);
testing.expectError(error.Overflow, powi(i8, 2, 7));
testing.expectError(error.Overflow, powi(i16, 2, 15));
testing.expectError(error.Overflow, powi(i32, 2, 31));
testing.expectError(error.Overflow, powi(i64, 2, 63));
testing.expectError(error.Overflow, powi(i17, 2, 16));
testing.expectError(error.Overflow, powi(i42, 2, 41));
assertError(powi(u8, 2, 8), error.Overflow);
assertError(powi(u16, 2, 16), error.Overflow);
assertError(powi(u32, 2, 32), error.Overflow);
assertError(powi(u64, 2, 64), error.Overflow);
assertError(powi(u17, 2, 17), error.Overflow);
assertError(powi(u42, 2, 42), error.Overflow);
testing.expectError(error.Overflow, powi(u8, 2, 8));
testing.expectError(error.Overflow, powi(u16, 2, 16));
testing.expectError(error.Overflow, powi(u32, 2, 32));
testing.expectError(error.Overflow, powi(u64, 2, 64));
testing.expectError(error.Overflow, powi(u17, 2, 17));
testing.expectError(error.Overflow, powi(u42, 2, 42));
}

View File

@ -5,7 +5,7 @@
// - round(nan) = nan
const builtin = @import("builtin");
const assert = std.debug.assert;
const expect = std.testing.expect;
const std = @import("../index.zig");
const math = std.math;
@ -85,36 +85,36 @@ fn round64(x_: f64) f64 {
}
test "math.round" {
assert(round(f32(1.3)) == round32(1.3));
assert(round(f64(1.3)) == round64(1.3));
expect(round(f32(1.3)) == round32(1.3));
expect(round(f64(1.3)) == round64(1.3));
}
test "math.round32" {
assert(round32(1.3) == 1.0);
assert(round32(-1.3) == -1.0);
assert(round32(0.2) == 0.0);
assert(round32(1.8) == 2.0);
expect(round32(1.3) == 1.0);
expect(round32(-1.3) == -1.0);
expect(round32(0.2) == 0.0);
expect(round32(1.8) == 2.0);
}
test "math.round64" {
assert(round64(1.3) == 1.0);
assert(round64(-1.3) == -1.0);
assert(round64(0.2) == 0.0);
assert(round64(1.8) == 2.0);
expect(round64(1.3) == 1.0);
expect(round64(-1.3) == -1.0);
expect(round64(0.2) == 0.0);
expect(round64(1.8) == 2.0);
}
test "math.round32.special" {
assert(round32(0.0) == 0.0);
assert(round32(-0.0) == -0.0);
assert(math.isPositiveInf(round32(math.inf(f32))));
assert(math.isNegativeInf(round32(-math.inf(f32))));
assert(math.isNan(round32(math.nan(f32))));
expect(round32(0.0) == 0.0);
expect(round32(-0.0) == -0.0);
expect(math.isPositiveInf(round32(math.inf(f32))));
expect(math.isNegativeInf(round32(-math.inf(f32))));
expect(math.isNan(round32(math.nan(f32))));
}
test "math.round64.special" {
assert(round64(0.0) == 0.0);
assert(round64(-0.0) == -0.0);
assert(math.isPositiveInf(round64(math.inf(f64))));
assert(math.isNegativeInf(round64(-math.inf(f64))));
assert(math.isNan(round64(math.nan(f64))));
expect(round64(0.0) == 0.0);
expect(round64(-0.0) == -0.0);
expect(math.isPositiveInf(round64(math.inf(f64))));
expect(math.isNegativeInf(round64(-math.inf(f64))));
expect(math.isNan(round64(math.nan(f64))));
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn scalbn(x: var, n: i32) @typeOf(x) {
const T = @typeOf(x);
@ -72,14 +72,14 @@ fn scalbn64(x: f64, n_: i32) f64 {
}
test "math.scalbn" {
assert(scalbn(f32(1.5), 4) == scalbn32(1.5, 4));
assert(scalbn(f64(1.5), 4) == scalbn64(1.5, 4));
expect(scalbn(f32(1.5), 4) == scalbn32(1.5, 4));
expect(scalbn(f64(1.5), 4) == scalbn64(1.5, 4));
}
test "math.scalbn32" {
assert(scalbn32(1.5, 4) == 24.0);
expect(scalbn32(1.5, 4) == 24.0);
}
test "math.scalbn64" {
assert(scalbn64(1.5, 4) == 24.0);
expect(scalbn64(1.5, 4) == 24.0);
}

View File

@ -1,6 +1,6 @@
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn signbit(x: var) bool {
const T = @typeOf(x);
@ -28,22 +28,22 @@ fn signbit64(x: f64) bool {
}
test "math.signbit" {
assert(signbit(f16(4.0)) == signbit16(4.0));
assert(signbit(f32(4.0)) == signbit32(4.0));
assert(signbit(f64(4.0)) == signbit64(4.0));
expect(signbit(f16(4.0)) == signbit16(4.0));
expect(signbit(f32(4.0)) == signbit32(4.0));
expect(signbit(f64(4.0)) == signbit64(4.0));
}
test "math.signbit16" {
assert(!signbit16(4.0));
assert(signbit16(-3.0));
expect(!signbit16(4.0));
expect(signbit16(-3.0));
}
test "math.signbit32" {
assert(!signbit32(4.0));
assert(signbit32(-3.0));
expect(!signbit32(4.0));
expect(signbit32(-3.0));
}
test "math.signbit64" {
assert(!signbit64(4.0));
assert(signbit64(-3.0));
expect(!signbit64(4.0));
expect(signbit64(-3.0));
}

View File

@ -7,7 +7,7 @@
const builtin = @import("builtin");
const std = @import("../index.zig");
const math = std.math;
const assert = std.debug.assert;
const expect = std.testing.expect;
pub fn sin(x: var) @typeOf(x) {
const T = @typeOf(x);
@ -142,45 +142,45 @@ fn sin64(x_: f64) f64 {
}
test "math.sin" {
assert(sin(f32(0.0)) == sin32(0.0));
assert(sin(f64(0.0)) == sin64(0.0));
assert(comptime (math.sin(f64(2))) == math.sin(f64(2)));
expect(sin(f32(0.0)) == sin32(0.0));
expect(sin(f64(0.0)) == sin64(0.0));
expect(comptime (math.sin(f64(2))) == math.sin(f64(2)));
}
test "math.sin32" {
const epsilon = 0.000001;
assert(math.approxEq(f32, sin32(0.0), 0.0, epsilon));
assert(math.approxEq(f32, sin32(0.2), 0.198669, epsilon));
assert(math.approxEq(f32, sin32(0.8923), 0.778517, epsilon));
assert(math.approxEq(f32, sin32(1.5), 0.997495, epsilon));
assert(math.approxEq(f32, sin32(37.45), -0.246544, epsilon));
assert(math.approxEq(f32, sin32(89.123), 0.916166, epsilon));
expect(math.approxEq(f32, sin32(0.0), 0.0, epsilon));
expect(math.approxEq(f32, sin32(0.2), 0.198669, epsilon));
expect(math.approxEq(f32, sin32(0.8923), 0.778517, epsilon));
expect(math.approxEq(f32, sin32(1.5), 0.997495, epsilon));
expect(math.approxEq(f32, sin32(37.45), -0.246544, epsilon));
expect(math.approxEq(f32, sin32(89.123), 0.916166, epsilon));
}
test "math.sin64" {
const epsilon = 0.000001;
assert(math.approxEq(f64, sin64(0.0), 0.0, epsilon));
assert(math.approxEq(f64, sin64(0.2), 0.198669, epsilon));
assert(math.approxEq(f64, sin64(0.8923), 0.778517, epsilon));
assert(math.approxEq(f64, sin64(1.5), 0.997495, epsilon));
assert(math.approxEq(f64, sin64(37.45), -0.246543, epsilon));
assert(math.approxEq(f64, sin64(89.123), 0.916166, epsilon));
expect(math.approxEq(f64, sin64(0.0), 0.0, epsilon));
expect(math.approxEq(f64, sin64(0.2), 0.198669, epsilon));
expect(math.approxEq(f64, sin64(0.8923), 0.778517, epsilon));
expect(math.approxEq(f64, sin64(1.5), 0.997495, epsilon));
expect(math.approxEq(f64, sin64(37.45), -0.246543, epsilon));
expect(math.approxEq(f64, sin64(89.123), 0.916166, epsilon));
}
test "math.sin32.special" {
assert(sin32(0.0) == 0.0);
assert(sin32(-0.0) == -0.0);
assert(math.isNan(sin32(math.inf(f32))));
assert(math.isNan(sin32(-math.inf(f32))));
assert(math.isNan(sin32(math.nan(f32))));
expect(sin32(0.0) == 0.0);
expect(sin32(-0.0) == -0.0);
expect(math.isNan(sin32(math.inf(f32))));
expect(math.isNan(sin32(-math.inf(f32))));
expect(math.isNan(sin32(math.nan(f32))));
}
test "math.sin64.special" {
assert(sin64(0.0) == 0.0);
assert(sin64(-0.0) == -0.0);
assert(math.isNan(sin64(math.inf(f64))));
assert(math.isNan(sin64(-math.inf(f64))));
assert(math.isNan(sin64(math.nan(f64))));
expect(sin64(0.0) == 0.0);
expect(sin64(-0.0) == -0.0);
expect(math.isNan(sin64(math.inf(f64))));
expect(math.isNan(sin64(-math.inf(f64))));
expect(math.isNan(sin64(math.nan(f64))));
}

Some files were not shown because too many files have changed in this diff Show More