Skip to content

Commit bf62e97

Browse files
committed
Unit Testing prototypes
1 parent 80b9e1c commit bf62e97

21 files changed

+347
-146
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# All test files
2+
test.*
3+
4+
# All executable files
5+
*.exe
6+
17
# Windows image file caches
28
Thumbs.db
39
ehthumbs.db
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//Deprecated

C/Plaindrome/Plaindrome.c

+18-9
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int main() {
4646
const short SIZE = 26;
4747
const short zerothSIZE = SIZE - 1;
4848
short freq[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
49-
short c,size = 0;
49+
short i,c,size = 0;
5050

5151
while(c != '\n'){ //input anagram of palindrome
5252
c = getchar();
@@ -58,10 +58,10 @@ int main() {
5858
}
5959

6060
short odd = 0, freqSize = 0; //test for palindrome qualities
61-
for(c = 0; c<SIZE; ++c){
62-
if(freq[c] > 0){
61+
for(i = 0; i<SIZE; ++i){
62+
if(freq[i] > 0){
6363
++freqSize;
64-
if(freq[c] % 2 != 0)
64+
if(freq[i] % 2 != 0)
6565
++odd;
6666
}
6767
}
@@ -72,15 +72,24 @@ int main() {
7272
reversedHeapsort(freq, SIZE);
7373

7474
short tscs = 0; //tscs stands for total state changing swaps
75-
for(c = 0; c < freqSize; ++ c){
76-
tscs += freq[c] * (size - freq[c]);
75+
for(i = 0; i < freqSize; ++i){
76+
tscs += freq[i] * (size - freq[i]);
7777
}
7878
tscs /= 2;
7979

80-
printf("Total states: %d\n", fact(size)/arrayFact(freq, freqSize, 0));
80+
int totalStates = fact(size)/arrayFact(freq, freqSize, 0);
81+
int posSwaps = size * (size - 1) / 2;
82+
83+
printf("Total states: %d\n", totalStates);
8184
printf("Total state changing swaps: %d\n", tscs);
82-
printf("All possible swaps per state: %d\n", size * (size - 1) / 2);
85+
printf("All possible swaps per state: %d\n", posSwaps);
86+
87+
double** MarkovMatrix;
88+
MarkovMatrix = (double**)calloc(sizeof(double*) * totalStates);
89+
MarkovMatrix[0] = (double*)calloc(sizeof(double) * totalStates * totalStates); // rows * columns
90+
91+
InitializeMatrix(freq, freqSize, stateSize, MarkovMatrix[i], totalStates, i, tscs);
8392
}
8493

85-
return 0; //Todo List: string->step, state matrix initialization, matrix inversion, estimated step calculation: https://en.wikipedia.org/wiki/Absorbing_Markov_chain#Fundamental_matrix
94+
return 0; //Todo List: matrix inversion, estimated step calculation: https://en.wikipedia.org/wiki/Absorbing_Markov_chain#Fundamental_matrix
8695
}

C/Plaindrome/PlaindromeMisc.h

-137
This file was deleted.
File renamed without changes.

C/Plaindrome/arrayCompare.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
short shortArrayCompare (const short x[], const short y[], short size){
2+
short i;
3+
for(i = 0; i < size; ++i){
4+
if(x[i] > y[i])
5+
return 1;
6+
if(x[i] < y[i])
7+
return -1;
8+
}
9+
return 0;
10+
};
11+
12+
short intArrayCompare (const int x[], const int y[], short size){
13+
short i;
14+
for(i = 0; i < size; ++i){
15+
if(x[i] > y[i])
16+
return 1;
17+
if(x[i] < y[i])
18+
return -1;
19+
}
20+
return 0;
21+
};

C/Plaindrome/arrayCompareTest.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <stdio.h>
2+
#include "arrayCompare.c"
3+
4+
int main() {
5+
{
6+
short x[] = {3, 2, 2};
7+
short y[] = {3, 2, 2};
8+
short arraySize = 3;
9+
10+
if(!shortArrayCompare(x, y, arraySize))
11+
puts("Short Arrays: {3, 2, 2} == {3, 2, 2} : Passed");
12+
else
13+
puts("Short Arrays: {3, 2, 2} == {3, 2, 2} : Failed");
14+
}
15+
16+
{
17+
int x[] = {3, 2, 2};
18+
int y[] = {3, 2, 2};
19+
short arraySize = 3;
20+
21+
if(!intArrayCompare(x, y, arraySize))
22+
puts("Int Arrays: {3, 2, 2} == {3, 2, 2} : Passed");
23+
else
24+
puts("Int Arrays: {3, 2, 2} == {3, 2, 2} : Failed");
25+
}
26+
27+
return 0;
28+
}

C/Plaindrome/arrayFactorial.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "Factorial.c"
2+
3+
int arrayFactorial(const short x[], short size, short startingIndex, short minus){ //~7/1-17:00 returns sum of each element's factorial O(N)
4+
short i = startingIndex;
5+
int total = factorial(x[i++] - minus);
6+
while(i < size){
7+
total *= factorial(x[i++]);
8+
}
9+
return total;
10+
};

C/Plaindrome/arrayFactorialTest.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
#include "arrayFactorial.c"
3+
4+
int main() {
5+
short array[] = {2, 3, 2};
6+
short arraySize = 3;
7+
short startingIndex = 0;
8+
short minus = 1;
9+
if(arrayFactorial(array, arraySize, startingIndex, minus) == 12)
10+
puts("arrayFactorial({2, 3, 2}, arraySize: 3, startingIndex: 0, minus: 1) == 12 : Passed");
11+
else
12+
puts("arrayFactorial({2, 3, 2}, arraySize: 3, startingIndex: 0, minus: 1) == 12 : Failed");
13+
return 0;
14+
}

C/Plaindrome/arraySum.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
short arraySum (const short x[], short size){
2+
int total = 0;
3+
short i;
4+
for(i = 0; i < size; ++i){
5+
total += x[i];
6+
}
7+
return total;
8+
};

C/Plaindrome/arraySumTest.c

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
#include "arraySum.c"
3+
4+
int main() {
5+
short array[] = {2, 3, 2};
6+
short arraySize = 3;
7+
if(arraySum(array, arraySize) == 7)
8+
puts("arraySum(array: {2, 3, 2}, arraySize: 3) == 7 : Passed");
9+
else
10+
puts("arraySum(array: {2, 3, 2}, arraySize: 3) == 7 : Failed");
11+
return 0;
12+
}

C/Plaindrome/factorial.c

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int factorial(short x){
2+
int y = 1;
3+
while(x > 1){
4+
y *= x;
5+
--x;
6+
}
7+
return y;
8+
};

C/Plaindrome/factorialTest.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
#include "Factorial.c"
3+
4+
int main() {
5+
if(factorial(4) == 24)
6+
puts("factorial(4) == 24 : Passed");
7+
else
8+
puts("factorial(4) == 24 : Failed");
9+
return 0;
10+
}

C/Plaindrome/printArray.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdio.h>
2+
3+
void printShortArray (const char* x, const short y[], short size){
4+
fputs(x, stdout);
5+
short i;
6+
for(i = 0; i < size; ++i)
7+
printf("%d ", y[i]);
8+
puts("");
9+
};
10+
11+
void printIntArray (const char* x, const int y[], short size){
12+
fputs(x, stdout);
13+
short i;
14+
for(i = 0; i < size; ++i)
15+
printf("%d ", y[i]);
16+
puts("");
17+
};

C/Plaindrome/printArrayTest.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "printArray.c"
2+
3+
void main(){
4+
short x[] = {0, -1, 2};
5+
int y[] = {3, -4, 5};
6+
7+
printShortArray("x: ", x, 3);
8+
printIntArray("y: ", y, 3);
9+
}

C/Plaindrome/promotionSignature.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "arrayFactorial.c"
2+
3+
/*creates an array that shows the number of steps required to promote a letter in the same digit.
4+
Example: [a, a, b] has the signature of [2, 1] because promoting a[0] requires going through both combinations of a[1] and a[2] while promoting a[1] merely requires switching a[1] and a[2].*/
5+
void promotionSignature (const short freq[], short freqSize, int result[], short resultSize, short startingIndex){
6+
short i, j = 0, k = 0;
7+
while(freq[j] == 0){
8+
++j;
9+
}
10+
11+
short remainingDigits = resultSize - startingIndex;
12+
13+
for(i = 0; i < startingIndex; ++i){
14+
result[i] = -1;
15+
}
16+
for(; i < resultSize; ++i){
17+
result[i] = factorial(remainingDigits) / arrayFactorial(freq, freqSize, j, k);
18+
if(!result[i])
19+
++result[i];
20+
++k;
21+
if(k == freq[j]){
22+
++j;
23+
k = 0;
24+
}
25+
--remainingDigits;
26+
}
27+
};

0 commit comments

Comments
 (0)