Initial public release
This commit is contained in:
		
							parent
							
								
									3c3f068914
								
							
						
					
					
						commit
						4ece4babad
					
				
					 63 changed files with 15101 additions and 0 deletions
				
			
		
							
								
								
									
										7
									
								
								test/tests.zig
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								test/tests.zig
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,7 @@ | |||
| const std = @import("std"); | ||||
| pub const buffer = @import("tests_buffer.zig"); | ||||
| pub const color = @import("tests_color.zig"); | ||||
| 
 | ||||
| test { | ||||
|     std.testing.refAllDecls(@This()); | ||||
| } | ||||
							
								
								
									
										279
									
								
								test/tests_buffer.zig
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										279
									
								
								test/tests_buffer.zig
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,279 @@ | |||
| const std = @import("std"); | ||||
| const Buffer = @import("Buffer"); | ||||
| 
 | ||||
| const ArrayList = std.ArrayList; | ||||
| const a = std.testing.allocator; | ||||
| 
 | ||||
| fn get_big_doc() !*Buffer { | ||||
|     const BigDocGen = struct { | ||||
|         line_num: usize = 0, | ||||
|         lines: usize = 10000, | ||||
| 
 | ||||
|         buf: [128]u8 = undefined, | ||||
|         line_buf: []u8 = "", | ||||
|         read_count: usize = 0, | ||||
| 
 | ||||
|         const Self = @This(); | ||||
|         const Reader = std.io.Reader(*Self, Err, read); | ||||
|         const Err = error{NoSpaceLeft}; | ||||
| 
 | ||||
|         fn gen_line(self: *Self) Err!void { | ||||
|             var stream = std.io.fixedBufferStream(&self.buf); | ||||
|             const writer = stream.writer(); | ||||
|             try writer.print("this is line {d}\n", .{self.line_num}); | ||||
|             self.line_buf = stream.getWritten(); | ||||
|             self.read_count = 0; | ||||
|             self.line_num += 1; | ||||
|         } | ||||
| 
 | ||||
|         fn read(self: *Self, buffer: []u8) Err!usize { | ||||
|             if (self.line_num > self.lines) | ||||
|                 return 0; | ||||
|             if (self.line_buf.len == 0 or self.line_buf.len - self.read_count == 0) | ||||
|                 try self.gen_line(); | ||||
|             const read_count = self.read_count; | ||||
|             const bytes_to_read = @min(self.line_buf.len - read_count, buffer.len); | ||||
|             @memcpy(buffer[0..bytes_to_read], self.line_buf[read_count .. read_count + bytes_to_read]); | ||||
|             self.read_count += bytes_to_read; | ||||
|             return bytes_to_read; | ||||
|         } | ||||
| 
 | ||||
|         fn reader(self: *Self) Reader { | ||||
|             return .{ .context = self }; | ||||
|         } | ||||
|     }; | ||||
|     var gen: BigDocGen = .{}; | ||||
|     var doc = ArrayList(u8).init(a); | ||||
|     defer doc.deinit(); | ||||
|     try gen.reader().readAllArrayList(&doc, std.math.maxInt(usize)); | ||||
|     var buf = try Buffer.create(a); | ||||
|     var fis = std.io.fixedBufferStream(doc.items); | ||||
|     buf.update(try buf.load(fis.reader(), doc.items.len)); | ||||
|     return buf; | ||||
| } | ||||
| 
 | ||||
| test "buffer" { | ||||
|     const doc: []const u8 = | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|     ; | ||||
|     const buffer = try Buffer.create(a); | ||||
|     defer buffer.deinit(); | ||||
|     const root = try buffer.load_from_string(doc); | ||||
| 
 | ||||
|     try std.testing.expect(root.is_balanced()); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const result: []const u8 = try buffer.store_to_string(a); | ||||
|     defer a.free(result); | ||||
|     try std.testing.expectEqualDeep(result, doc); | ||||
|     try std.testing.expectEqual(doc.len, result.len); | ||||
|     try std.testing.expectEqual(doc.len, buffer.root.length()); | ||||
| } | ||||
| 
 | ||||
| fn get_line(buf: *const Buffer, line: usize) ![]const u8 { | ||||
|     var result = ArrayList(u8).init(a); | ||||
|     try buf.root.get_line(line, &result); | ||||
|     return result.toOwnedSlice(); | ||||
| } | ||||
| 
 | ||||
| test "walk_from_line" { | ||||
|     const buffer = try get_big_doc(); | ||||
|     defer buffer.deinit(); | ||||
| 
 | ||||
|     const lines = buffer.root.lines(); | ||||
|     try std.testing.expectEqual(lines, 10002); | ||||
| 
 | ||||
|     const line0 = try get_line(buffer, 0); | ||||
|     defer a.free(line0); | ||||
|     try std.testing.expect(std.mem.eql(u8, line0, "this is line 0")); | ||||
| 
 | ||||
|     const line1 = try get_line(buffer, 1); | ||||
|     defer a.free(line1); | ||||
|     try std.testing.expect(std.mem.eql(u8, line1, "this is line 1")); | ||||
| 
 | ||||
|     const line100 = try get_line(buffer, 100); | ||||
|     defer a.free(line100); | ||||
|     try std.testing.expect(std.mem.eql(u8, line100, "this is line 100")); | ||||
| 
 | ||||
|     const line9999 = try get_line(buffer, 9999); | ||||
|     defer a.free(line9999); | ||||
|     try std.testing.expect(std.mem.eql(u8, line9999, "this is line 9999")); | ||||
| } | ||||
| 
 | ||||
| test "line_len" { | ||||
|     const doc: []const u8 = | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|     ; | ||||
|     const buffer = try Buffer.create(a); | ||||
|     defer buffer.deinit(); | ||||
|     buffer.update(try buffer.load_from_string(doc)); | ||||
| 
 | ||||
|     try std.testing.expectEqual(try buffer.root.line_width(0), 8); | ||||
|     try std.testing.expectEqual(try buffer.root.line_width(1), 5); | ||||
| } | ||||
| 
 | ||||
| test "del_chars" { | ||||
|     const doc: []const u8 = | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|     ; | ||||
|     const buffer = try Buffer.create(a); | ||||
|     defer buffer.deinit(); | ||||
|     buffer.update(try buffer.load_from_string(doc)); | ||||
| 
 | ||||
|     buffer.update(try buffer.root.del_chars(3, try buffer.root.line_width(3) - 1, 1, buffer.a)); | ||||
|     const line3 = try get_line(buffer, 3); | ||||
|     defer a.free(line3); | ||||
|     try std.testing.expect(std.mem.eql(u8, line3, "us")); | ||||
| 
 | ||||
|     buffer.update(try buffer.root.del_chars(3, 0, 7, buffer.a)); | ||||
|     const line3_1 = try get_line(buffer, 3); | ||||
|     defer a.free(line3_1); | ||||
|     try std.testing.expect(std.mem.eql(u8, line3_1, "your")); | ||||
| 
 | ||||
|     // try buffer.rebalance(); | ||||
|     // try std.testing.expect(buffer.is_balanced()); | ||||
| 
 | ||||
|     buffer.update(try buffer.root.del_chars(0, try buffer.root.line_width(0) - 1, 2, buffer.a)); | ||||
|     const line0 = try get_line(buffer, 0); | ||||
|     defer a.free(line0); | ||||
|     try std.testing.expect(std.mem.eql(u8, line0, "All youropes")); | ||||
| } | ||||
| 
 | ||||
| fn check_line(buffer: *const Buffer, line_no: usize, expect: []const u8) !void { | ||||
|     const line = try get_line(buffer, line_no); | ||||
|     defer a.free(line); | ||||
|     try std.testing.expect(std.mem.eql(u8, line, expect)); | ||||
| } | ||||
| 
 | ||||
| test "del_chars2" { | ||||
|     const doc: []const u8 = | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|         \\All your | ||||
|         \\ropes | ||||
|         \\are belong to | ||||
|         \\us! | ||||
|     ; | ||||
|     const buffer = try Buffer.create(a); | ||||
|     defer buffer.deinit(); | ||||
|     buffer.update(try buffer.load_from_string(doc)); | ||||
| 
 | ||||
|     buffer.update(try buffer.root.del_chars(2, try buffer.root.line_width(2) - 3, 6, buffer.a)); | ||||
| 
 | ||||
|     try check_line(buffer, 2, "are belong!"); | ||||
|     try check_line(buffer, 3, "All your"); | ||||
|     try check_line(buffer, 4, "ropes"); | ||||
| } | ||||
| 
 | ||||
| test "insert_chars" { | ||||
|     const doc: []const u8 = | ||||
|         \\B | ||||
|     ; | ||||
|     const buffer = try Buffer.create(a); | ||||
|     defer buffer.deinit(); | ||||
|     buffer.update(try buffer.load_from_string(doc)); | ||||
| 
 | ||||
|     const line0 = try get_line(buffer, 0); | ||||
|     defer a.free(line0); | ||||
|     try std.testing.expect(std.mem.eql(u8, line0, "B")); | ||||
| 
 | ||||
|     _, _, const root = try buffer.root.insert_chars(0, 0, "1", buffer.a); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const line1 = try get_line(buffer, 0); | ||||
|     defer a.free(line1); | ||||
|     try std.testing.expect(std.mem.eql(u8, line1, "1B")); | ||||
| 
 | ||||
|     _, _, root = try root.insert_chars(0, 1, "2", buffer.a); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const line2 = try get_line(buffer, 0); | ||||
|     defer a.free(line2); | ||||
|     try std.testing.expect(std.mem.eql(u8, line2, "12B")); | ||||
| 
 | ||||
|     _, _, root = try root.insert_chars(0, 2, "3", buffer.a); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const line3 = try get_line(buffer, 0); | ||||
|     defer a.free(line3); | ||||
|     try std.testing.expect(std.mem.eql(u8, line3, "123B")); | ||||
| 
 | ||||
|     _, _, root = try root.insert_chars(0, 3, "4", buffer.a); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const line4 = try get_line(buffer, 0); | ||||
|     defer a.free(line4); | ||||
|     try std.testing.expect(std.mem.eql(u8, line4, "1234B")); | ||||
| 
 | ||||
|     _, _, root = try root.insert_chars(0, 4, "5", buffer.a); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const line5 = try get_line(buffer, 0); | ||||
|     defer a.free(line5); | ||||
|     try std.testing.expect(std.mem.eql(u8, line5, "12345B")); | ||||
| 
 | ||||
|     _, _, root = try root.insert_chars(0, 5, "6", buffer.a); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const line6 = try get_line(buffer, 0); | ||||
|     defer a.free(line6); | ||||
|     try std.testing.expect(std.mem.eql(u8, line6, "123456B")); | ||||
| 
 | ||||
|     _, _, root = try root.insert_chars(0, 6, "7", buffer.a); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const line7 = try get_line(buffer, 0); | ||||
|     defer a.free(line7); | ||||
|     try std.testing.expect(std.mem.eql(u8, line7, "1234567B")); | ||||
| 
 | ||||
|     const line, const col, root = try buffer.root.insert_chars(0, 7, "8\n9", buffer.a); | ||||
|     buffer.update(root); | ||||
| 
 | ||||
|     const line8 = try get_line(buffer, 0); | ||||
|     defer a.free(line8); | ||||
|     const line9 = try get_line(buffer, 1); | ||||
|     defer a.free(line9); | ||||
|     try std.testing.expect(std.mem.eql(u8, line8, "12345678")); | ||||
|     try std.testing.expect(std.mem.eql(u8, line9, "9B")); | ||||
|     try std.testing.expectEqual(line, 1); | ||||
|     try std.testing.expectEqual(col, 1); | ||||
| } | ||||
							
								
								
									
										43
									
								
								test/tests_color.zig
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								test/tests_color.zig
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,43 @@ | |||
| const std = @import("std"); | ||||
| const color = @import("color"); | ||||
| 
 | ||||
| const RGB = color.RGB; | ||||
| const rgb = RGB.from_u24; | ||||
| 
 | ||||
| test "contrast white/yellow" { | ||||
|     const a: u24 = 0xFFFFFF; // white | ||||
|     const b: u24 = 0x00FFFF; // yellow | ||||
|     const ratio = RGB.contrast(rgb(a), rgb(b)); | ||||
|     try std.testing.expectApproxEqAbs(ratio, 1.25388109, 0.000001); | ||||
| } | ||||
| 
 | ||||
| test "contrast white/blue" { | ||||
|     const a: u24 = 0xFFFFFF; // white | ||||
|     const b: u24 = 0x0000FF; // blue | ||||
|     const ratio = RGB.contrast(rgb(a), rgb(b)); | ||||
|     try std.testing.expectApproxEqAbs(ratio, 8.59247135, 0.000001); | ||||
| } | ||||
| 
 | ||||
| test "contrast black/yellow" { | ||||
|     const a: u24 = 0x000000; // black | ||||
|     const b: u24 = 0x00FFFF; // yellow | ||||
|     const ratio = RGB.contrast(rgb(a), rgb(b)); | ||||
|     try std.testing.expectApproxEqAbs(ratio, 16.7479991, 0.000001); | ||||
| } | ||||
| 
 | ||||
| test "contrast black/blue" { | ||||
|     const a: u24 = 0x000000; // black | ||||
|     const b: u24 = 0x0000FF; // blue | ||||
|     const ratio = RGB.contrast(rgb(a), rgb(b)); | ||||
|     try std.testing.expectApproxEqAbs(ratio, 2.444, 0.000001); | ||||
| } | ||||
| 
 | ||||
| test "best contrast black/white to yellow" { | ||||
|     const best = color.max_contrast(0x00FFFF, 0xFFFFFF, 0x000000); | ||||
|     try std.testing.expectEqual(best, 0x000000); | ||||
| } | ||||
| 
 | ||||
| test "best contrast black/white to blue" { | ||||
|     const best = color.max_contrast(0x0000FF, 0xFFFFFF, 0x000000); | ||||
|     try std.testing.expectEqual(best, 0xFFFFFF); | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue