liblash

Basic tools written and used by lash in c programming.
Info | Log | Files | Refs

test_endian.c (930B)


      1 #include <string.h>
      2 #include <stdio.h>
      3 
      4 #include "endian.h"
      5 #include "strip.h"
      6 
      7 
      8 int main() {
      9 	int r;
     10 	int i;
     11 	char left[] = { 0x02, 0x13, 0x24, 0x35};
     12 	char right[] = { 0x35, 0x24, 0x13, 0x02};
     13 	unsigned char tmp[4];
     14 
     15 	memcpy(tmp, left, 4);
     16 
     17 	// four byte flip
     18 	flip_endian(4, tmp);
     19 	for (i = 0; i < 4; i++) {
     20 		if (tmp[i] != *(right+i)) {
     21 			return 1;
     22 		}
     23 	}
     24 
     25 	// two byte flip
     26 	memcpy(tmp, left, 2);
     27 	flip_endian(2, tmp);
     28 	for (i = 0; i < 4; i++) {
     29 		if (tmp[i] != *(right+2+i)) {
     30 			return 1;
     31 		}
     32 	}
     33 
     34 	// single byte flip
     35 	tmp[0] = 0x2a;
     36 	flip_endian(1, tmp);
     37 	tmp[0] = 0x2a;
     38 
     39 
     40 	// check explicit endian convert
     41 	if (is_le()) {
     42 		memcpy(tmp, left, 4);
     43 	} else {
     44 		memcpy(tmp, right, 4);
     45 	}
     46 	r = to_endian(0, 4, (void*)tmp);
     47 	if (r) {
     48 		return 1;
     49 	}
     50 	for (i = 0; i < 4; i++) {
     51 		if (tmp[i] != *(right+i)) {
     52 			return 1;
     53 		}
     54 	}
     55 
     56 	// check invalid length
     57 	r = to_endian(0, 3, (void*)tmp);
     58 	if (!r) {
     59 		return 1;
     60 	}
     61 
     62 	return 0;
     63 }