Skip to content

Commit e75703a

Browse files
committed
test: Add tests for float types other than f64
1 parent 831a4ca commit e75703a

File tree

7 files changed

+361
-294
lines changed

7 files changed

+361
-294
lines changed

src/color.zig

Lines changed: 86 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,10 @@ pub fn Color(comptime T: type) type {
6565

6666
test init {
6767
const color = Color(f64).init(1.23, 0.5, -0.01, 1.01);
68-
try testing.expectEqualSlices(f64, &[4]f64{ 1.0, 0.5, 0.0, 1.0 }, &[4]f64{
69-
color.red,
70-
color.green,
71-
color.blue,
72-
color.alpha,
73-
});
68+
try testing.expectEqual(
69+
.{ 1.0, 0.5, 0.0, 1.0 },
70+
.{ color.red, color.green, color.blue, color.alpha },
71+
);
7472
}
7573

7674
/// Restricts the values to the valid range.
@@ -84,12 +82,10 @@ pub fn Color(comptime T: type) type {
8482
test clamp {
8583
var color = Color(f64){ .red = 1.23, .green = 0.5, .blue = -0.01, .alpha = 1.01 };
8684
color.clamp();
87-
try testing.expectEqualSlices(f64, &[4]f64{ 1.0, 0.5, 0.0, 1.0 }, &[4]f64{
88-
color.red,
89-
color.green,
90-
color.blue,
91-
color.alpha,
92-
});
85+
try testing.expectEqual(
86+
.{ 1.0, 0.5, 0.0, 1.0 },
87+
.{ color.red, color.green, color.blue, color.alpha },
88+
);
9389
}
9490

9591
/// Creates a `Color` from
@@ -106,7 +102,7 @@ pub fn Color(comptime T: type) type {
106102

107103
test fromRgba8 {
108104
const color = Color(f64).fromRgba8(255, 0, 0, 255);
109-
try testing.expectEqualSlices(u8, &[4]u8{ 255, 0, 0, 255 }, &color.toRgba8());
105+
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
110106
}
111107

112108
/// Creates a `Color` from linear-light RGB values.
@@ -129,7 +125,7 @@ pub fn Color(comptime T: type) type {
129125

130126
test fromLinearRgb {
131127
const color = Color(f64).fromLinearRgb(1.0, 0.0, 0.0, 1.0);
132-
try testing.expectEqualSlices(u8, &[4]u8{ 255, 0, 0, 255 }, &color.toRgba8());
128+
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
133129
}
134130

135131
/// Creates a `Color` from
@@ -146,7 +142,7 @@ pub fn Color(comptime T: type) type {
146142

147143
test fromHsl {
148144
const color = Color(f64).fromHsl(360.0, 1.0, 0.5, 1.0);
149-
try testing.expectEqualSlices(u8, &[4]u8{ 255, 0, 0, 255 }, &color.toRgba8());
145+
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
150146
}
151147

152148
/// Creates a `Color` from
@@ -177,7 +173,7 @@ pub fn Color(comptime T: type) type {
177173

178174
test fromHwb {
179175
const color = Color(f64).fromHwb(0.0, 0.0, 0.0, 1.0);
180-
try testing.expectEqualSlices(u8, &[4]u8{ 255, 0, 0, 255 }, &color.toRgba8());
176+
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
181177
}
182178

183179
/// Creates a `Color` from
@@ -201,7 +197,7 @@ pub fn Color(comptime T: type) type {
201197
0.125_802_870_124_518_02,
202198
1.0,
203199
);
204-
try testing.expectEqualSlices(u8, &[4]u8{ 255, 0, 0, 255 }, &color.toRgba8());
200+
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
205201
}
206202

207203
/// Creates a `Color` from
@@ -272,20 +268,23 @@ pub fn Color(comptime T: type) type {
272268

273269
test "parsePercentOrFloat" {
274270
{
275-
const test_data = [_]struct { []const u8, struct { f64, bool } }{
276-
.{ "0%", .{ 0.0, true } },
277-
.{ "100%", .{ 1.0, true } },
278-
.{ "50%", .{ 0.5, true } },
279-
.{ "0", .{ 0.0, false } },
280-
.{ "1", .{ 1.0, false } },
281-
.{ "0.5", .{ 0.5, false } },
282-
.{ "100.0", .{ 100.0, false } },
283-
.{ "-23.7", .{ -23.7, false } },
284-
};
285-
for (test_data) |td| {
286-
const v = @This().parsePercentOrFloat(td[0]).?;
287-
try testing.expectEqual(td[1][0], v[0]);
288-
try testing.expectEqual(td[1][1], v[1]);
271+
const float_types = [_]type{f64};
272+
inline for (float_types) |ft| {
273+
const test_data = [_]struct { []const u8, struct { ft, bool } }{
274+
.{ "0%", .{ 0.0, true } },
275+
.{ "100%", .{ 1.0, true } },
276+
.{ "50%", .{ 0.5, true } },
277+
.{ "0", .{ 0.0, false } },
278+
.{ "1", .{ 1.0, false } },
279+
.{ "0.5", .{ 0.5, false } },
280+
.{ "100.0", .{ 100.0, false } },
281+
.{ "-23.7", .{ -23.7, false } },
282+
};
283+
for (test_data) |td| {
284+
const v = @This().parsePercentOrFloat(td[0]).?;
285+
try testing.expectEqual(td[1][0], v[0]);
286+
try testing.expectEqual(td[1][1], v[1]);
287+
}
289288
}
290289
}
291290
{
@@ -309,19 +308,22 @@ pub fn Color(comptime T: type) type {
309308

310309
test "parsePercentOr255" {
311310
{
312-
const test_data = [_]struct { []const u8, struct { f64, bool } }{
313-
.{ "0%", .{ 0.0, true } },
314-
.{ "100%", .{ 1.0, true } },
315-
.{ "50%", .{ 0.5, true } },
316-
.{ "-100%", .{ -1.0, true } },
317-
.{ "0", .{ 0.0, false } },
318-
.{ "255", .{ 1.0, false } },
319-
.{ "127.5", .{ 0.5, false } },
320-
};
321-
for (test_data) |td| {
322-
const v = @This().parsePercentOr255(td[0]).?;
323-
try testing.expectEqual(td[1][0], v[0]);
324-
try testing.expectEqual(td[1][1], v[1]);
311+
const float_types = [_]type{ f16, f32, f64, f80, f128 };
312+
inline for (float_types) |ft| {
313+
const test_data = [_]struct { []const u8, struct { ft, bool } }{
314+
.{ "0%", .{ 0.0, true } },
315+
.{ "100%", .{ 1.0, true } },
316+
.{ "50%", .{ 0.5, true } },
317+
.{ "-100%", .{ -1.0, true } },
318+
.{ "0", .{ 0.0, false } },
319+
.{ "255", .{ 1.0, false } },
320+
.{ "127.5", .{ 0.5, false } },
321+
};
322+
for (test_data) |td| {
323+
const v = @This().parsePercentOr255(td[0]).?;
324+
try testing.expectEqual(td[1][0], v[0]);
325+
try testing.expectEqual(td[1][1], v[1]);
326+
}
325327
}
326328
}
327329
{
@@ -354,20 +356,23 @@ pub fn Color(comptime T: type) type {
354356

355357
test "parseAngle" {
356358
{
357-
const test_data = [_]struct { []const u8, f64 }{
358-
.{ "360", 360.0 },
359-
.{ "127.356", 127.356 },
360-
.{ "+120deg", 120.0 },
361-
.{ "90deg", 90.0 },
362-
.{ "-127deg", -127.0 },
363-
.{ "100grad", 90.0 },
364-
.{ "1.5707963267948966rad", 90.0 },
365-
.{ "0.25turn", 90.0 },
366-
.{ "-0.25turn", -90.0 },
367-
};
368-
for (test_data) |td| {
369-
const v = @This().parseAngle(td[0]).?;
370-
try testing.expectEqual(td[1], v);
359+
const float_types = [_]type{f64};
360+
inline for (float_types) |ft| {
361+
const test_data = [_]struct { []const u8, ft }{
362+
.{ "360", 360.0 },
363+
.{ "127.356", 127.356 },
364+
.{ "+120deg", 120.0 },
365+
.{ "90deg", 90.0 },
366+
.{ "-127deg", -127.0 },
367+
.{ "100grad", 90.0 },
368+
.{ "1.5707963267948966rad", 90.0 },
369+
.{ "0.25turn", 90.0 },
370+
.{ "-0.25turn", -90.0 },
371+
};
372+
for (test_data) |td| {
373+
const v = @This().parseAngle(td[0]).?;
374+
try testing.expectEqual(td[1], v);
375+
}
371376
}
372377
}
373378
{
@@ -532,12 +537,11 @@ pub fn Color(comptime T: type) type {
532537

533538
test parse {
534539
const color = try Color(f64).parse("#ff0");
535-
try testing.expectEqualSlices(
536-
f64,
537-
&[4]f64{ 1.0, 1.0, 0.0, 1.0 },
538-
&[4]f64{ color.red, color.green, color.blue, color.alpha },
540+
try testing.expectEqual(
541+
.{ 1.0, 1.0, 0.0, 1.0 },
542+
.{ color.red, color.green, color.blue, color.alpha },
539543
);
540-
try testing.expectEqualSlices(u8, &[4]u8{ 255, 255, 0, 255 }, &color.toRgba8());
544+
try testing.expectEqual(.{ 255, 255, 0, 255 }, color.toRgba8());
541545
var buf: [7]u8 = undefined;
542546
const hex = try color.toHexString(&buf);
543547
try testing.expectEqualStrings("#ffff00", hex);
@@ -573,7 +577,7 @@ pub fn Color(comptime T: type) type {
573577

574578
test toRgba8 {
575579
const color = Color(f64).init(1.0, 0.0, 0.0, 1.0);
576-
try testing.expectEqualSlices(u8, &[4]u8{ 255, 0, 0, 255 }, &color.toRgba8());
580+
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
577581
}
578582

579583
/// Returns an array of linear-light RGB values.
@@ -593,7 +597,7 @@ pub fn Color(comptime T: type) type {
593597

594598
test toLinearRgb {
595599
const color = Color(f64).init(1.0, 0.0, 0.0, 1.0);
596-
try testing.expectEqualSlices(f64, &[4]f64{ 1.0, 0.0, 0.0, 1.0 }, &color.toLinearRgb());
600+
try testing.expectEqual(.{ 1.0, 0.0, 0.0, 1.0 }, color.toLinearRgb());
597601
}
598602

599603
/// Returns an array of
@@ -606,7 +610,7 @@ pub fn Color(comptime T: type) type {
606610

607611
test toHsl {
608612
const color = Color(f64).init(1.0, 0.0, 0.0, 1.0);
609-
try testing.expectEqualSlices(f64, &[4]f64{ 0.0, 1.0, 0.5, 1.0 }, &color.toHsl());
613+
try testing.expectEqual(.{ 0.0, 1.0, 0.5, 1.0 }, color.toHsl());
610614
}
611615

612616
/// Returns an array of
@@ -627,7 +631,7 @@ pub fn Color(comptime T: type) type {
627631

628632
test toHwb {
629633
const color = Color(f64).init(1.0, 0.0, 0.0, 1.0);
630-
try testing.expectEqualSlices(f64, &[4]f64{ 0.0, 0.0, 0.0, 1.0 }, &color.toHwb());
634+
try testing.expectEqual(.{ 0.0, 0.0, 0.0, 1.0 }, color.toHwb());
631635
}
632636

633637
/// Returns an array of
@@ -725,17 +729,20 @@ pub fn Color(comptime T: type) type {
725729
}
726730

727731
test "normalizeAngle" {
728-
const data = [_][2]f64{
729-
.{ 0.0, 0.0 },
730-
.{ 360.0, 0.0 },
731-
.{ 400.0, 40.0 },
732-
.{ 1155.0, 75.0 },
733-
.{ -360.0, 0.0 },
734-
.{ -90.0, 270.0 },
735-
.{ -765.0, 315.0 },
736-
};
737-
for (data) |d| {
738-
const c = Color(f64).normalizeAngle(d[0]);
739-
try testing.expectEqual(d[1], c);
732+
const float_types = [_]type{ f16, f32, f64, f80, f128 };
733+
inline for (float_types) |ft| {
734+
const data = [_][2]ft{
735+
.{ 0.0, 0.0 },
736+
.{ 360.0, 0.0 },
737+
.{ 400.0, 40.0 },
738+
.{ 1155.0, 75.0 },
739+
.{ -360.0, 0.0 },
740+
.{ -90.0, 270.0 },
741+
.{ -765.0, 315.0 },
742+
};
743+
for (data) |d| {
744+
const c = Color(ft).normalizeAngle(d[0]);
745+
try testing.expectEqual(d[1], c);
746+
}
740747
}
741748
}

tests/chrome_android.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const testing = @import("std").testing;
88
const Color = @import("csscolorparser").Color;
99

1010
test "random colors" {
11-
// The color string is randomly generated, then parsed using Chrome 87.0 on Android.
11+
// The color string is randomly generated, then parsed using Chrome 87.0 on
12+
// Android.
1213
const test_data = [_]struct { []const u8, [4]u8 }{
1314
.{ "#6946", .{ 102, 153, 68, 102 } },
1415
.{ "#4F0", .{ 68, 255, 0, 255 } },
@@ -211,8 +212,11 @@ test "random colors" {
211212
.{ "hsl(235.834,104.274%,12.569%)", .{ 0, 4, 64, 255 } },
212213
.{ "hsl(5.068rad,5.213%,83.391%)", .{ 214, 210, 215, 255 } },
213214
};
214-
for (test_data) |td| {
215-
const c = try Color(f64).parse(td[0]);
216-
try testing.expectEqualSlices(u8, &td[1], &c.toRgba8());
215+
const float_types = [_]type{ f32, f64 };
216+
inline for (float_types) |ft| {
217+
for (test_data) |td| {
218+
const c = try Color(ft).parse(td[0]);
219+
try testing.expectEqual(td[1], c.toRgba8());
220+
}
217221
}
218222
}

tests/chromium.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const testing = @import("std").testing;
88
const Color = @import("csscolorparser").Color;
99

1010
test "random colors" {
11-
// The color string is randomly generated, then parsed using Chromium 87.0.4280.66.
11+
// The color string is randomly generated, then parsed using Chromium
12+
// 87.0.4280.66.
1213
const test_data = [_]struct { []const u8, [4]u8 }{
1314
.{ "#9F2", .{ 153, 255, 34, 255 } },
1415
.{ "#919211", .{ 145, 146, 17, 255 } },
@@ -211,8 +212,11 @@ test "random colors" {
211212
.{ "hsl(41.239,30.040%,7.348%)", .{ 24, 21, 13, 255 } },
212213
.{ "hsl(5.794rad,109.895%,71.423%)", .{ 255, 109, 177, 255 } },
213214
};
214-
for (test_data) |td| {
215-
const c = try Color(f64).parse(td[0]);
216-
try testing.expectEqualSlices(u8, &td[1], &c.toRgba8());
215+
const float_types = [_]type{ f32, f64 };
216+
inline for (float_types) |ft| {
217+
for (test_data) |td| {
218+
const c = try Color(ft).parse(td[0]);
219+
try testing.expectEqual(td[1], c.toRgba8());
220+
}
217221
}
218222
}

0 commit comments

Comments
 (0)