-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALUTest.java
More file actions
456 lines (414 loc) · 18.7 KB
/
ALUTest.java
File metadata and controls
456 lines (414 loc) · 18.7 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import org.junit.Test;
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
import static org.junit.Assert.*;
/**
* Created by xuxiangzhe on 2017/5/27.
*/
public class ALUTest {
static XALU xalu=new XALU();
static ALU myALU=new ALU();
static ALU qALU=new ALU();
public static void main(String[] args) {
// System.out.println(alu.integerSubtraction("0000","1000",8));
// System.out.println(alu.signedAddition("0001","1000",4));
// System.out.println(alu.floatTrueValue("0111111111111000000000000000000000000000000000000000000000000000", 11, 52));
System.out.println(qALU.floatTrueValue("0110000100100110110000101101010000100101011011111111110011000011", 11, 52));
}
//expect actual
@org.junit.Test
public void integerRepresentation() throws Exception {
for(int number=-1024;number<=1024;number++){
for(int length=64;length<=128;length++){
System.out.println(number+"###"+length);
assertEquals(xalu.integerRepresentation(String.valueOf(number),length),myALU.integerRepresentation(String.valueOf(number),length));
}
}
}
@Test
public void integerRepresentation2() throws Exception {
for(int number=-128;number<=128;number++){
for(int length=8;length<=32;length++){
System.out.println(number+"###"+length);
assertEquals(xalu.integerRepresentation(String.valueOf(number),length),qALU.integerRepresentation(String.valueOf(number),length));
}
}
}
@org.junit.Test
public void floatRepresentation() throws Exception {
String string;
for(int i=0;i<1000;i=(int)(i+Math.random()*10)) {
string=String.valueOf(-50000+0.1D*i);
for(int elength=4;elength<20;elength++){
for(int slength=4;slength<40;slength+=3){
System.out.println(i);
System.out.println(string+"##"+elength+"##"+slength);
System.out.println(string);
assertEquals(xalu.floatRepresentation(string, elength, slength), myALU.floatRepresentation(string, elength, slength));
}
}
}
}
@org.junit.Test
public void floatRepresentation2() throws Exception {
int random=0;
String string;
for(int i=0;i<100;i++) {
string=String.valueOf(-5.0+0.1D*i);
for(int elength=4;elength<19;elength++){
for(int slength=4;slength<40;slength++){
random++;
if(random>6){
System.out.println(string+"##"+elength+"##"+slength);
assertEquals(xalu.floatRepresentation(string, elength, slength), myALU.floatRepresentation(string, elength, slength));
random=0;
}
}
}
}
}
@org.junit.Test
public void ieee754() throws Exception {
String string;
for(int i=0;i<10000;i++){
string=String.valueOf(-250000+50*i);
string=string+"."+"0";
System.out.println(string);
assertEquals(xalu.ieee754(string,32),myALU.ieee754(string,32));
assertEquals(xalu.ieee754(string,64),myALU.ieee754(string,64));
}
}
@org.junit.Test
public void ieee7542() throws Exception {
String string;
for(int i=0;i<10000;i++){
string=String.valueOf(-2500+0.5D*i);
System.out.println("ieee7542 "+string);
assertEquals(xalu.ieee754(string,32),qALU.ieee754(string,32));
System.out.println("ieee7542 "+string);
assertEquals(xalu.ieee754(string,64),qALU.ieee754(string,64));
}
}
@org.junit.Test
public void integerTrueValue() throws Exception {
String[] strings4={
"0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101","0000"
};
String[] strings8=new String[0x100];
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
strings8[(i<<4)+j]=strings4[i]+strings4[j];
}
}
String temp=null;
String[] strings16=new String[0x10000];
for(int i=0;i<0x100;i++){
for(int j=0;j<0x100;j++){
// strings16[(i<<8)+j]=strings8[i]+strings8[j];
temp=strings8[i]+strings8[j];
assertEquals(xalu.integerTrueValue(temp),myALU.integerTrueValue(temp));
}
}
// String temp=null;
// String[] strings32=new String[0x7fffffff];
// for(int i=0;i<0x10000;i++){
// for(int j=0;j<0x10000;j++){
// temp=strings16[i]+strings16[j];
//
// }
// System.out.println(temp);
// }
// for(int i=0;i<0x7fffffff;i++){
// System.out.println(strings32[i]);
// assertEquals(alu.integerTrueValue(strings32[i]),wALU.integerTrueValue(strings32[i]));
// }
}
@org.junit.Test
public void integerTrueValue2() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
System.out.println(strings[i]);
assertEquals(xalu.integerTrueValue(strings[i]),qALU.integerTrueValue(strings[i]));
}
}
@org.junit.Test
public void floatTrueValue() throws Exception {
for(int j=0;j<200000;j++) {
long x = Double.doubleToLongBits(0.00000000000001D*j);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 64; i++) {
if ((x & (1L << (63 - i))) != 0) {
buffer.append('1');
} else {
buffer.append('0');
}
}
String test = new String(buffer).substring(0,buffer.length()-1);
// System.out.println(alu.floatTrueValue(test,11,52));
System.out.println(test);
double value=0.00000000000001D*j;
System.out.println(j);
System.out.printf("value %.10f\n",value*100000000000000D);
System.out.printf("value %.19e\n",value*100000000000000D);
assertTrue(true);
assertEquals(xalu.floatTrueValue(test, 11, 40), myALU.floatTrueValue(test, 11, 40));
}
}
@org.junit.Test
public void floatTrueValue2() throws Exception {
double temp=10000000000000000000000000000000000000000D;
temp=temp*temp;temp=temp*temp;
for(int j=0;j<200000;j=(int)(j+Math.random()*100)) {
long x = Double.doubleToLongBits(temp * j);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 64; i++) {
if ((x & (1L << (63 - i))) != 0) {
buffer.append('1');
} else {
buffer.append('0');
}
}
System.out.printf("%f\n",Double.longBitsToDouble(x));
String test = new String(buffer);
System.out.println(test);
assertEquals(xalu.floatTrueValue(test, 11, 52), qALU.floatTrueValue(test, 11, 52));
}
}
@org.junit.Test
public void negation() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++) {
assertEquals(xalu.negation(strings[i]),myALU.negation(strings[i]));
assertEquals(xalu.negation(strings[i]),qALU.negation(strings[i]));
}
}
@org.junit.Test
public void leftShift() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101",
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=1;i<32;i++) {
System.out.println(strings[i]);
System.out.println(xalu.leftShift(strings[i],3)+" "+myALU.leftShift(strings[i],3));
assertEquals(xalu.leftShift(strings[i],3),myALU.leftShift(strings[i],3));
// assertEquals(alu.leftShift(strings[i],2*i),qALU.leftShift(strings[i],2*i));
}
}
//w pass q fail
@org.junit.Test
public void logRightShift() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101",
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101",
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=1;i<16;i++) {
for(int j=0;j<16;j++) {
assertEquals(xalu.logRightShift(strings[i]+strings[j], 3), myALU.logRightShift(strings[i]+strings[j], 3));
assertEquals(xalu.logRightShift(strings[i]+strings[j], 3), qALU.logRightShift(strings[i]+strings[j], 3));
}
}
}
//w pass q fail
@org.junit.Test
public void ariRightShift() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101",
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101",
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=1;i<48;i++) {
for (int j = 0; j < 16; j++) {
assertEquals(xalu.ariRightShift(strings[i] + strings[j], 3), myALU.ariRightShift(strings[i] + strings[j], 3));
assertEquals(xalu.ariRightShift(strings[i] + strings[j], 3), qALU.ariRightShift(strings[i] + strings[j], 3));
}
}
}
@org.junit.Test
public void fullAdder() throws Exception {
assertEquals(xalu.fullAdder('1','1','1'),myALU.fullAdder('1','1','1'));
assertEquals(xalu.fullAdder('1','1','0'),myALU.fullAdder('1','1','0'));
assertEquals(xalu.fullAdder('1','0','0'),myALU.fullAdder('1','0','0'));
assertEquals(xalu.fullAdder('0','1','0'),myALU.fullAdder('0','1','0'));
assertEquals(xalu.fullAdder('0','1','1'),myALU.fullAdder('0','1','1'));
assertEquals(xalu.fullAdder('0','0','1'),myALU.fullAdder('0','0','1'));
assertEquals(xalu.fullAdder('0','0','0'),myALU.fullAdder('0','0','0'));
assertEquals(xalu.fullAdder('1','0','1'),myALU.fullAdder('1','0','1'));
}
@org.junit.Test
public void fullAdder2() throws Exception {
assertEquals(xalu.fullAdder('1','1','1'),qALU.fullAdder('1','1','1'));
assertEquals(xalu.fullAdder('1','1','0'),qALU.fullAdder('1','1','0'));
assertEquals(xalu.fullAdder('1','0','0'),qALU.fullAdder('1','0','0'));
assertEquals(xalu.fullAdder('0','1','0'),qALU.fullAdder('0','1','0'));
assertEquals(xalu.fullAdder('0','1','1'),qALU.fullAdder('0','1','1'));
assertEquals(xalu.fullAdder('0','0','1'),qALU.fullAdder('0','0','1'));
assertEquals(xalu.fullAdder('0','0','0'),qALU.fullAdder('0','0','0'));
assertEquals(xalu.fullAdder('1','0','1'),qALU.fullAdder('1','0','1'));
}
@org.junit.Test
public void claAdder() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
System.out.println(strings[i]+"##"+strings[j]);
assertEquals(xalu.claAdder(strings[i],strings[j],'1'),myALU.claAdder(strings[i],strings[j],'1'));
System.out.println(strings[i]+"#*#"+strings[j]);
assertEquals(xalu.claAdder(strings[i],strings[j],'0'),myALU.claAdder(strings[i],strings[j],'0'));
}
}
}
@org.junit.Test
public void claAdder2() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
System.out.println(strings[i]+"##"+strings[j]);
assertEquals(xalu.claAdder(strings[i],strings[j],'1'),qALU.claAdder(strings[i],strings[j],'1'));
System.out.println(strings[i]+"#*#"+strings[j]);
assertEquals(xalu.claAdder(strings[i],strings[j],'0'),qALU.claAdder(strings[i],strings[j],'0'));
}
}
}
//q pass w fail
@org.junit.Test
public void oneAdder() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
System.out.println(strings[i]);
// assertEquals(alu.oneAdder(strings[i]),wALU.oneAdder(strings[i]));
assertEquals(xalu.oneAdder(strings[i]),qALU.oneAdder(strings[i]));
}
}
@org.junit.Test
public void adder() throws Exception {
String[] strings={
"00", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","100","10","1","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
System.out.println(strings[i]+"##"+strings[j]);
assertEquals(xalu.adder(strings[i],strings[j],'1',4),myALU.adder(strings[i],strings[j],'1',4));
System.out.println(strings[i]+"#*#"+strings[j]);
assertEquals(xalu.adder(strings[i],strings[j],'0',4),myALU.adder(strings[i],strings[j],'0',4));
}
}
}
@org.junit.Test
public void adder2() throws Exception {
String[] strings={
"000", "001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","110","11","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
System.out.println(strings[i]+"##"+strings[j]);
assertEquals(xalu.adder(strings[i],strings[j],'1',4),qALU.adder(strings[i],strings[j],'1',4));
System.out.println(strings[i]+"#*#"+strings[j]);
assertEquals(xalu.adder(strings[i],strings[j],'0',4),qALU.adder(strings[i],strings[j],'0',4));
}
}
}
@org.junit.Test
public void integerAddition() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
System.out.println(strings[i]+"#*#"+strings[j]);
assertEquals(xalu.integerAddition(strings[i],strings[j],4),myALU.integerAddition(strings[i],strings[j],4));
}
}
}
@org.junit.Test
public void integerAddition2() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
System.out.println(strings[i]+"##"+strings[j]);
assertEquals(xalu.integerAddition(strings[i],strings[j],4),qALU.integerAddition(strings[i],strings[j],4));
}
}
}
@org.junit.Test
public void integerSubtraction() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
// System.out.println(strings[i]+"##"+strings[j]);
// assertEquals(alu.integerSubtraction(strings[i],strings[j],4),wALU.integerSubtraction(strings[i],strings[j],4));
System.out.println(strings[i]+"#*#"+strings[j]);
assertEquals(xalu.integerSubtraction(strings[i],strings[j],8),qALU.integerSubtraction(strings[i],strings[j],8));
}
}
}
@org.junit.Test
public void integerMultiplication() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
System.out.println(strings[i]+"##"+strings[j]);
assertEquals(xalu.integerMultiplication(strings[i],strings[j],4),myALU.integerMultiplication(strings[i],strings[j],4));
System.out.println(strings[i]+"#*#"+strings[j]);
assertEquals(xalu.integerMultiplication(strings[i],strings[j],8),myALU.integerMultiplication(strings[i],strings[j],8));
}
}
}
@org.junit.Test
public void integerDivision() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=0;i<16;i++){
for(int j=0;j<16;j++){
if(strings[j].equals("0000")){
continue;
}
System.out.println(strings[i]+"##"+strings[j]);
assertEquals(xalu.integerDivision(strings[i],strings[j],4),myALU.integerDivision(strings[i],strings[j],4));
System.out.println(strings[i]+"#*#"+strings[j]);
assertEquals(xalu.integerDivision(strings[i],strings[j],8),myALU.integerDivision(strings[i],strings[j],8));
}
}
}
@org.junit.Test
public void signedAddition() throws Exception {
String[] strings={
"0000", "0001", "0010", "0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1110","1111","1101"
};
for(int i=1;i<16;i++){
for(int j=1;j<16;j++){
System.out.println(strings[i]+"##"+strings[j]);
assertEquals(xalu.signedAddition(strings[i],strings[j],4),qALU.signedAddition(strings[i],strings[j],4));
}
}
}
@org.junit.Test
public void floatAddition() throws Exception {
}
@org.junit.Test
public void floatSubtraction() throws Exception {
}
@org.junit.Test
public void floatMultiplication() throws Exception {
}
@org.junit.Test
public void floatDivision() throws Exception {
}
}