-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.c
100 lines (98 loc) · 1.54 KB
/
tests.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
/* Testing Code */
#include <limits.h>
//
// 15 problems, 40 points
// 2 - rating 1
// 3 - rating 2
// 7 - rating 3
// 3 - rating 4
// rating 1
int test_isZero(int x) {
return x == 0;
}
int test_bitAnd(int x, int y)
{
return x&y;
}
// rating 2
int test_fitsBits(int x, int n)
{
int TMin_n = -(1 << (n-1));
int TMax_n = (1 << (n-1)) - 1;
return x >= TMin_n && x <= TMax_n;
}
int test_getByte(int x, int n)
{
union {
int word;
unsigned char bytes[4];
} u;
int test = 1;
int littleEndian = (int) *(char *) &test;
u.word = x;
return littleEndian ? (unsigned) u.bytes[n] : (unsigned) u.bytes[3-n];
}
int test_isNotEqual(int x, int y)
{
return x != y;
}
int test_bitXor(int x, int y)
{
return x^y;
}
int test_copyLSB(int x)
{
return (x & 0x1) ? -1 : 0;
}
// rating 3
int test_sum3(int x, int y, int z)
{
return x+y+z;
}
int test_reverseBytes(int x)
{
union U {
int result;
char byte[4];
};
union U u;
int temp;
u.result = x;
temp = u.byte[0];
u.byte[0] = u.byte[3];
u.byte[3] = temp;
temp = u.byte[1];
u.byte[1] = u.byte[2];
u.byte[2] = temp;
return u.result;
}
int test_isNegative(int x) {
return x < 0;
}
int test_isLess(int x, int y)
{
return x < y;
}
// rating 4
int test_isNonZero(int x)
{
return x!=0;
}
int test_bitCount(int x) {
int result = 0;
int i;
for (i = 0; i < 32; i++)
result += (x >> i) & 0x1;
return result;
}
int test_bang(int x)
{
return !x;
}
int test_bitParity(int x) {
int result = 0;
int i;
for (i = 0; i < 32; i++)
result ^= (x >> i) & 0x1;
return result;
}