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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
#include <stdio.h>
#include "tests.h"
#define START_TEST(t) \
diag(#t); \
test_##t();
void test_cast()
{
int c[] = {(int)'a', (int)'b'};
is_eq(c[0], 97);
double d = (double) 1;
is_eq(d, 1.0);
}
void test_castbool()
{
char i1 = (1 == 1);
short i2 = (1 == 1);
int i3 = (1 == 1);
long i4 = (1 == 0);
long long i5 = (1 == 0);
is_eq((i1==1) && (i2==1) && (i3==1) && (i4==0) && (i5==0), 1);
}
void char_overflow()
{
{
char c; c = -1;
unsigned char u = c;
is_eq(u, 256-1);
}
{
char c = -1;
unsigned char u = c;
is_eq(u, 256-1);
}
{
char c = (-1);
unsigned char u = c;
is_eq(u, 256-1);
}
{
char c = (((-1)));
unsigned char u = c;
is_eq(u, 256-1);
}
}
typedef double * vertex;
void test_vertex()
{
diag("vertex");
double a[1];
a[0] = 42;
double b[1];
b[0] = 45;
double dxoa;
vertex triorg = (vertex)(a);
vertex triapex = (vertex)(b);
dxoa = triorg[0] - triapex[0];
is_eq(dxoa, -3);
}
static int strlenChar(const char *z){
int n = 0;
while( *z ){
if( (0xc0&*(z++))!=0x80 ) n++;
}
return n;
}
void test_strCh()
{
char * z = "Hello, c2go\0";
is_eq(strlenChar(z),11);
}
typedef unsigned int pcre_uint32;
#define CHAR_NBSP ((unsigned char)'\xa0')
void test_preprocessor()
{
int tmp = 160;
pcre_uint32 chr = tmp;
is_eq(chr, CHAR_NBSP);
}
typedef unsigned char pcre_uchar;
typedef unsigned char pcre_uint8;
typedef struct pcre_study_data {
pcre_uint8 start_bits[32];
} pcre_study_data;
void caststr() {
pcre_uchar str[] = "abcd";
is_streq((char *) str, "abcd");
}
static const pcre_uchar TEST[] = {
'x', (pcre_uchar) CHAR_NBSP, '\n', '\0' };
#define CHAR_E ((unsigned char) 'e')
static const pcre_uchar TEST2[] = {
'x', (pcre_uchar) CHAR_NBSP, '\n',
(pcre_uchar) CHAR_E, '\0' };
void test_static_array()
{
is_eq('x', TEST[0]);
is_eq((pcre_uchar) '\xa0', TEST[1]);
is_eq('\n', TEST[2]);
is_eq('x', TEST2[0]);
is_eq('e', TEST2[3]); // can distinguish character at same column in different lines
}
void castbitwise() {
pcre_uint32 x = 0xff;
x &= ~0x3c;
is_eq(x, 0xc3);
}
void cast_pointer_diff(pcre_uchar *str, int *x) {
pcre_uchar *p = str;
pcre_uchar ab = '\0';
*x = (int)(p - str) - ab;
}
typedef unsigned char x_uint;
typedef unsigned char y_uint;
typedef struct {
unsigned char a;
unsigned char b;
} z_struct;
void test_voidcast()
{
x_uint x = 42;
void * y = &x;
y_uint *z = (y_uint*) y;
is_eq(42, *z);
x_uint arr1[] = { 1, 2, 3, 4 };
y = arr1;
z_struct *arr2 = (z_struct*) y;
is_eq(1, arr2[0].a);
is_eq(2, arr2[0].b);
is_eq(3, arr2[1].a);
is_eq(4, arr2[1].b);
pcre_uchar **stringlist;
y = &x;
void * py = &y;
stringlist = (pcre_uchar **)(py);
is_eq(**((x_uint **)py), 42);
is_eq(**stringlist, 42);
*((const char **)py) = NULL;
is_eq(x, 42);
is_true(y == NULL);
y = &x;
*((const pcre_uint8 **)py) = NULL;
is_eq(x, 42);
is_true(y == NULL);
y = &x;
*((const pcre_uchar **)py) = NULL;
is_eq(x, 42);
is_true(y == NULL);
}
int main()
{
plan(52);
START_TEST(cast);
START_TEST(castbool);
START_TEST(vertex);
START_TEST(strCh);
START_TEST(voidcast);
{
typedef unsigned int u32;
u32 x = 42;
is_eq(x , 42);
u32 a[10];
a[0] = x;
is_eq(a[0],42);
}
{
typedef double u32d;
u32d x = 42.0;
is_eq(x , 42.0);
u32d a[10];
a[0] = x;
is_eq(a[0],42.0);
}
{
typedef int integer;
typedef int INTEGER;
integer i = 123;
INTEGER j = 567;
j = i;
i = j;
is_eq(i , 123);
is_eq(j , 123);
}
double *d = (double *) 0;
is_true(d == NULL);
int *i = (int *) 0;
is_true(i == NULL);
float *f = (float *) 0;
is_true(f == NULL);
char *c = (char *) 0;
is_true(c == NULL);
double *d2 = 0;
is_true(d2 == NULL);
int *i2 = 0;
is_true(i2 == NULL);
float *f2 = 0;
is_true(f2 == NULL);
char *c2 = 0;
is_true(c2 == NULL);
diag("Calloc with type")
{
double *ddd = (double *)calloc(2,sizeof(double));
is_not_null(ddd);
(void)(ddd);
}
{
double *ddd;
ddd = (double *)calloc(2,sizeof(double));
is_not_null(ddd);
(void)(ddd);
}
diag("Type convertion from void* to ...")
{
void * ptr2;
int tInt = 55;
ptr2 = &tInt;
is_eq(*(int*)ptr2, 55);
double tDouble = -13;
ptr2 = &tDouble;
is_eq(*(double*)ptr2,-13);
float tFloat = 67;
is_eq(*(float *)(&tFloat),67);
}
diag("Type convertion from void* to ... in initialization")
{
long tLong = 556;
void * ptr3 = &tLong;
is_eq(*(long *) ptr3, 556);
}
char_overflow();
diag("Compare preprocessor with type")
test_preprocessor();
diag("Typedef slice convertion")
caststr();
diag("Compare with static array")
test_static_array();
diag("Cast with compound assign operator")
castbitwise();
diag("Cast pointer diff");
{
pcre_uchar s[] = "abcd";
int b = 42;
cast_pointer_diff(&s[0], &b);
is_eq(b, 0);
}
diag("Cast array to slice");
{
pcre_study_data sdata;
sdata.start_bits[1] = 42;
const pcre_study_data *study = &sdata;
const pcre_uint8 *p = 0;
p = study->start_bits;
is_eq(p[1], 42);
}
done_testing();
}
|