From d179bf2a8d604c9a6df95b4eca5df8dde4be7962 Mon Sep 17 00:00:00 2001 From: Josh Ashton Date: Mon, 9 Oct 2023 10:52:07 -0600 Subject: [PATCH] toBinary implemented (not yet memory safe), all tests pass --- src/test/testFormats.c | 76 ++++++++++++++++++++++++------------------ src/util/formats.h | 20 +++++++++-- 2 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/test/testFormats.c b/src/test/testFormats.c index 3c194c5..f78a328 100644 --- a/src/test/testFormats.c +++ b/src/test/testFormats.c @@ -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() { diff --git a/src/util/formats.h b/src/util/formats.h index f3defd3..2a37961 100644 --- a/src/util/formats.h +++ b/src/util/formats.h @@ -1,13 +1,27 @@ +#include +#include + // 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.