00001 #include <sys/stat.h>
00002 #include <sys/time.h>
00003 #include <sys/types.h>
00004 #include <string.h>
00005 #include <stdlib.h>
00006 #include <limits.h>
00007 #include <stdio.h>
00008 #include <fcntl.h>
00009 #include <signal.h>
00010 #include <u/libu.h>
00011
00012 int test_suite_array_register (u_test_t *t);
00013
00014 static int test_resize (u_test_case_t *tc);
00015 static int test_short (u_test_case_t *tc);
00016 static int test_ptr (u_test_case_t *tc);
00017 static int test_u_short (u_test_case_t *tc);
00018 static int test_char (u_test_case_t *tc);
00019 static int test_u_char (u_test_case_t *tc);
00020
00021 static int test_resize (u_test_case_t *tc)
00022 {
00023 u_array_t *da = NULL;
00024 size_t idx;
00025 short s, s0;
00026
00027 u_test_err_if (u_array_create(U_ARRAY_TYPE_SHORT, 100, &da));
00028
00029 for (s = SHRT_MIN, idx = 0; s < SHRT_MAX; s++, idx++)
00030 {
00031 u_test_err_if (u_array_set_short(da, idx, s, NULL));
00032 u_test_err_if (u_array_get_short(da, idx, &s0));
00033 u_test_err_ifm (s != s0, "s = %d, s0 = %d, idx = %zu", s, s0, idx);
00034 }
00035
00036 u_array_free(da);
00037
00038 return U_TEST_SUCCESS;
00039 err:
00040 u_array_free(da);
00041
00042 return U_TEST_FAILURE;
00043 }
00044
00045 static int test_short (u_test_case_t *tc)
00046 {
00047 u_array_t *da = NULL;
00048 size_t idx;
00049 short s, s0;
00050
00051 u_test_err_if (u_array_create(U_ARRAY_TYPE_SHORT, SHRT_MAX * 2 + 1, &da));
00052
00053 for (s = SHRT_MIN, idx = 0; s < SHRT_MAX; s++, idx++)
00054 {
00055 u_test_err_if (u_array_set_short(da, idx, s, NULL));
00056 u_test_err_if (u_array_get_short(da, idx, &s0));
00057 u_test_err_ifm (s != s0, "s = %d, s0 = %d, idx = %zu", s, s0, idx);
00058 }
00059
00060 u_array_free(da);
00061
00062 return U_TEST_SUCCESS;
00063 err:
00064 u_array_free(da);
00065
00066 return U_TEST_FAILURE;
00067 }
00068
00069 static int test_ptr (u_test_case_t *tc)
00070 {
00071 int rc = 0;
00072 u_array_t *da = NULL;
00073 size_t idx;
00074 struct S { int i; char c; } s, *s0;
00075
00076 u_test_err_if (u_array_create(U_ARRAY_TYPE_PTR, 10, &da));
00077
00078 for (idx = 0; idx < 100; idx++)
00079 {
00080 s.i = (int) idx;
00081 s.c = (char) idx;
00082
00083
00084 (void) u_array_set_ptr(da, idx, &s, &rc);
00085 u_test_err_ifm (rc == -1, "setting %p at idx %zu failed", &s, idx);
00086
00087 s0 = u_array_get_ptr(da, idx, &rc);
00088 u_test_err_ifm (rc == -1, "getting from idx %zu failed", idx);
00089
00090 u_test_err_ifm (s.i != s0->i, "%d != %d at index %zu", s.i, s0->i, idx);
00091 u_test_err_ifm (s.c != s0->c, "%c != %c at index %zu", s.c, s0->c, idx);
00092 }
00093
00094 u_array_free(da);
00095
00096 return U_TEST_SUCCESS;
00097 err:
00098 u_array_free(da);
00099
00100 return U_TEST_FAILURE;
00101 }
00102
00103 static int test_u_short (u_test_case_t *tc)
00104 {
00105 u_array_t *da = NULL;
00106 size_t idx;
00107 unsigned short s, s0;
00108
00109 u_test_err_if (u_array_create(U_ARRAY_TYPE_U_SHORT, USHRT_MAX + 1, &da));
00110
00111 for (s = 0, idx = 0; s < USHRT_MAX; s++, idx++)
00112 {
00113 u_test_err_if (u_array_set_u_short(da, idx, s, NULL));
00114 u_test_err_if (u_array_get_u_short(da, idx, &s0));
00115 u_test_err_ifm (s != s0, "s = %d, s0 = %d, idx = %zu", s, s0, idx);
00116 }
00117
00118 u_array_free(da);
00119
00120 return U_TEST_SUCCESS;
00121 err:
00122 u_array_free(da);
00123
00124 return U_TEST_FAILURE;
00125 }
00126
00127 static int test_char (u_test_case_t *tc)
00128 {
00129 u_array_t *da = NULL;
00130 size_t idx;
00131 char s, s0;
00132
00133 u_test_err_if (u_array_create(U_ARRAY_TYPE_CHAR, CHAR_MAX * 2 + 1, &da));
00134
00135 for (s = CHAR_MIN, idx = 0; s < CHAR_MAX; s++, idx++)
00136 {
00137 u_test_err_if (u_array_set_char(da, idx, s, NULL));
00138 u_test_err_if (u_array_get_char(da, idx, &s0));
00139 u_test_err_ifm (s != s0, "s = %d, s0 = %d, idx = %zu", s, s0, idx);
00140 }
00141
00142 u_array_free(da);
00143
00144 return U_TEST_SUCCESS;
00145 err:
00146 u_array_free(da);
00147
00148 return U_TEST_FAILURE;
00149 }
00150
00151 static int test_u_char (u_test_case_t *tc)
00152 {
00153 size_t idx;
00154 u_array_t *da = NULL;
00155 unsigned char s, s0;
00156
00157 u_test_err_if (u_array_create(U_ARRAY_TYPE_U_CHAR, UCHAR_MAX + 1, &da));
00158
00159 for (s = 0, idx = 0; s < UCHAR_MAX; s++, idx++)
00160 {
00161 u_test_err_if (u_array_set_u_char(da, idx, s, NULL));
00162 u_test_err_if (u_array_get_u_char(da, idx, &s0));
00163 u_test_err_ifm (s != s0, "s = %d, s0 = %d, idx = %zu", s, s0, idx);
00164 }
00165
00166 u_array_free(da);
00167
00168 return U_TEST_SUCCESS;
00169 err:
00170 u_array_free(da);
00171
00172 return U_TEST_FAILURE;
00173 }
00174
00175 int test_suite_array_register (u_test_t *t)
00176 {
00177 u_test_suite_t *ts = NULL;
00178
00179 con_err_if (u_test_suite_new("Dynamic Arrays", &ts));
00180
00181 con_err_if (u_test_case_register("Pointer elements", test_ptr, ts));
00182 con_err_if (u_test_case_register("Short elements", test_short, ts));
00183 con_err_if (u_test_case_register("Unsigned short elements",
00184 test_u_short, ts));
00185 con_err_if (u_test_case_register("Char elements", test_char, ts));
00186 con_err_if (u_test_case_register("Unsigned char elements",
00187 test_u_char, ts));
00188 con_err_if (u_test_case_register("Force array resize", test_resize, ts));
00189
00190 return u_test_suite_add(ts, t);
00191 err:
00192 u_test_suite_free(ts);
00193 return ~0;
00194 }