-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_phash.c
105 lines (83 loc) · 2.92 KB
/
test_phash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <assert.h>
#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "pHash.h"
// Mock image data for testing
static unsigned char test_image_data[] = {
255, 0, 0, 0, 255, 0, 0, 0, 255, // RGB pixels
0, 0, 0, 255, 255, 255, 128, 128, 128 // More pixels
};
void test_initialization() {
PhashError err = phash_initialize();
assert(err == PHASH_OK);
printf("✓ Initialization test passed\n");
}
void test_image_creation() {
PhashImage* img = NULL;
PhashError err = phash_image_create(test_image_data, 3, 2, 3, 1, &img);
assert(err == PHASH_OK);
assert(img != NULL);
assert(img->width == 3);
assert(img->height == 2);
assert(img->channels == 3);
assert(img->owns_memory == 1);
phash_image_destroy(img);
printf("✓ Image creation test passed\n");
}
void test_config_validation() {
PhashConfig config = phash_config_default();
// Test valid configuration
PhashError err = phash_config_validate(&config);
assert(err == PHASH_OK);
// Test invalid configurations
config.dct_size = 7; // Not power of 2
err = phash_config_validate(&config);
assert(err == PHASH_ERR_INVALID_ARGUMENT);
config = phash_config_default();
config.hash_size = config.dct_size + 1; // Too large
err = phash_config_validate(&config);
assert(err == PHASH_ERR_INVALID_ARGUMENT);
printf("✓ Configuration validation test passed\n");
}
void test_hash_computation() {
PhashImage* img = NULL;
uint64_t hash;
PhashConfig config = phash_config_default();
PhashError err = phash_image_create(test_image_data, 3, 2, 3, 1, &img);
assert(err == PHASH_OK);
err = phash_compute(img, &config, &hash);
assert(err == PHASH_OK);
assert(hash != 0); // Hash should not be zero for non-zero image
phash_image_destroy(img);
printf("✓ Hash computation test passed\n");
}
void test_hash_comparison() {
uint64_t hash1 = 0x1234567890ABCDEF;
uint64_t hash2 = 0x1234567890ABCDEF;
uint64_t hash3 = 0xFFFFFFFFFFFFFFFF;
int distance;
PhashError err = phash_compare(hash1, hash2, &distance);
assert(err == PHASH_OK);
assert(distance == 0); // Identical hashes
err = phash_compare(hash1, hash3, &distance);
assert(err == PHASH_OK);
assert(distance > 0); // Different hashes
printf("✓ Hash comparison test passed\n");
}
void test_error_handling() {
assert(strcmp(phash_error_string(PHASH_OK), "Success") == 0);
assert(phash_error_string(PHASH_ERR_NULL_POINTER) != NULL);
printf("✓ Error handling test passed\n");
}
int main() {
printf("Running pHash library tests...\n\n");
test_initialization();
test_image_creation();
test_config_validation();
test_hash_computation();
test_hash_comparison();
test_error_handling();
phash_terminate();
printf("\nAll tests passed successfully!\n");
return 0;
}