| author | |
| committer | |
| log | 4cb5fed10ba2233a3b19c33b56585eb73da8b001 |
| tree | b0f52f720e66ed1ea5ef7af2b418ec0b822a15c6 |
| parent | 361217bda2b4ce397a2b49ab7613162d1abcab67 |
Before it was being emitted as an `alloc` which caused inline for loops
to not work correctly.8 files changed, 490 insertions(+), 484 deletions(-)
src/AstGen.zig+2-1| ... | @@ -5735,7 +5735,8 @@ fn forExpr( | ... | @@ -5735,7 +5735,8 @@ fn forExpr( |
| 5735 | const len = try parent_gz.addUnNode(.indexable_ptr_len, array_ptr, for_full.ast.cond_expr); | 5735 | const len = try parent_gz.addUnNode(.indexable_ptr_len, array_ptr, for_full.ast.cond_expr); |
| 5736 | 5736 | ||
| 5737 | const index_ptr = blk: { | 5737 | const index_ptr = blk: { |
| 5738 | const index_ptr = try parent_gz.addUnNode(.alloc, .usize_type, node); | 5738 | const alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime else .alloc; |
| 5739 | const index_ptr = try parent_gz.addUnNode(alloc_tag, .usize_type, node); | ||
| 5739 | // initialize to zero | 5740 | // initialize to zero |
| 5740 | _ = try parent_gz.addBin(.store, index_ptr, .zero_usize); | 5741 | _ = try parent_gz.addBin(.store, index_ptr, .zero_usize); |
| 5741 | break :blk index_ptr; | 5742 | break :blk index_ptr; |
test/behavior.zig+2-1| ... | @@ -32,6 +32,7 @@ test { | ... | @@ -32,6 +32,7 @@ test { |
| 32 | _ = @import("behavior/enum.zig"); | 32 | _ = @import("behavior/enum.zig"); |
| 33 | _ = @import("behavior/error.zig"); | 33 | _ = @import("behavior/error.zig"); |
| 34 | _ = @import("behavior/eval.zig"); | 34 | _ = @import("behavior/eval.zig"); |
| 35 | _ = @import("behavior/floatop.zig"); | ||
| 35 | _ = @import("behavior/for.zig"); | 36 | _ = @import("behavior/for.zig"); |
| 36 | _ = @import("behavior/generics.zig"); | 37 | _ = @import("behavior/generics.zig"); |
| 37 | _ = @import("behavior/hasdecl.zig"); | 38 | _ = @import("behavior/hasdecl.zig"); |
| ... | @@ -123,7 +124,7 @@ test { | ... | @@ -123,7 +124,7 @@ test { |
| 123 | _ = @import("behavior/error_stage1.zig"); | 124 | _ = @import("behavior/error_stage1.zig"); |
| 124 | _ = @import("behavior/eval_stage1.zig"); | 125 | _ = @import("behavior/eval_stage1.zig"); |
| 125 | _ = @import("behavior/field_parent_ptr.zig"); | 126 | _ = @import("behavior/field_parent_ptr.zig"); |
| 126 | _ = @import("behavior/floatop.zig"); | 127 | _ = @import("behavior/floatop_stage1.zig"); |
| 127 | _ = @import("behavior/fn.zig"); | 128 | _ = @import("behavior/fn.zig"); |
| 128 | _ = @import("behavior/fn_delegation.zig"); | 129 | _ = @import("behavior/fn_delegation.zig"); |
| 129 | _ = @import("behavior/fn_in_struct_in_comptime.zig"); | 130 | _ = @import("behavior/fn_in_struct_in_comptime.zig"); |
test/behavior/eval.zig+22| ... | @@ -368,3 +368,25 @@ test "return 0 from function that has u0 return type" { | ... | @@ -368,3 +368,25 @@ test "return 0 from function that has u0 return type" { |
| 368 | } | 368 | } |
| 369 | } | 369 | } |
| 370 | } | 370 | } |
| 371 | |||
| 372 | test "statically initialized struct" { | ||
| 373 | st_init_str_foo.x += 1; | ||
| 374 | try expect(st_init_str_foo.x == 14); | ||
| 375 | } | ||
| 376 | const StInitStrFoo = struct { | ||
| 377 | x: i32, | ||
| 378 | y: bool, | ||
| 379 | }; | ||
| 380 | var st_init_str_foo = StInitStrFoo{ | ||
| 381 | .x = 13, | ||
| 382 | .y = true, | ||
| 383 | }; | ||
| 384 | |||
| 385 | test "inline for with same type but different values" { | ||
| 386 | var res: usize = 0; | ||
| 387 | inline for ([_]type{ [2]u8, [1]u8, [2]u8 }) |T| { | ||
| 388 | var a: T = undefined; | ||
| 389 | res += a.len; | ||
| 390 | } | ||
| 391 | try expect(res == 5); | ||
| 392 | } |
test/behavior/eval_stage1.zig+1-27| ... | @@ -12,27 +12,10 @@ pub const Vec3 = struct { | ... | @@ -12,27 +12,10 @@ pub const Vec3 = struct { |
| 12 | }; | 12 | }; |
| 13 | pub fn vec3(x: f32, y: f32, z: f32) Vec3 { | 13 | pub fn vec3(x: f32, y: f32, z: f32) Vec3 { |
| 14 | return Vec3{ | 14 | return Vec3{ |
| 15 | .data = [_]f32{ | 15 | .data = [_]f32{ x, y, z }, |
| 16 | x, | ||
| 17 | y, | ||
| 18 | z, | ||
| 19 | }, | ||
| 20 | }; | 16 | }; |
| 21 | } | 17 | } |
| 22 | 18 | ||
| 23 | test "statically initialized struct" { | ||
| 24 | st_init_str_foo.x += 1; | ||
| 25 | try expect(st_init_str_foo.x == 14); | ||
| 26 | } | ||
| 27 | const StInitStrFoo = struct { | ||
| 28 | x: i32, | ||
| 29 | y: bool, | ||
| 30 | }; | ||
| 31 | var st_init_str_foo = StInitStrFoo{ | ||
| 32 | .x = 13, | ||
| 33 | .y = true, | ||
| 34 | }; | ||
| 35 | |||
| 36 | test "inlined loop has array literal with elided runtime scope on first iteration but not second iteration" { | 19 | test "inlined loop has array literal with elided runtime scope on first iteration but not second iteration" { |
| 37 | var runtime = [1]i32{3}; | 20 | var runtime = [1]i32{3}; |
| 38 | comptime var i: usize = 0; | 21 | comptime var i: usize = 0; |
| ... | @@ -301,15 +284,6 @@ fn testVarInsideInlineLoop(args: anytype) !void { | ... | @@ -301,15 +284,6 @@ fn testVarInsideInlineLoop(args: anytype) !void { |
| 301 | } | 284 | } |
| 302 | } | 285 | } |
| 303 | 286 | ||
| 304 | test "inline for with same type but different values" { | ||
| 305 | var res: usize = 0; | ||
| 306 | inline for ([_]type{ [2]u8, [1]u8, [2]u8 }) |T| { | ||
| 307 | var a: T = undefined; | ||
| 308 | res += a.len; | ||
| 309 | } | ||
| 310 | try expect(res == 5); | ||
| 311 | } | ||
| 312 | |||
| 313 | test "bit shift a u1" { | 287 | test "bit shift a u1" { |
| 314 | var x: u1 = 1; | 288 | var x: u1 = 1; |
| 315 | var y = x << 0; | 289 | var y = x << 0; |
test/behavior/floatop.zig-397| ... | @@ -7,403 +7,6 @@ const Vector = std.meta.Vector; | ... | @@ -7,403 +7,6 @@ const Vector = std.meta.Vector; |
| 7 | 7 | ||
| 8 | const epsilon = 0.000001; | 8 | const epsilon = 0.000001; |
| 9 | 9 | ||
| 10 | test "@sqrt" { | ||
| 11 | comptime try testSqrt(); | ||
| 12 | try testSqrt(); | ||
| 13 | } | ||
| 14 | |||
| 15 | fn testSqrt() !void { | ||
| 16 | { | ||
| 17 | var a: f16 = 4; | ||
| 18 | try expect(@sqrt(a) == 2); | ||
| 19 | } | ||
| 20 | { | ||
| 21 | var a: f32 = 9; | ||
| 22 | try expect(@sqrt(a) == 3); | ||
| 23 | var b: f32 = 1.1; | ||
| 24 | try expect(math.approxEqAbs(f32, @sqrt(b), 1.0488088481701516, epsilon)); | ||
| 25 | } | ||
| 26 | { | ||
| 27 | var a: f64 = 25; | ||
| 28 | try expect(@sqrt(a) == 5); | ||
| 29 | } | ||
| 30 | { | ||
| 31 | const a: comptime_float = 25.0; | ||
| 32 | try expect(@sqrt(a) == 5.0); | ||
| 33 | } | ||
| 34 | // TODO https://github.com/ziglang/zig/issues/4026 | ||
| 35 | //{ | ||
| 36 | // var a: f128 = 49; | ||
| 37 | //try expect(@sqrt(a) == 7); | ||
| 38 | //} | ||
| 39 | { | ||
| 40 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 }; | ||
| 41 | var result = @sqrt(v); | ||
| 42 | try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 1.1)), result[0], epsilon)); | ||
| 43 | try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 2.2)), result[1], epsilon)); | ||
| 44 | try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 3.3)), result[2], epsilon)); | ||
| 45 | try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 4.4)), result[3], epsilon)); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | test "more @sqrt f16 tests" { | ||
| 50 | // TODO these are not all passing at comptime | ||
| 51 | try expect(@sqrt(@as(f16, 0.0)) == 0.0); | ||
| 52 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon)); | ||
| 53 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 3.6)), 1.897367, epsilon)); | ||
| 54 | try expect(@sqrt(@as(f16, 4.0)) == 2.0); | ||
| 55 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 7.539840)), 2.745877, epsilon)); | ||
| 56 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 19.230934)), 4.385309, epsilon)); | ||
| 57 | try expect(@sqrt(@as(f16, 64.0)) == 8.0); | ||
| 58 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 64.1)), 8.006248, epsilon)); | ||
| 59 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 8942.230469)), 94.563370, epsilon)); | ||
| 60 | |||
| 61 | // special cases | ||
| 62 | try expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16))))); | ||
| 63 | try expect(@sqrt(@as(f16, 0.0)) == 0.0); | ||
| 64 | try expect(@sqrt(@as(f16, -0.0)) == -0.0); | ||
| 65 | try expect(math.isNan(@sqrt(@as(f16, -1.0)))); | ||
| 66 | try expect(math.isNan(@sqrt(@as(f16, math.nan(f16))))); | ||
| 67 | } | ||
| 68 | |||
| 69 | test "@sin" { | ||
| 70 | comptime try testSin(); | ||
| 71 | try testSin(); | ||
| 72 | } | ||
| 73 | |||
| 74 | fn testSin() !void { | ||
| 75 | // TODO test f128, and c_longdouble | ||
| 76 | // https://github.com/ziglang/zig/issues/4026 | ||
| 77 | { | ||
| 78 | var a: f16 = 0; | ||
| 79 | try expect(@sin(a) == 0); | ||
| 80 | } | ||
| 81 | { | ||
| 82 | var a: f32 = 0; | ||
| 83 | try expect(@sin(a) == 0); | ||
| 84 | } | ||
| 85 | { | ||
| 86 | var a: f64 = 0; | ||
| 87 | try expect(@sin(a) == 0); | ||
| 88 | } | ||
| 89 | { | ||
| 90 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 }; | ||
| 91 | var result = @sin(v); | ||
| 92 | try expect(math.approxEqAbs(f32, @sin(@as(f32, 1.1)), result[0], epsilon)); | ||
| 93 | try expect(math.approxEqAbs(f32, @sin(@as(f32, 2.2)), result[1], epsilon)); | ||
| 94 | try expect(math.approxEqAbs(f32, @sin(@as(f32, 3.3)), result[2], epsilon)); | ||
| 95 | try expect(math.approxEqAbs(f32, @sin(@as(f32, 4.4)), result[3], epsilon)); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | test "@cos" { | ||
| 100 | comptime try testCos(); | ||
| 101 | try testCos(); | ||
| 102 | } | ||
| 103 | |||
| 104 | fn testCos() !void { | ||
| 105 | // TODO test f128, and c_longdouble | ||
| 106 | // https://github.com/ziglang/zig/issues/4026 | ||
| 107 | { | ||
| 108 | var a: f16 = 0; | ||
| 109 | try expect(@cos(a) == 1); | ||
| 110 | } | ||
| 111 | { | ||
| 112 | var a: f32 = 0; | ||
| 113 | try expect(@cos(a) == 1); | ||
| 114 | } | ||
| 115 | { | ||
| 116 | var a: f64 = 0; | ||
| 117 | try expect(@cos(a) == 1); | ||
| 118 | } | ||
| 119 | { | ||
| 120 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 }; | ||
| 121 | var result = @cos(v); | ||
| 122 | try expect(math.approxEqAbs(f32, @cos(@as(f32, 1.1)), result[0], epsilon)); | ||
| 123 | try expect(math.approxEqAbs(f32, @cos(@as(f32, 2.2)), result[1], epsilon)); | ||
| 124 | try expect(math.approxEqAbs(f32, @cos(@as(f32, 3.3)), result[2], epsilon)); | ||
| 125 | try expect(math.approxEqAbs(f32, @cos(@as(f32, 4.4)), result[3], epsilon)); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | test "@exp" { | ||
| 130 | comptime try testExp(); | ||
| 131 | try testExp(); | ||
| 132 | } | ||
| 133 | |||
| 134 | fn testExp() !void { | ||
| 135 | // TODO test f128, and c_longdouble | ||
| 136 | // https://github.com/ziglang/zig/issues/4026 | ||
| 137 | { | ||
| 138 | var a: f16 = 0; | ||
| 139 | try expect(@exp(a) == 1); | ||
| 140 | } | ||
| 141 | { | ||
| 142 | var a: f32 = 0; | ||
| 143 | try expect(@exp(a) == 1); | ||
| 144 | } | ||
| 145 | { | ||
| 146 | var a: f64 = 0; | ||
| 147 | try expect(@exp(a) == 1); | ||
| 148 | } | ||
| 149 | { | ||
| 150 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 151 | var result = @exp(v); | ||
| 152 | try expect(math.approxEqAbs(f32, @exp(@as(f32, 1.1)), result[0], epsilon)); | ||
| 153 | try expect(math.approxEqAbs(f32, @exp(@as(f32, 2.2)), result[1], epsilon)); | ||
| 154 | try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.3)), result[2], epsilon)); | ||
| 155 | try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.4)), result[3], epsilon)); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | test "@exp2" { | ||
| 160 | comptime try testExp2(); | ||
| 161 | try testExp2(); | ||
| 162 | } | ||
| 163 | |||
| 164 | fn testExp2() !void { | ||
| 165 | // TODO test f128, and c_longdouble | ||
| 166 | // https://github.com/ziglang/zig/issues/4026 | ||
| 167 | { | ||
| 168 | var a: f16 = 2; | ||
| 169 | try expect(@exp2(a) == 4); | ||
| 170 | } | ||
| 171 | { | ||
| 172 | var a: f32 = 2; | ||
| 173 | try expect(@exp2(a) == 4); | ||
| 174 | } | ||
| 175 | { | ||
| 176 | var a: f64 = 2; | ||
| 177 | try expect(@exp2(a) == 4); | ||
| 178 | } | ||
| 179 | { | ||
| 180 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 181 | var result = @exp2(v); | ||
| 182 | try expect(math.approxEqAbs(f32, @exp2(@as(f32, 1.1)), result[0], epsilon)); | ||
| 183 | try expect(math.approxEqAbs(f32, @exp2(@as(f32, 2.2)), result[1], epsilon)); | ||
| 184 | try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.3)), result[2], epsilon)); | ||
| 185 | try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.4)), result[3], epsilon)); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | test "@log" { | ||
| 190 | // Old musl (and glibc?), and our current math.ln implementation do not return 1 | ||
| 191 | // so also accept those values. | ||
| 192 | comptime try testLog(); | ||
| 193 | try testLog(); | ||
| 194 | } | ||
| 195 | |||
| 196 | fn testLog() !void { | ||
| 197 | // TODO test f128, and c_longdouble | ||
| 198 | // https://github.com/ziglang/zig/issues/4026 | ||
| 199 | { | ||
| 200 | var a: f16 = e; | ||
| 201 | try expect(math.approxEqAbs(f16, @log(a), 1, epsilon)); | ||
| 202 | } | ||
| 203 | { | ||
| 204 | var a: f32 = e; | ||
| 205 | try expect(@log(a) == 1 or @log(a) == @bitCast(f32, @as(u32, 0x3f7fffff))); | ||
| 206 | } | ||
| 207 | { | ||
| 208 | var a: f64 = e; | ||
| 209 | try expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000))); | ||
| 210 | } | ||
| 211 | { | ||
| 212 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 213 | var result = @log(v); | ||
| 214 | try expect(math.approxEqAbs(f32, @log(@as(f32, 1.1)), result[0], epsilon)); | ||
| 215 | try expect(math.approxEqAbs(f32, @log(@as(f32, 2.2)), result[1], epsilon)); | ||
| 216 | try expect(math.approxEqAbs(f32, @log(@as(f32, 0.3)), result[2], epsilon)); | ||
| 217 | try expect(math.approxEqAbs(f32, @log(@as(f32, 0.4)), result[3], epsilon)); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | test "@log2" { | ||
| 222 | comptime try testLog2(); | ||
| 223 | try testLog2(); | ||
| 224 | } | ||
| 225 | |||
| 226 | fn testLog2() !void { | ||
| 227 | // TODO test f128, and c_longdouble | ||
| 228 | // https://github.com/ziglang/zig/issues/4026 | ||
| 229 | { | ||
| 230 | var a: f16 = 4; | ||
| 231 | try expect(@log2(a) == 2); | ||
| 232 | } | ||
| 233 | { | ||
| 234 | var a: f32 = 4; | ||
| 235 | try expect(@log2(a) == 2); | ||
| 236 | } | ||
| 237 | { | ||
| 238 | var a: f64 = 4; | ||
| 239 | try expect(@log2(a) == 2); | ||
| 240 | } | ||
| 241 | { | ||
| 242 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 243 | var result = @log2(v); | ||
| 244 | try expect(math.approxEqAbs(f32, @log2(@as(f32, 1.1)), result[0], epsilon)); | ||
| 245 | try expect(math.approxEqAbs(f32, @log2(@as(f32, 2.2)), result[1], epsilon)); | ||
| 246 | try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.3)), result[2], epsilon)); | ||
| 247 | try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.4)), result[3], epsilon)); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | test "@log10" { | ||
| 252 | comptime try testLog10(); | ||
| 253 | try testLog10(); | ||
| 254 | } | ||
| 255 | |||
| 256 | fn testLog10() !void { | ||
| 257 | // TODO test f128, and c_longdouble | ||
| 258 | // https://github.com/ziglang/zig/issues/4026 | ||
| 259 | { | ||
| 260 | var a: f16 = 100; | ||
| 261 | try expect(@log10(a) == 2); | ||
| 262 | } | ||
| 263 | { | ||
| 264 | var a: f32 = 100; | ||
| 265 | try expect(@log10(a) == 2); | ||
| 266 | } | ||
| 267 | { | ||
| 268 | var a: f64 = 1000; | ||
| 269 | try expect(@log10(a) == 3); | ||
| 270 | } | ||
| 271 | { | ||
| 272 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 273 | var result = @log10(v); | ||
| 274 | try expect(math.approxEqAbs(f32, @log10(@as(f32, 1.1)), result[0], epsilon)); | ||
| 275 | try expect(math.approxEqAbs(f32, @log10(@as(f32, 2.2)), result[1], epsilon)); | ||
| 276 | try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.3)), result[2], epsilon)); | ||
| 277 | try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.4)), result[3], epsilon)); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | test "@fabs" { | ||
| 282 | comptime try testFabs(); | ||
| 283 | try testFabs(); | ||
| 284 | } | ||
| 285 | |||
| 286 | fn testFabs() !void { | ||
| 287 | // TODO test f128, and c_longdouble | ||
| 288 | // https://github.com/ziglang/zig/issues/4026 | ||
| 289 | { | ||
| 290 | var a: f16 = -2.5; | ||
| 291 | var b: f16 = 2.5; | ||
| 292 | try expect(@fabs(a) == 2.5); | ||
| 293 | try expect(@fabs(b) == 2.5); | ||
| 294 | } | ||
| 295 | { | ||
| 296 | var a: f32 = -2.5; | ||
| 297 | var b: f32 = 2.5; | ||
| 298 | try expect(@fabs(a) == 2.5); | ||
| 299 | try expect(@fabs(b) == 2.5); | ||
| 300 | } | ||
| 301 | { | ||
| 302 | var a: f64 = -2.5; | ||
| 303 | var b: f64 = 2.5; | ||
| 304 | try expect(@fabs(a) == 2.5); | ||
| 305 | try expect(@fabs(b) == 2.5); | ||
| 306 | } | ||
| 307 | { | ||
| 308 | var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 }; | ||
| 309 | var result = @fabs(v); | ||
| 310 | try expect(math.approxEqAbs(f32, @fabs(@as(f32, 1.1)), result[0], epsilon)); | ||
| 311 | try expect(math.approxEqAbs(f32, @fabs(@as(f32, -2.2)), result[1], epsilon)); | ||
| 312 | try expect(math.approxEqAbs(f32, @fabs(@as(f32, 0.3)), result[2], epsilon)); | ||
| 313 | try expect(math.approxEqAbs(f32, @fabs(@as(f32, -0.4)), result[3], epsilon)); | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | test "@floor" { | ||
| 318 | comptime try testFloor(); | ||
| 319 | try testFloor(); | ||
| 320 | } | ||
| 321 | |||
| 322 | fn testFloor() !void { | ||
| 323 | // TODO test f128, and c_longdouble | ||
| 324 | // https://github.com/ziglang/zig/issues/4026 | ||
| 325 | { | ||
| 326 | var a: f16 = 2.1; | ||
| 327 | try expect(@floor(a) == 2); | ||
| 328 | } | ||
| 329 | { | ||
| 330 | var a: f32 = 2.1; | ||
| 331 | try expect(@floor(a) == 2); | ||
| 332 | } | ||
| 333 | { | ||
| 334 | var a: f64 = 3.5; | ||
| 335 | try expect(@floor(a) == 3); | ||
| 336 | } | ||
| 337 | { | ||
| 338 | var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 }; | ||
| 339 | var result = @floor(v); | ||
| 340 | try expect(math.approxEqAbs(f32, @floor(@as(f32, 1.1)), result[0], epsilon)); | ||
| 341 | try expect(math.approxEqAbs(f32, @floor(@as(f32, -2.2)), result[1], epsilon)); | ||
| 342 | try expect(math.approxEqAbs(f32, @floor(@as(f32, 0.3)), result[2], epsilon)); | ||
| 343 | try expect(math.approxEqAbs(f32, @floor(@as(f32, -0.4)), result[3], epsilon)); | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | test "@ceil" { | ||
| 348 | comptime try testCeil(); | ||
| 349 | try testCeil(); | ||
| 350 | } | ||
| 351 | |||
| 352 | fn testCeil() !void { | ||
| 353 | // TODO test f128, and c_longdouble | ||
| 354 | // https://github.com/ziglang/zig/issues/4026 | ||
| 355 | { | ||
| 356 | var a: f16 = 2.1; | ||
| 357 | try expect(@ceil(a) == 3); | ||
| 358 | } | ||
| 359 | { | ||
| 360 | var a: f32 = 2.1; | ||
| 361 | try expect(@ceil(a) == 3); | ||
| 362 | } | ||
| 363 | { | ||
| 364 | var a: f64 = 3.5; | ||
| 365 | try expect(@ceil(a) == 4); | ||
| 366 | } | ||
| 367 | { | ||
| 368 | var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 }; | ||
| 369 | var result = @ceil(v); | ||
| 370 | try expect(math.approxEqAbs(f32, @ceil(@as(f32, 1.1)), result[0], epsilon)); | ||
| 371 | try expect(math.approxEqAbs(f32, @ceil(@as(f32, -2.2)), result[1], epsilon)); | ||
| 372 | try expect(math.approxEqAbs(f32, @ceil(@as(f32, 0.3)), result[2], epsilon)); | ||
| 373 | try expect(math.approxEqAbs(f32, @ceil(@as(f32, -0.4)), result[3], epsilon)); | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | test "@trunc" { | ||
| 378 | comptime try testTrunc(); | ||
| 379 | try testTrunc(); | ||
| 380 | } | ||
| 381 | |||
| 382 | fn testTrunc() !void { | ||
| 383 | // TODO test f128, and c_longdouble | ||
| 384 | // https://github.com/ziglang/zig/issues/4026 | ||
| 385 | { | ||
| 386 | var a: f16 = 2.1; | ||
| 387 | try expect(@trunc(a) == 2); | ||
| 388 | } | ||
| 389 | { | ||
| 390 | var a: f32 = 2.1; | ||
| 391 | try expect(@trunc(a) == 2); | ||
| 392 | } | ||
| 393 | { | ||
| 394 | var a: f64 = -3.5; | ||
| 395 | try expect(@trunc(a) == -3); | ||
| 396 | } | ||
| 397 | { | ||
| 398 | var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 }; | ||
| 399 | var result = @trunc(v); | ||
| 400 | try expect(math.approxEqAbs(f32, @trunc(@as(f32, 1.1)), result[0], epsilon)); | ||
| 401 | try expect(math.approxEqAbs(f32, @trunc(@as(f32, -2.2)), result[1], epsilon)); | ||
| 402 | try expect(math.approxEqAbs(f32, @trunc(@as(f32, 0.3)), result[2], epsilon)); | ||
| 403 | try expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon)); | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | test "floating point comparisons" { | 10 | test "floating point comparisons" { |
| 408 | try testFloatComparisons(); | 11 | try testFloatComparisons(); |
| 409 | comptime try testFloatComparisons(); | 12 | comptime try testFloatComparisons(); |
test/behavior/floatop_stage1.zig created+405| ... | @@ -0,0 +1,405 @@ | ||
| 1 | const std = @import("std"); | ||
| 2 | const expect = std.testing.expect; | ||
| 3 | const math = std.math; | ||
| 4 | const pi = std.math.pi; | ||
| 5 | const e = std.math.e; | ||
| 6 | const Vector = std.meta.Vector; | ||
| 7 | |||
| 8 | const epsilon = 0.000001; | ||
| 9 | |||
| 10 | test "@sqrt" { | ||
| 11 | comptime try testSqrt(); | ||
| 12 | try testSqrt(); | ||
| 13 | } | ||
| 14 | |||
| 15 | fn testSqrt() !void { | ||
| 16 | { | ||
| 17 | var a: f16 = 4; | ||
| 18 | try expect(@sqrt(a) == 2); | ||
| 19 | } | ||
| 20 | { | ||
| 21 | var a: f32 = 9; | ||
| 22 | try expect(@sqrt(a) == 3); | ||
| 23 | var b: f32 = 1.1; | ||
| 24 | try expect(math.approxEqAbs(f32, @sqrt(b), 1.0488088481701516, epsilon)); | ||
| 25 | } | ||
| 26 | { | ||
| 27 | var a: f64 = 25; | ||
| 28 | try expect(@sqrt(a) == 5); | ||
| 29 | } | ||
| 30 | { | ||
| 31 | const a: comptime_float = 25.0; | ||
| 32 | try expect(@sqrt(a) == 5.0); | ||
| 33 | } | ||
| 34 | // TODO https://github.com/ziglang/zig/issues/4026 | ||
| 35 | //{ | ||
| 36 | // var a: f128 = 49; | ||
| 37 | //try expect(@sqrt(a) == 7); | ||
| 38 | //} | ||
| 39 | { | ||
| 40 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 }; | ||
| 41 | var result = @sqrt(v); | ||
| 42 | try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 1.1)), result[0], epsilon)); | ||
| 43 | try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 2.2)), result[1], epsilon)); | ||
| 44 | try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 3.3)), result[2], epsilon)); | ||
| 45 | try expect(math.approxEqAbs(f32, @sqrt(@as(f32, 4.4)), result[3], epsilon)); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | test "more @sqrt f16 tests" { | ||
| 50 | // TODO these are not all passing at comptime | ||
| 51 | try expect(@sqrt(@as(f16, 0.0)) == 0.0); | ||
| 52 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon)); | ||
| 53 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 3.6)), 1.897367, epsilon)); | ||
| 54 | try expect(@sqrt(@as(f16, 4.0)) == 2.0); | ||
| 55 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 7.539840)), 2.745877, epsilon)); | ||
| 56 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 19.230934)), 4.385309, epsilon)); | ||
| 57 | try expect(@sqrt(@as(f16, 64.0)) == 8.0); | ||
| 58 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 64.1)), 8.006248, epsilon)); | ||
| 59 | try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 8942.230469)), 94.563370, epsilon)); | ||
| 60 | |||
| 61 | // special cases | ||
| 62 | try expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16))))); | ||
| 63 | try expect(@sqrt(@as(f16, 0.0)) == 0.0); | ||
| 64 | try expect(@sqrt(@as(f16, -0.0)) == -0.0); | ||
| 65 | try expect(math.isNan(@sqrt(@as(f16, -1.0)))); | ||
| 66 | try expect(math.isNan(@sqrt(@as(f16, math.nan(f16))))); | ||
| 67 | } | ||
| 68 | |||
| 69 | test "@sin" { | ||
| 70 | comptime try testSin(); | ||
| 71 | try testSin(); | ||
| 72 | } | ||
| 73 | |||
| 74 | fn testSin() !void { | ||
| 75 | // TODO test f128, and c_longdouble | ||
| 76 | // https://github.com/ziglang/zig/issues/4026 | ||
| 77 | { | ||
| 78 | var a: f16 = 0; | ||
| 79 | try expect(@sin(a) == 0); | ||
| 80 | } | ||
| 81 | { | ||
| 82 | var a: f32 = 0; | ||
| 83 | try expect(@sin(a) == 0); | ||
| 84 | } | ||
| 85 | { | ||
| 86 | var a: f64 = 0; | ||
| 87 | try expect(@sin(a) == 0); | ||
| 88 | } | ||
| 89 | { | ||
| 90 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 }; | ||
| 91 | var result = @sin(v); | ||
| 92 | try expect(math.approxEqAbs(f32, @sin(@as(f32, 1.1)), result[0], epsilon)); | ||
| 93 | try expect(math.approxEqAbs(f32, @sin(@as(f32, 2.2)), result[1], epsilon)); | ||
| 94 | try expect(math.approxEqAbs(f32, @sin(@as(f32, 3.3)), result[2], epsilon)); | ||
| 95 | try expect(math.approxEqAbs(f32, @sin(@as(f32, 4.4)), result[3], epsilon)); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | test "@cos" { | ||
| 100 | comptime try testCos(); | ||
| 101 | try testCos(); | ||
| 102 | } | ||
| 103 | |||
| 104 | fn testCos() !void { | ||
| 105 | // TODO test f128, and c_longdouble | ||
| 106 | // https://github.com/ziglang/zig/issues/4026 | ||
| 107 | { | ||
| 108 | var a: f16 = 0; | ||
| 109 | try expect(@cos(a) == 1); | ||
| 110 | } | ||
| 111 | { | ||
| 112 | var a: f32 = 0; | ||
| 113 | try expect(@cos(a) == 1); | ||
| 114 | } | ||
| 115 | { | ||
| 116 | var a: f64 = 0; | ||
| 117 | try expect(@cos(a) == 1); | ||
| 118 | } | ||
| 119 | { | ||
| 120 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 3.3, 4.4 }; | ||
| 121 | var result = @cos(v); | ||
| 122 | try expect(math.approxEqAbs(f32, @cos(@as(f32, 1.1)), result[0], epsilon)); | ||
| 123 | try expect(math.approxEqAbs(f32, @cos(@as(f32, 2.2)), result[1], epsilon)); | ||
| 124 | try expect(math.approxEqAbs(f32, @cos(@as(f32, 3.3)), result[2], epsilon)); | ||
| 125 | try expect(math.approxEqAbs(f32, @cos(@as(f32, 4.4)), result[3], epsilon)); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | test "@exp" { | ||
| 130 | comptime try testExp(); | ||
| 131 | try testExp(); | ||
| 132 | } | ||
| 133 | |||
| 134 | fn testExp() !void { | ||
| 135 | // TODO test f128, and c_longdouble | ||
| 136 | // https://github.com/ziglang/zig/issues/4026 | ||
| 137 | { | ||
| 138 | var a: f16 = 0; | ||
| 139 | try expect(@exp(a) == 1); | ||
| 140 | } | ||
| 141 | { | ||
| 142 | var a: f32 = 0; | ||
| 143 | try expect(@exp(a) == 1); | ||
| 144 | } | ||
| 145 | { | ||
| 146 | var a: f64 = 0; | ||
| 147 | try expect(@exp(a) == 1); | ||
| 148 | } | ||
| 149 | { | ||
| 150 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 151 | var result = @exp(v); | ||
| 152 | try expect(math.approxEqAbs(f32, @exp(@as(f32, 1.1)), result[0], epsilon)); | ||
| 153 | try expect(math.approxEqAbs(f32, @exp(@as(f32, 2.2)), result[1], epsilon)); | ||
| 154 | try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.3)), result[2], epsilon)); | ||
| 155 | try expect(math.approxEqAbs(f32, @exp(@as(f32, 0.4)), result[3], epsilon)); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | test "@exp2" { | ||
| 160 | comptime try testExp2(); | ||
| 161 | try testExp2(); | ||
| 162 | } | ||
| 163 | |||
| 164 | fn testExp2() !void { | ||
| 165 | // TODO test f128, and c_longdouble | ||
| 166 | // https://github.com/ziglang/zig/issues/4026 | ||
| 167 | { | ||
| 168 | var a: f16 = 2; | ||
| 169 | try expect(@exp2(a) == 4); | ||
| 170 | } | ||
| 171 | { | ||
| 172 | var a: f32 = 2; | ||
| 173 | try expect(@exp2(a) == 4); | ||
| 174 | } | ||
| 175 | { | ||
| 176 | var a: f64 = 2; | ||
| 177 | try expect(@exp2(a) == 4); | ||
| 178 | } | ||
| 179 | { | ||
| 180 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 181 | var result = @exp2(v); | ||
| 182 | try expect(math.approxEqAbs(f32, @exp2(@as(f32, 1.1)), result[0], epsilon)); | ||
| 183 | try expect(math.approxEqAbs(f32, @exp2(@as(f32, 2.2)), result[1], epsilon)); | ||
| 184 | try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.3)), result[2], epsilon)); | ||
| 185 | try expect(math.approxEqAbs(f32, @exp2(@as(f32, 0.4)), result[3], epsilon)); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | test "@log" { | ||
| 190 | // Old musl (and glibc?), and our current math.ln implementation do not return 1 | ||
| 191 | // so also accept those values. | ||
| 192 | comptime try testLog(); | ||
| 193 | try testLog(); | ||
| 194 | } | ||
| 195 | |||
| 196 | fn testLog() !void { | ||
| 197 | // TODO test f128, and c_longdouble | ||
| 198 | // https://github.com/ziglang/zig/issues/4026 | ||
| 199 | { | ||
| 200 | var a: f16 = e; | ||
| 201 | try expect(math.approxEqAbs(f16, @log(a), 1, epsilon)); | ||
| 202 | } | ||
| 203 | { | ||
| 204 | var a: f32 = e; | ||
| 205 | try expect(@log(a) == 1 or @log(a) == @bitCast(f32, @as(u32, 0x3f7fffff))); | ||
| 206 | } | ||
| 207 | { | ||
| 208 | var a: f64 = e; | ||
| 209 | try expect(@log(a) == 1 or @log(a) == @bitCast(f64, @as(u64, 0x3ff0000000000000))); | ||
| 210 | } | ||
| 211 | { | ||
| 212 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 213 | var result = @log(v); | ||
| 214 | try expect(math.approxEqAbs(f32, @log(@as(f32, 1.1)), result[0], epsilon)); | ||
| 215 | try expect(math.approxEqAbs(f32, @log(@as(f32, 2.2)), result[1], epsilon)); | ||
| 216 | try expect(math.approxEqAbs(f32, @log(@as(f32, 0.3)), result[2], epsilon)); | ||
| 217 | try expect(math.approxEqAbs(f32, @log(@as(f32, 0.4)), result[3], epsilon)); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | test "@log2" { | ||
| 222 | comptime try testLog2(); | ||
| 223 | try testLog2(); | ||
| 224 | } | ||
| 225 | |||
| 226 | fn testLog2() !void { | ||
| 227 | // TODO test f128, and c_longdouble | ||
| 228 | // https://github.com/ziglang/zig/issues/4026 | ||
| 229 | { | ||
| 230 | var a: f16 = 4; | ||
| 231 | try expect(@log2(a) == 2); | ||
| 232 | } | ||
| 233 | { | ||
| 234 | var a: f32 = 4; | ||
| 235 | try expect(@log2(a) == 2); | ||
| 236 | } | ||
| 237 | { | ||
| 238 | var a: f64 = 4; | ||
| 239 | try expect(@log2(a) == 2); | ||
| 240 | } | ||
| 241 | { | ||
| 242 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 243 | var result = @log2(v); | ||
| 244 | try expect(math.approxEqAbs(f32, @log2(@as(f32, 1.1)), result[0], epsilon)); | ||
| 245 | try expect(math.approxEqAbs(f32, @log2(@as(f32, 2.2)), result[1], epsilon)); | ||
| 246 | try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.3)), result[2], epsilon)); | ||
| 247 | try expect(math.approxEqAbs(f32, @log2(@as(f32, 0.4)), result[3], epsilon)); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | test "@log10" { | ||
| 252 | comptime try testLog10(); | ||
| 253 | try testLog10(); | ||
| 254 | } | ||
| 255 | |||
| 256 | fn testLog10() !void { | ||
| 257 | // TODO test f128, and c_longdouble | ||
| 258 | // https://github.com/ziglang/zig/issues/4026 | ||
| 259 | { | ||
| 260 | var a: f16 = 100; | ||
| 261 | try expect(@log10(a) == 2); | ||
| 262 | } | ||
| 263 | { | ||
| 264 | var a: f32 = 100; | ||
| 265 | try expect(@log10(a) == 2); | ||
| 266 | } | ||
| 267 | { | ||
| 268 | var a: f64 = 1000; | ||
| 269 | try expect(@log10(a) == 3); | ||
| 270 | } | ||
| 271 | { | ||
| 272 | var v: Vector(4, f32) = [_]f32{ 1.1, 2.2, 0.3, 0.4 }; | ||
| 273 | var result = @log10(v); | ||
| 274 | try expect(math.approxEqAbs(f32, @log10(@as(f32, 1.1)), result[0], epsilon)); | ||
| 275 | try expect(math.approxEqAbs(f32, @log10(@as(f32, 2.2)), result[1], epsilon)); | ||
| 276 | try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.3)), result[2], epsilon)); | ||
| 277 | try expect(math.approxEqAbs(f32, @log10(@as(f32, 0.4)), result[3], epsilon)); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | test "@fabs" { | ||
| 282 | comptime try testFabs(); | ||
| 283 | try testFabs(); | ||
| 284 | } | ||
| 285 | |||
| 286 | fn testFabs() !void { | ||
| 287 | // TODO test f128, and c_longdouble | ||
| 288 | // https://github.com/ziglang/zig/issues/4026 | ||
| 289 | { | ||
| 290 | var a: f16 = -2.5; | ||
| 291 | var b: f16 = 2.5; | ||
| 292 | try expect(@fabs(a) == 2.5); | ||
| 293 | try expect(@fabs(b) == 2.5); | ||
| 294 | } | ||
| 295 | { | ||
| 296 | var a: f32 = -2.5; | ||
| 297 | var b: f32 = 2.5; | ||
| 298 | try expect(@fabs(a) == 2.5); | ||
| 299 | try expect(@fabs(b) == 2.5); | ||
| 300 | } | ||
| 301 | { | ||
| 302 | var a: f64 = -2.5; | ||
| 303 | var b: f64 = 2.5; | ||
| 304 | try expect(@fabs(a) == 2.5); | ||
| 305 | try expect(@fabs(b) == 2.5); | ||
| 306 | } | ||
| 307 | { | ||
| 308 | var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 }; | ||
| 309 | var result = @fabs(v); | ||
| 310 | try expect(math.approxEqAbs(f32, @fabs(@as(f32, 1.1)), result[0], epsilon)); | ||
| 311 | try expect(math.approxEqAbs(f32, @fabs(@as(f32, -2.2)), result[1], epsilon)); | ||
| 312 | try expect(math.approxEqAbs(f32, @fabs(@as(f32, 0.3)), result[2], epsilon)); | ||
| 313 | try expect(math.approxEqAbs(f32, @fabs(@as(f32, -0.4)), result[3], epsilon)); | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | test "@floor" { | ||
| 318 | comptime try testFloor(); | ||
| 319 | try testFloor(); | ||
| 320 | } | ||
| 321 | |||
| 322 | fn testFloor() !void { | ||
| 323 | // TODO test f128, and c_longdouble | ||
| 324 | // https://github.com/ziglang/zig/issues/4026 | ||
| 325 | { | ||
| 326 | var a: f16 = 2.1; | ||
| 327 | try expect(@floor(a) == 2); | ||
| 328 | } | ||
| 329 | { | ||
| 330 | var a: f32 = 2.1; | ||
| 331 | try expect(@floor(a) == 2); | ||
| 332 | } | ||
| 333 | { | ||
| 334 | var a: f64 = 3.5; | ||
| 335 | try expect(@floor(a) == 3); | ||
| 336 | } | ||
| 337 | { | ||
| 338 | var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 }; | ||
| 339 | var result = @floor(v); | ||
| 340 | try expect(math.approxEqAbs(f32, @floor(@as(f32, 1.1)), result[0], epsilon)); | ||
| 341 | try expect(math.approxEqAbs(f32, @floor(@as(f32, -2.2)), result[1], epsilon)); | ||
| 342 | try expect(math.approxEqAbs(f32, @floor(@as(f32, 0.3)), result[2], epsilon)); | ||
| 343 | try expect(math.approxEqAbs(f32, @floor(@as(f32, -0.4)), result[3], epsilon)); | ||
| 344 | } | ||
| 345 | } | ||
| 346 | |||
| 347 | test "@ceil" { | ||
| 348 | comptime try testCeil(); | ||
| 349 | try testCeil(); | ||
| 350 | } | ||
| 351 | |||
| 352 | fn testCeil() !void { | ||
| 353 | // TODO test f128, and c_longdouble | ||
| 354 | // https://github.com/ziglang/zig/issues/4026 | ||
| 355 | { | ||
| 356 | var a: f16 = 2.1; | ||
| 357 | try expect(@ceil(a) == 3); | ||
| 358 | } | ||
| 359 | { | ||
| 360 | var a: f32 = 2.1; | ||
| 361 | try expect(@ceil(a) == 3); | ||
| 362 | } | ||
| 363 | { | ||
| 364 | var a: f64 = 3.5; | ||
| 365 | try expect(@ceil(a) == 4); | ||
| 366 | } | ||
| 367 | { | ||
| 368 | var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 }; | ||
| 369 | var result = @ceil(v); | ||
| 370 | try expect(math.approxEqAbs(f32, @ceil(@as(f32, 1.1)), result[0], epsilon)); | ||
| 371 | try expect(math.approxEqAbs(f32, @ceil(@as(f32, -2.2)), result[1], epsilon)); | ||
| 372 | try expect(math.approxEqAbs(f32, @ceil(@as(f32, 0.3)), result[2], epsilon)); | ||
| 373 | try expect(math.approxEqAbs(f32, @ceil(@as(f32, -0.4)), result[3], epsilon)); | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | test "@trunc" { | ||
| 378 | comptime try testTrunc(); | ||
| 379 | try testTrunc(); | ||
| 380 | } | ||
| 381 | |||
| 382 | fn testTrunc() !void { | ||
| 383 | // TODO test f128, and c_longdouble | ||
| 384 | // https://github.com/ziglang/zig/issues/4026 | ||
| 385 | { | ||
| 386 | var a: f16 = 2.1; | ||
| 387 | try expect(@trunc(a) == 2); | ||
| 388 | } | ||
| 389 | { | ||
| 390 | var a: f32 = 2.1; | ||
| 391 | try expect(@trunc(a) == 2); | ||
| 392 | } | ||
| 393 | { | ||
| 394 | var a: f64 = -3.5; | ||
| 395 | try expect(@trunc(a) == -3); | ||
| 396 | } | ||
| 397 | { | ||
| 398 | var v: Vector(4, f32) = [_]f32{ 1.1, -2.2, 0.3, -0.4 }; | ||
| 399 | var result = @trunc(v); | ||
| 400 | try expect(math.approxEqAbs(f32, @trunc(@as(f32, 1.1)), result[0], epsilon)); | ||
| 401 | try expect(math.approxEqAbs(f32, @trunc(@as(f32, -2.2)), result[1], epsilon)); | ||
| 402 | try expect(math.approxEqAbs(f32, @trunc(@as(f32, 0.3)), result[2], epsilon)); | ||
| 403 | try expect(math.approxEqAbs(f32, @trunc(@as(f32, -0.4)), result[3], epsilon)); | ||
| 404 | } | ||
| 405 | } | ||
test/behavior/for.zig+58| ... | @@ -2,3 +2,61 @@ const std = @import("std"); | ... | @@ -2,3 +2,61 @@ const std = @import("std"); |
| 2 | const expect = std.testing.expect; | 2 | const expect = std.testing.expect; |
| 3 | const expectEqual = std.testing.expectEqual; | 3 | const expectEqual = std.testing.expectEqual; |
| 4 | const mem = std.mem; | 4 | const mem = std.mem; |
| 5 | |||
| 6 | test "continue in for loop" { | ||
| 7 | const array = [_]i32{ 1, 2, 3, 4, 5 }; | ||
| 8 | var sum: i32 = 0; | ||
| 9 | for (array) |x| { | ||
| 10 | sum += x; | ||
| 11 | if (x < 3) { | ||
| 12 | continue; | ||
| 13 | } | ||
| 14 | break; | ||
| 15 | } | ||
| 16 | if (sum != 6) unreachable; | ||
| 17 | } | ||
| 18 | |||
| 19 | test "break from outer for loop" { | ||
| 20 | try testBreakOuter(); | ||
| 21 | comptime try testBreakOuter(); | ||
| 22 | } | ||
| 23 | |||
| 24 | fn testBreakOuter() !void { | ||
| 25 | var array = "aoeu"; | ||
| 26 | var count: usize = 0; | ||
| 27 | outer: for (array) |_| { | ||
| 28 | for (array) |_| { | ||
| 29 | count += 1; | ||
| 30 | break :outer; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | try expect(count == 1); | ||
| 34 | } | ||
| 35 | |||
| 36 | test "continue outer for loop" { | ||
| 37 | try testContinueOuter(); | ||
| 38 | comptime try testContinueOuter(); | ||
| 39 | } | ||
| 40 | |||
| 41 | fn testContinueOuter() !void { | ||
| 42 | var array = "aoeu"; | ||
| 43 | var counter: usize = 0; | ||
| 44 | outer: for (array) |_| { | ||
| 45 | for (array) |_| { | ||
| 46 | counter += 1; | ||
| 47 | continue :outer; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | try expect(counter == array.len); | ||
| 51 | } | ||
| 52 | |||
| 53 | test "ignore lval with underscore (for loop)" { | ||
| 54 | for ([_]void{}) |_, i| { | ||
| 55 | _ = i; | ||
| 56 | for ([_]void{}) |_, j| { | ||
| 57 | _ = j; | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | break; | ||
| 61 | } | ||
| 62 | } |
test/behavior/for_stage1.zig-58| ... | @@ -3,19 +3,6 @@ const expect = std.testing.expect; | ... | @@ -3,19 +3,6 @@ const expect = std.testing.expect; |
| 3 | const expectEqual = std.testing.expectEqual; | 3 | const expectEqual = std.testing.expectEqual; |
| 4 | const mem = std.mem; | 4 | const mem = std.mem; |
| 5 | 5 | ||
| 6 | test "continue in for loop" { | ||
| 7 | const array = [_]i32{ 1, 2, 3, 4, 5 }; | ||
| 8 | var sum: i32 = 0; | ||
| 9 | for (array) |x| { | ||
| 10 | sum += x; | ||
| 11 | if (x < 3) { | ||
| 12 | continue; | ||
| 13 | } | ||
| 14 | break; | ||
| 15 | } | ||
| 16 | if (sum != 6) unreachable; | ||
| 17 | } | ||
| 18 | |||
| 19 | test "for loop with pointer elem var" { | 6 | test "for loop with pointer elem var" { |
| 20 | const source = "abcdefg"; | 7 | const source = "abcdefg"; |
| 21 | var target: [source.len]u8 = undefined; | 8 | var target: [source.len]u8 = undefined; |
| ... | @@ -78,40 +65,6 @@ test "basic for loop" { | ... | @@ -78,40 +65,6 @@ test "basic for loop" { |
| 78 | try expect(mem.eql(u8, buffer[0..buf_index], &expected_result)); | 65 | try expect(mem.eql(u8, buffer[0..buf_index], &expected_result)); |
| 79 | } | 66 | } |
| 80 | 67 | ||
| 81 | test "break from outer for loop" { | ||
| 82 | try testBreakOuter(); | ||
| 83 | comptime try testBreakOuter(); | ||
| 84 | } | ||
| 85 | |||
| 86 | fn testBreakOuter() !void { | ||
| 87 | var array = "aoeu"; | ||
| 88 | var count: usize = 0; | ||
| 89 | outer: for (array) |_| { | ||
| 90 | for (array) |_| { | ||
| 91 | count += 1; | ||
| 92 | break :outer; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | try expect(count == 1); | ||
| 96 | } | ||
| 97 | |||
| 98 | test "continue outer for loop" { | ||
| 99 | try testContinueOuter(); | ||
| 100 | comptime try testContinueOuter(); | ||
| 101 | } | ||
| 102 | |||
| 103 | fn testContinueOuter() !void { | ||
| 104 | var array = "aoeu"; | ||
| 105 | var counter: usize = 0; | ||
| 106 | outer: for (array) |_| { | ||
| 107 | for (array) |_| { | ||
| 108 | counter += 1; | ||
| 109 | continue :outer; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | try expect(counter == array.len); | ||
| 113 | } | ||
| 114 | |||
| 115 | test "2 break statements and an else" { | 68 | test "2 break statements and an else" { |
| 116 | const S = struct { | 69 | const S = struct { |
| 117 | fn entry(t: bool, f: bool) !void { | 70 | fn entry(t: bool, f: bool) !void { |
| ... | @@ -172,14 +125,3 @@ test "for on slice with allowzero ptr" { | ... | @@ -172,14 +125,3 @@ test "for on slice with allowzero ptr" { |
| 172 | try S.doTheTest(&[_]u8{ 1, 2, 3, 4 }); | 125 | try S.doTheTest(&[_]u8{ 1, 2, 3, 4 }); |
| 173 | comptime try S.doTheTest(&[_]u8{ 1, 2, 3, 4 }); | 126 | comptime try S.doTheTest(&[_]u8{ 1, 2, 3, 4 }); |
| 174 | } | 127 | } |
| 175 | |||
| 176 | test "ignore lval with underscore (for loop)" { | ||
| 177 | for ([_]void{}) |_, i| { | ||
| 178 | _ = i; | ||
| 179 | for ([_]void{}) |_, j| { | ||
| 180 | _ = j; | ||
| 181 | break; | ||
| 182 | } | ||
| 183 | break; | ||
| 184 | } | ||
| 185 | } |