Skip to content

Commit

Permalink
Add gradient parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
bynect committed Aug 17, 2024
1 parent e18f382 commit 5fa2cc3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void gradient_pattern(struct gradient *grad)
}
}

char *gradient_to_string(struct gradient *grad)
char *gradient_to_string(const struct gradient *grad)
{
if (!GRADIENT_VALID(grad)) return NULL;

Expand Down
2 changes: 1 addition & 1 deletion src/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void gradient_free(struct gradient *grad);

void gradient_pattern(struct gradient *grad);

char *gradient_to_string(struct gradient *grad);
char *gradient_to_string(const struct gradient *grad);


void draw_setup(void);
Expand Down
66 changes: 65 additions & 1 deletion test/option_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ TEST test_string_to_color_invalid(void)
struct setting s;
s.type = TYPE_COLOR;
s.value = &val;
s.name = "test_color";
s.name = "test_color_invalid";

const char* inputs[] = {
"",
Expand Down Expand Up @@ -727,6 +727,69 @@ TEST test_string_to_color_invalid(void)
PASS();
}

TEST test_string_to_gradient(void)
{
struct gradient *grad = NULL;
struct setting s;
s.type = TYPE_GRADIENT;
s.value = &grad;
s.name = "test_gradient";

const char* inputs[] = {
"#123456",
"#ab123c",
"#abc, #ebf, #aaafff",
"#abc123, #acaf8f",
};

// NOTE: Flexible array shenanigans
struct gradient *results[] = {
gradient_alloc(1),
gradient_alloc(1),
gradient_alloc(3),
gradient_alloc(2),
};

results[0]->colors[0] = (struct color) { (double)0x12 / 0xff, (double)0x34 / 0xff, (double)0x56 / 0xff, 1.0};

results[1]->colors[0] = (struct color) { (double)0xab / 0xff, (double)0x12 / 0xff, (double)0x3c / 0xff, 1.0};

results[2]->colors[0] = (struct color) { (double)0xaa / 0xff, (double)0xbb / 0xff, (double)0xcc / 0xff, 1.0};
results[2]->colors[1] = (struct color) { (double)0xee / 0xff, (double)0xbb / 0xff, (double)0xff / 0xff, 1.0};
results[2]->colors[2] = (struct color) { (double)0xaa / 0xff, (double)0xaf / 0xff, (double)0xff / 0xff, 1.0};

results[3]->colors[0] = (struct color) { (double)0xab / 0xff, (double)0xc1 / 0xff, (double)0x23 / 0xff, 1.0};
results[3]->colors[1] = (struct color) { (double)0xac / 0xff, (double)0xaf / 0xff, (double)0x8f / 0xff, 1.0};

ARRAY_SAME_LENGTH(inputs, results);

static char buf[100];

for (int i = 0; i < G_N_ELEMENTS(inputs); i++) {
sprintf(buf, "Failed in round %i", i);
ASSERTm(buf, set_from_string(&grad, s, inputs[i]));

char *t1 = gradient_to_string(results[i]);
char *t2 = gradient_to_string(grad);

sprintf(buf, "Failed in round %i. Expected %s, got %s", i, t1, t2);

g_free(t1);
g_free(t2);

ASSERTm(buf, grad != NULL);
ASSERTm(buf, grad->length == results[i]->length);

for (int k = 0; k < grad->length; k++)
ASSERTm(buf, COLOR_SAME(grad->colors[k], results[i]->colors[k]));
}

for (int i = 0; i < G_N_ELEMENTS(results); i++)
gradient_free(results[i]);

PASS();
}

TEST test_string_to_length(void)
{
struct length val = {0};
Expand Down Expand Up @@ -999,6 +1062,7 @@ SUITE(suite_option_parser)
RUN_TEST(test_string_to_sepcolor_invalid);
RUN_TEST(test_string_to_color);
RUN_TEST(test_string_to_color_invalid);
RUN_TEST(test_string_to_gradient);
RUN_TEST(test_enum_size);
RUN_TEST(test_string_to_length);
RUN_TEST(test_string_to_length_invalid);
Expand Down

0 comments on commit 5fa2cc3

Please sign in to comment.