comparison base64.c @ 654:b751ac480294 libavutil

Add a new test program for base64, based on that removed in r17024. See the thread: "[PATCH] remove unused and broken test program in libavutil/base64.c".
author stefano
date Fri, 06 Feb 2009 23:29:25 +0000
parents 332a5b820330
children c9bd2357f2d4
comparison
equal deleted inserted replaced
653:332a5b820330 654:b751ac480294
96 *dst++ = '='; 96 *dst++ = '=';
97 *dst = '\0'; 97 *dst = '\0';
98 98
99 return ret; 99 return ret;
100 } 100 }
101
102 #ifdef TEST
103
104 #undef printf
105
106 #define MAX_DATA_SIZE 1024
107 #define MAX_ENCODED_SIZE 2048
108
109 int test_encode_decode(const uint8_t *data, unsigned int data_size, const char *encoded_ref)
110 {
111 char encoded[MAX_ENCODED_SIZE];
112 uint8_t data2[MAX_DATA_SIZE];
113 int data2_size, max_data2_size = MAX_DATA_SIZE;
114
115 if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
116 printf("Failed: cannot encode the input data\n");
117 return 1;
118 }
119 if (encoded_ref && strcmp(encoded, encoded_ref)) {
120 printf("Failed: encoded string differs from reference\n"
121 "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
122 return 1;
123 }
124
125 if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) {
126 printf("Failed: cannot decode the encoded string\n"
127 "Encoded:\n%s\n", encoded);
128 return 1;
129 }
130 if (memcmp(data2, data, data_size)) {
131 printf("Failed: encoded/decoded data differs from original data\n");
132 return 1;
133 }
134
135 printf("Passed!\n");
136 return 0;
137 }
138
139 int main(void)
140 {
141 int i, error_count = 0;
142 struct test {
143 const uint8_t *data;
144 const char *encoded_ref;
145 } tests[] = {
146 { "", ""},
147 { "1", "MQ=="},
148 { "22", "MjI="},
149 { "333", "MzMz"},
150 { "4444", "NDQ0NA=="},
151 { "55555", "NTU1NTU="},
152 { "666666", "NjY2NjY2"},
153 { "abc:def", "YWJjOmRlZg=="},
154 };
155
156 printf("Encoding/decoding tests\n");
157 for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
158 error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
159
160 return error_count;
161 }
162
163 #endif