| ... | ... | @@ -186,6 +186,43 @@ test "Dir.Iterator" { |
| 186 | 186 | try testing.expect(contains(&entries, .{ .name = "some_dir", .kind = .Directory })); |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | test "Dir.Iterator many entries" { |
| 190 | if (builtin.os.tag == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/"); |
| 191 | |
| 192 | var tmp_dir = tmpIterableDir(.{}); |
| 193 | defer tmp_dir.cleanup(); |
| 194 | |
| 195 | const num = 1024; |
| 196 | var i: usize = 0; |
| 197 | var buf: [4]u8 = undefined; // Enough to store "1024". |
| 198 | while (i < num) : (i += 1) { |
| 199 | const name = try std.fmt.bufPrint(&buf, "{}", .{i}); |
| 200 | const file = try tmp_dir.iterable_dir.dir.createFile(name, .{}); |
| 201 | file.close(); |
| 202 | } |
| 203 | |
| 204 | var arena = ArenaAllocator.init(testing.allocator); |
| 205 | defer arena.deinit(); |
| 206 | const allocator = arena.allocator(); |
| 207 | |
| 208 | var entries = std.ArrayList(IterableDir.Entry).init(allocator); |
| 209 | |
| 210 | // Create iterator. |
| 211 | var iter = tmp_dir.iterable_dir.iterate(); |
| 212 | while (try iter.next()) |entry| { |
| 213 | // We cannot just store `entry` as on Windows, we're re-using the name buffer |
| 214 | // which means we'll actually share the `name` pointer between entries! |
| 215 | const name = try allocator.dupe(u8, entry.name); |
| 216 | try entries.append(.{ .name = name, .kind = entry.kind }); |
| 217 | } |
| 218 | |
| 219 | i = 0; |
| 220 | while (i < num) : (i += 1) { |
| 221 | const name = try std.fmt.bufPrint(&buf, "{}", .{i}); |
| 222 | try testing.expect(contains(&entries, .{ .name = name, .kind = .File })); |
| 223 | } |
| 224 | } |
| 225 | |
| 189 | 226 | test "Dir.Iterator twice" { |
| 190 | 227 | var tmp_dir = tmpIterableDir(.{}); |
| 191 | 228 | defer tmp_dir.cleanup(); |