| ... | @@ -222,6 +222,35 @@ test "stable sort" { | ... | @@ -222,6 +222,35 @@ test "stable sort" { |
| 222 | } | 222 | } |
| 223 | } | 223 | } |
| 224 | | 224 | |
| | 225 | test "stable sort fuzz testing" { |
| | 226 | var prng = std.Random.DefaultPrng.init(0x12345678); |
| | 227 | const random = prng.random(); |
| | 228 | const test_case_count = 10; |
| | 229 | |
| | 230 | for (0..test_case_count) |_| { |
| | 231 | const array_size = random.intRangeLessThan(usize, 0, 1000); |
| | 232 | const array = try testing.allocator.alloc(IdAndValue, array_size); |
| | 233 | defer testing.allocator.free(array); |
| | 234 | // Value is a small random numbers to create collisions. |
| | 235 | // Id is a reverse index to make sure sorting function only uses provided `lessThan`. |
| | 236 | for (array, 0..) |*item, index| { |
| | 237 | item.* = .{ |
| | 238 | .value = random.intRangeLessThan(i32, 0, 100), |
| | 239 | .id = array_size - index, |
| | 240 | }; |
| | 241 | } |
| | 242 | block(IdAndValue, array, {}, IdAndValue.lessThan); |
| | 243 | if (array_size > 0) { |
| | 244 | for (array[0 .. array_size - 1], array[1..]) |x, y| { |
| | 245 | try testing.expect(x.value <= y.value); |
| | 246 | if (x.value == y.value) { |
| | 247 | try testing.expect(x.id > y.id); |
| | 248 | } |
| | 249 | } |
| | 250 | } |
| | 251 | } |
| | 252 | } |
| | 253 | |
| 225 | test "sort" { | 254 | test "sort" { |
| 226 | const u8cases = [_][]const []const u8{ | 255 | const u8cases = [_][]const []const u8{ |
| 227 | &[_][]const u8{ | 256 | &[_][]const u8{ |
| ... | @@ -384,8 +413,7 @@ test "sort fuzz testing" { | ... | @@ -384,8 +413,7 @@ test "sort fuzz testing" { |
| 384 | const test_case_count = 10; | 413 | const test_case_count = 10; |
| 385 | | 414 | |
| 386 | inline for (sort_funcs) |sortFn| { | 415 | inline for (sort_funcs) |sortFn| { |
| 387 | var i: usize = 0; | 416 | for (0..test_case_count) |_| { |
| 388 | while (i < test_case_count) : (i += 1) { | | |
| 389 | const array_size = random.intRangeLessThan(usize, 0, 1000); | 417 | const array_size = random.intRangeLessThan(usize, 0, 1000); |
| 390 | const array = try testing.allocator.alloc(i32, array_size); | 418 | const array = try testing.allocator.alloc(i32, array_size); |
| 391 | defer testing.allocator.free(array); | 419 | defer testing.allocator.free(array); |