toBinary implemented (not yet memory safe), all tests pass

This commit is contained in:
Josh Ashton
2023-10-09 10:52:07 -06:00
parent d143b3ab55
commit d179bf2a8d
2 changed files with 60 additions and 36 deletions
+43 -33
View File
@@ -9,53 +9,63 @@ int test_binaryToHex() {
} }
int test_toBinary_0_size5() { int test_toBinary_0_size5() {
char * binaryString = toBinary(0, 5); int size = 5;
char * expectedOutput = "00000"; int * binary = toBinary(0, size);
int expectedOutput[] = { 0, 0, 0, 0, 0 };
int cmp = strcmp(binaryString, expectedOutput); int cmp = 1;
for(int i = 0; i < size; i++)
if(cmp < 0 || cmp > 0) return 0; if(binary[i] != expectedOutput[i]) cmp = 0;
else return 1;
return cmp;
} }
int test_toBinary_0_size6() { int test_toBinary_0_size6() {
char * binaryString = toBinary(0, 6); int size = 6;
char * expectedOutput = "000000"; int * binary = toBinary(0, size);
int expectedOutput[] = { 0, 0, 0, 0, 0, 0 };
int cmp = strcmp(binaryString, expectedOutput); int cmp = 1;
for(int i = 0; i < size; i++)
if(cmp < 0 || cmp > 0) return 0; if(binary[i] != expectedOutput[i]) cmp = 0;
else return 1;
} return cmp;
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 test_toBinary_10_size6() { int test_toBinary_10_size6() {
char * binaryString = toBinary(10, 6); int size = 6;
char * expectedOutput = "001010"; 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; int test_toBinary_20_size6() {
else return 1; 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() { int test_toBinary_17_size16() {
char * binaryString = toBinary(17, 16); int size = 16;
char * expectedOutput = "0000000000010001"; 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); int cmp = 1;
for(int i = 0; i < size; i++)
if(cmp < 0 || cmp > 0) return 0; if(binary[i] != expectedOutput[i]) cmp = 0;
else return 1;
return cmp;
} }
int test_rToBinary() { int test_rToBinary() {
+17 -3
View File
@@ -1,13 +1,27 @@
#include <stdlib.h>
#include <stdio.h>
// Utility function to convert any given binary instruction into hexadecimal. // Utility function to convert any given binary instruction into hexadecimal.
char* binaryToHex(char* bin) { char* binaryToHex(char* bin) {
return "0x00000000"; return "0x00000000";
} }
// Utility function to convert a number to binary // Utility function to convert a number to binary
char* toBinary(int num, int size) { int* toBinary(int num, int size) {
char bin[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. // Represents the R instruction format.