-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_driver.c
More file actions
86 lines (70 loc) · 2.68 KB
/
Copy pathtest_driver.c
File metadata and controls
86 lines (70 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> // for fabs function
// Forward declaration of customFunction
void customFunction(int int1, int int2,
double double1, double double2,
int *outInt, double *outDouble);
// Helper function to parse keyword-based parameters
const char* getKeywordValue(const char *keyword, int argc, char *argv[]) {
int length = strlen(keyword);
for (int i = 1; i < argc; i++) {
if (strncmp(argv[i], keyword, length) == 0 && argv[i][length] == '=') {
return &argv[i][length + 1];
}
}
return NULL;
}
int main(int argc, char *argv[]) {
// Parse the inputs and expected outputs using keyword-based parameters
const char *int1Str = getKeywordValue("int1", argc, argv);
const char *int2Str = getKeywordValue("int2", argc, argv);
const char *double1Str = getKeywordValue("double1", argc, argv);
const char *double2Str = getKeywordValue("double2", argc, argv);
const char *expectedIntStr = getKeywordValue("expectedInt", argc, argv);
const char *expectedDoubleStr = getKeywordValue("expectedDouble", argc, argv);
const char *iterationsStr = getKeywordValue("iterations", argc, argv);
if (!int1Str || !int2Str || !double1Str || !double2Str ||
!expectedIntStr || !expectedDoubleStr || !iterationsStr) {
printf("Missing one or more required keyword-based parameters.\n");
return 1;
}
int int1 = atoi(int1Str);
int int2 = atoi(int2Str);
double double1 = atof(double1Str);
double double2 = atof(double2Str);
int expectedInt = atoi(expectedIntStr);
double expectedDouble = atof(expectedDoubleStr);
int iterations = atoi(iterationsStr);
printf("Running tests for %d iterations...\n", iterations);
// Variables to hold the outputs
int outInt = 0;
double outDouble = 0;
// Define a threshold for floating point comparison
const double THRESHOLD = 1e-9;
for (int i = 0; i < iterations; i++) {
// Call the function
customFunction(int1, int2, double1, double2, &outInt, &outDouble);
// Compare outputs
if (outInt != expectedInt ||
fabs(outDouble - expectedDouble) > THRESHOLD) {
// Print detailed failure information
printf("Test failed on iteration %d:\n", i + 1);
printf("Inputs:\n");
printf("\tInteger 1: %d\n", int1);
printf("\tInteger 2: %d\n", int2);
printf("\tDouble 1: %f\n", double1);
printf("\tDouble 2: %f\n", double2);
printf("Expected outputs:\n");
printf("\tInteger: %d\n", expectedInt);
printf("\tDouble: %f\n", expectedDouble);
printf("Actual outputs:\n");
printf("\tInteger: %d\n", outInt);
printf("\tDouble: %f\n", outDouble);
return 1; // Non-zero exit code for failure
}
}
printf("All tests passed!\n");
return 0; // Zero exit code for success
}