toBinary implemented (not yet memory safe), all tests pass
This commit is contained in:
+43
-33
@@ -9,53 +9,63 @@ int test_binaryToHex() {
|
||||
}
|
||||
|
||||
int test_toBinary_0_size5() {
|
||||
char * binaryString = toBinary(0, 5);
|
||||
char * expectedOutput = "00000";
|
||||
int size = 5;
|
||||
int * binary = toBinary(0, size);
|
||||
int expectedOutput[] = { 0, 0, 0, 0, 0 };
|
||||
|
||||
int cmp = strcmp(binaryString, expectedOutput);
|
||||
|
||||
if(cmp < 0 || cmp > 0) return 0;
|
||||
else return 1;
|
||||
int cmp = 1;
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
return cmp;
|
||||
}
|
||||
|
||||
int test_toBinary_0_size6() {
|
||||
char * binaryString = toBinary(0, 6);
|
||||
char * expectedOutput = "000000";
|
||||
int size = 6;
|
||||
int * binary = toBinary(0, size);
|
||||
int expectedOutput[] = { 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
int cmp = strcmp(binaryString, expectedOutput);
|
||||
|
||||
if(cmp < 0 || cmp > 0) return 0;
|
||||
else return 1;
|
||||
}
|
||||
|
||||
int test_toBinary_20_size6() {
|
||||
char * binaryString = toBinary(20, 6);
|
||||
char * expectedOutput = "010100";
|
||||
|
||||
int cmp = strcmp(binaryString, expectedOutput);
|
||||
|
||||
if(cmp < 0 || cmp > 0) return 0;
|
||||
else return 1;
|
||||
int cmp = 1;
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
return cmp;
|
||||
}
|
||||
|
||||
int test_toBinary_10_size6() {
|
||||
char * binaryString = toBinary(10, 6);
|
||||
char * expectedOutput = "001010";
|
||||
int size = 6;
|
||||
int * binary = toBinary(10, size);
|
||||
int expectedOutput[] = { 0, 0, 1, 0, 1, 0 };
|
||||
|
||||
int cmp = strcmp(binaryString, expectedOutput);
|
||||
int cmp = 1;
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
return cmp;
|
||||
}
|
||||
|
||||
if(cmp < 0 || cmp > 0) return 0;
|
||||
else return 1;
|
||||
int test_toBinary_20_size6() {
|
||||
int size = 6;
|
||||
int * binary = toBinary(20, size);
|
||||
int expectedOutput[] = { 0, 1, 0, 1, 0, 0 };
|
||||
|
||||
int cmp = 1;
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
return cmp;
|
||||
}
|
||||
|
||||
int test_toBinary_17_size16() {
|
||||
char * binaryString = toBinary(17, 16);
|
||||
char * expectedOutput = "0000000000010001";
|
||||
int size = 16;
|
||||
int * binary = toBinary(17, size);
|
||||
int expectedOutput[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1};
|
||||
|
||||
int cmp = strcmp(binaryString, expectedOutput);
|
||||
|
||||
if(cmp < 0 || cmp > 0) return 0;
|
||||
else return 1;
|
||||
int cmp = 1;
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
return cmp;
|
||||
}
|
||||
|
||||
int test_rToBinary() {
|
||||
|
||||
+17
-3
@@ -1,13 +1,27 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Utility function to convert any given binary instruction into hexadecimal.
|
||||
char* binaryToHex(char* bin) {
|
||||
return "0x00000000";
|
||||
}
|
||||
|
||||
// Utility function to convert a number to binary
|
||||
char* toBinary(int num, int size) {
|
||||
char bin[size];
|
||||
int* toBinary(int num, int size) {
|
||||
int * bin = (int*)calloc(size, sizeof(int));
|
||||
for(int i = 0; i < size; i++)
|
||||
bin[i] = 0;
|
||||
|
||||
return "000000"; // TODO
|
||||
if(num == 0) return bin;
|
||||
|
||||
int i = size - 1;
|
||||
while(num > 0) {
|
||||
bin[i] = num % 2;
|
||||
num = num / 2;
|
||||
i--;
|
||||
}
|
||||
|
||||
return bin;
|
||||
}
|
||||
|
||||
// Represents the R instruction format.
|
||||
|
||||
Reference in New Issue
Block a user