memory safety & documentation added
This commit is contained in:
@@ -8,6 +8,7 @@ int test_binaryToHex() {
|
||||
return 1; // TODO
|
||||
}
|
||||
|
||||
// Testing the toBinary function with an input of 0 and a size of 5.
|
||||
int test_toBinary_0_size5() {
|
||||
int size = 5;
|
||||
int * binary = toBinary(0, size);
|
||||
@@ -17,9 +18,11 @@ int test_toBinary_0_size5() {
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
free(binary);
|
||||
return cmp;
|
||||
}
|
||||
|
||||
// Testing the toBinary function with an input of 0 and a size of 6.
|
||||
int test_toBinary_0_size6() {
|
||||
int size = 6;
|
||||
int * binary = toBinary(0, size);
|
||||
@@ -29,9 +32,11 @@ int test_toBinary_0_size6() {
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
free(binary);
|
||||
return cmp;
|
||||
}
|
||||
|
||||
// Testing the toBinary function with an input of 10 and a size of 6.
|
||||
int test_toBinary_10_size6() {
|
||||
int size = 6;
|
||||
int * binary = toBinary(10, size);
|
||||
@@ -41,9 +46,11 @@ int test_toBinary_10_size6() {
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
free(binary);
|
||||
return cmp;
|
||||
}
|
||||
|
||||
// Testing the toBinary function with an input of 20 and a size of 6.
|
||||
int test_toBinary_20_size6() {
|
||||
int size = 6;
|
||||
int * binary = toBinary(20, size);
|
||||
@@ -53,9 +60,11 @@ int test_toBinary_20_size6() {
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
free(binary);
|
||||
return cmp;
|
||||
}
|
||||
|
||||
// Testing the toBinary function with an input of 17 and a size of 16.
|
||||
int test_toBinary_17_size16() {
|
||||
int size = 16;
|
||||
int * binary = toBinary(17, size);
|
||||
@@ -65,6 +74,7 @@ int test_toBinary_17_size16() {
|
||||
for(int i = 0; i < size; i++)
|
||||
if(binary[i] != expectedOutput[i]) cmp = 0;
|
||||
|
||||
free(binary);
|
||||
return cmp;
|
||||
}
|
||||
|
||||
|
||||
+5
-6
@@ -8,12 +8,17 @@ char* binaryToHex(char* bin) {
|
||||
|
||||
// Utility function to convert a number to binary
|
||||
int* toBinary(int num, int size) {
|
||||
// Dynamically allows for different sized arrays. Need to manually release from memory.
|
||||
int * bin = (int*)calloc(size, sizeof(int));
|
||||
|
||||
// Fill up the array with default values.
|
||||
for(int i = 0; i < size; i++)
|
||||
bin[i] = 0;
|
||||
|
||||
if(num == 0) return bin;
|
||||
|
||||
// Since the binary values are calculated in reverse order, entering the values into the array
|
||||
// in reverse order will negate this, and leave us with an array in the correct order.
|
||||
int i = size - 1;
|
||||
while(num > 0) {
|
||||
bin[i] = num % 2;
|
||||
@@ -43,12 +48,6 @@ typedef struct r {
|
||||
char* rToBinary(r* format) {
|
||||
char binary[32];
|
||||
|
||||
char* opcode = toBinary(format->opcode, format->opcode_size);
|
||||
if(format->opcode == 0)
|
||||
for(int i = 0; i < 6; i++) binary[i] = 0;
|
||||
|
||||
|
||||
|
||||
return binary;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user