228 lines
7.7 KiB
C
228 lines
7.7 KiB
C
#include "swill.c"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int test_new_bucket(void) {
|
|
bucket_t *test = new_bucket(300);
|
|
if (test->bufsize != 900) {
|
|
fprintf(stderr, "bufsize does not equal 3 times pixel count.\n");
|
|
return EXIT_FAILURE;
|
|
} else if (test->len != 0) {
|
|
fprintf(stderr, "len does not equal 0\n");
|
|
return EXIT_FAILURE;
|
|
} else if (test->low_r != 255) {
|
|
fprintf(stderr, "low_r does not equal 255\n");
|
|
return EXIT_FAILURE;
|
|
} else if (test->high_r != 0) {
|
|
fprintf(stderr, "high_r does not equal 0\n");
|
|
return EXIT_FAILURE;
|
|
} else if (test->low_g != 255) {
|
|
fprintf(stderr, "low_g does not equal 255\n");
|
|
return EXIT_FAILURE;
|
|
} else if (test->high_g != 0) {
|
|
fprintf(stderr, "high_g does not equal 0\n");
|
|
return EXIT_FAILURE;
|
|
} else if (test->low_b != 255) {
|
|
fprintf(stderr, "low_b does not equal 255\n");
|
|
return EXIT_FAILURE;
|
|
} else if (test->high_b != 0) {
|
|
fprintf(stderr, "high_b does not equal 0\n");
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
return EXIT_SUCCESS;
|
|
}
|
|
}
|
|
|
|
int test_analyze_bucket(void) {
|
|
bucket_t *bucket = new_bucket(5);
|
|
uint8_t test_data[] = {0, 40, 30, 30, 20, 50, 10, 15,
|
|
99, 28, 23, 44, 9, 48, 2};
|
|
bucket->pixels = test_data;
|
|
bucket->len = 5;
|
|
analyze_bucket(bucket);
|
|
|
|
if (bucket->low_r != 0) {
|
|
fprintf(stderr, "low_r (%u) does not match lowest r value\n",
|
|
bucket->low_r);
|
|
return EXIT_FAILURE;
|
|
} else if (bucket->high_r != 30) {
|
|
fprintf(stderr, "high_r (%u) does not match highest r value\n",
|
|
bucket->high_r);
|
|
return EXIT_FAILURE;
|
|
} else if (bucket->low_g != 15) {
|
|
fprintf(stderr, "low_g (%u) does not match highest g value\n",
|
|
bucket->low_g);
|
|
return EXIT_FAILURE;
|
|
} else if (bucket->high_g != 48) {
|
|
fprintf(stderr, "high_g (%u) does not match highest g value\n",
|
|
bucket->high_g);
|
|
return EXIT_FAILURE;
|
|
} else if (bucket->low_b != 2) {
|
|
fprintf(stderr, "low_b (%u) does not match lowest b value\n",
|
|
bucket->low_b);
|
|
return EXIT_FAILURE;
|
|
} else if (bucket->high_b != 99) {
|
|
fprintf(stderr, "high_b (%u) does not match highest b value\n",
|
|
bucket->high_b);
|
|
return EXIT_FAILURE;
|
|
} else {
|
|
return EXIT_SUCCESS;
|
|
}
|
|
}
|
|
|
|
int test_fill_bucket(void) {
|
|
uint8_t *pixel_data[3];
|
|
uint8_t row1[] = {0, 10, 20, 10, 20, 30, 5, 6, 7};
|
|
uint8_t row2[] = {20, 30, 99, 40, 30, 22, 3, 4, 9};
|
|
uint8_t row3[] = {90, 30, 70, 33, 54, 39, 22, 12, 8};
|
|
|
|
uint8_t sorted_pixel_data[] = {90, 30, 70, 40, 30, 22, 33, 54, 39,
|
|
22, 12, 8, 20, 30, 99, 10, 20, 30,
|
|
5, 6, 7, 3, 4, 9, 0, 10, 20};
|
|
pixel_data[0] = row1;
|
|
pixel_data[1] = row2;
|
|
pixel_data[2] = row3;
|
|
bucket_t *bucket = new_bucket(9);
|
|
fill_bucket(bucket, pixel_data, 3, 3, SORT_RED);
|
|
if (bucket->len != 9) {
|
|
fprintf(stderr, "bucket->len (%u) is not 9 as expected\n", bucket->len);
|
|
return EXIT_FAILURE;
|
|
}
|
|
for (unsigned int i = 0; i < bucket->bufsize; i++) {
|
|
if (bucket->pixels[i] != sorted_pixel_data[i]) {
|
|
fprintf(
|
|
stderr,
|
|
"bucket->pixels[i] (%u) != sorted_pixel_data[i] (%u), for i = %u\n",
|
|
bucket->pixels[i], sorted_pixel_data[i], i);
|
|
return EXIT_FAILURE;
|
|
}
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int test_swap_colors(void) {
|
|
uint8_t a[] = {1, 2, 3};
|
|
uint8_t b[] = {4, 5, 6};
|
|
swap_colors(a, b);
|
|
if (a[0] != 4 || a[1] != 5 || a[2] != 6) {
|
|
fprintf(stderr, "a[] = {%u, %u, %u} and not expected {4, 5, 6}", a[0], a[1],
|
|
a[2]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
if (b[0] != 1 || b[1] != 2 || b[2] != 3) {
|
|
fprintf(stderr, "b[] = {%u, %u, %u} and not expected {1, 2, 3}", b[0], b[1],
|
|
b[2]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int test_sift_down(void) {
|
|
uint8_t heap[] = {
|
|
45, 3, 82, // Root (index 1)
|
|
9, 99, 32, 3, 9, 54, // First layer (2,3)
|
|
50, 30, 90, 8, 12, 39, 99, 84, 28, 5, 32, 77, // Second layer (4,5,6,7)
|
|
79, 84, 33, 1, 80, 40, 7, 90, 43 // Last layer (8,9,10)
|
|
};
|
|
uint8_t sifted_heap[] = {
|
|
45, 3, 82, // Root (index 1)
|
|
8, 12, 39, 3, 9, 54, // First layer (2,3)
|
|
50, 30, 90, 7, 90, 43, 99, 84, 28, 5, 32, 77, // Second layer (4,5,6,7)
|
|
79, 84, 33, 1, 80, 40, 9, 99, 32 // Last layer (8,9,10)
|
|
};
|
|
// heap - 3 so the root node is at index 1 (three bytes per element),
|
|
// which simplifies heap arithmetic.
|
|
sift_down(heap - 3, 2, 10, SORT_GREEN);
|
|
for (int i = 0; i < 30; i++) {
|
|
if (heap[i] != sifted_heap[i]) {
|
|
fprintf(stderr, "heap[i] (%u) != sifted_heap[i] (%u), i = %u\n", heap[i],
|
|
sifted_heap[i], i);
|
|
return EXIT_FAILURE;
|
|
}
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int test_sort_bucket(void) {
|
|
uint8_t pixel_data[] = {0, 10, 20, 10, 20, 30, 5, 6, 7,
|
|
20, 30, 99, 40, 30, 22, 3, 4, 9,
|
|
90, 30, 70, 33, 54, 39, 22, 12, 8};
|
|
|
|
uint8_t sorted_pixel_data[] = {90, 30, 70, 40, 30, 22, 33, 54, 39,
|
|
22, 12, 8, 20, 30, 99, 10, 20, 30,
|
|
5, 6, 7, 3, 4, 9, 0, 10, 20};
|
|
bucket_t *bucket = new_bucket(9);
|
|
bucket->len = 9;
|
|
bucket->pixels = pixel_data;
|
|
sort_bucket(bucket, SORT_RED);
|
|
for (unsigned int i = 0; i < bucket->bufsize; i++) {
|
|
if (bucket->pixels[i] != sorted_pixel_data[i]) {
|
|
fprintf(
|
|
stderr,
|
|
"bucket->pixels[i] (%u) != sorted_pixel_data[i] (%u), for i = %u\n",
|
|
bucket->pixels[i], sorted_pixel_data[i], i);
|
|
return EXIT_FAILURE;
|
|
}
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int test_normalize_bytes(void) {
|
|
uint8_t *pixel_data[3];
|
|
uint8_t row1[] = {0, 25, 51, 25, 51, 77, 13, 15, 18};
|
|
uint8_t row2[] = {51, 76, 252, 102, 77, 56, 8, 10, 23};
|
|
uint8_t row3[] = {229, 76, 179, 84, 138, 99, 56, 31, 20};
|
|
pixel_data[0] = row1;
|
|
pixel_data[1] = row2;
|
|
pixel_data[2] = row3;
|
|
uint8_t *normal_pixel_data[3];
|
|
uint8_t normal_row1[] = {0, 10, 20, 10, 20, 30, 5, 6, 7};
|
|
uint8_t normal_row2[] = {20, 30, 99, 40, 30, 22, 3, 4, 9};
|
|
uint8_t normal_row3[] = {90, 30, 70, 33, 54, 39, 22, 12, 8};
|
|
normal_pixel_data[0] = normal_row1;
|
|
normal_pixel_data[1] = normal_row2;
|
|
normal_pixel_data[2] = normal_row3;
|
|
uint8_t **output;
|
|
normalize_bytes(pixel_data, &output, true, 3, 3);
|
|
for (int y = 0; y < 3; y++) {
|
|
for (int x = 0; x < 3; x++) {
|
|
if (output[y][x] != normal_pixel_data[y][x]) {
|
|
fprintf(
|
|
stderr,
|
|
"output[y][x] (%u) != normal_pixel_data[y][x] (%u), y = %u, x = %u",
|
|
output[y][x], normal_pixel_data[y][x], y, x);
|
|
return EXIT_FAILURE;
|
|
}
|
|
}
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc != 2) {
|
|
printf("%s takes one argument: the test to perform.\n", argv[0]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
if (strcmp(argv[1], "new_bucket") == 0) {
|
|
printf("Running test new_bucket\n");
|
|
return test_new_bucket();
|
|
} else if (strcmp(argv[1], "analyze_bucket") == 0) {
|
|
printf("Running test analyze_bucket\n");
|
|
return test_analyze_bucket();
|
|
} else if (strcmp(argv[1], "fill_bucket") == 0) {
|
|
printf("Running test fill_bucket\n");
|
|
return test_fill_bucket();
|
|
} else if (strcmp(argv[1], "swap_colors") == 0) {
|
|
printf("Running test swap_colors\n");
|
|
return test_swap_colors();
|
|
} else if (strcmp(argv[1], "sift_down") == 0) {
|
|
printf("Running test sift_down\n");
|
|
return test_sift_down();
|
|
} else if (strcmp(argv[1], "sort_bucket") == 0) {
|
|
printf("Running test sort_bucket\n");
|
|
return test_sort_bucket();
|
|
} else if (strcmp(argv[1], "normalize_bytes") == 0) {
|
|
printf("Running test normalize_bytes\n");
|
|
return test_normalize_bytes();
|
|
}
|
|
}
|