-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_file.c
105 lines (83 loc) · 2.6 KB
/
test_file.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "helpers.h" // Include your helper functions header
#include <stdio.h>
#include <math.h>
// Function to validate mean calculation
int validate_mean(double mean) {
// Round the mean to the nearest integer
double rounded_mean = round(mean);
if (!(fabs(mean = rounded_mean) ) ){ // Tolerance of 0.5 for rounding to nearest integer
printf("Mean validation failed: Calculated mean is %.f, \n", mean);
return -1;
}
return 1;
}
// Function to validate variance calculation
int validate_variance(double variance) {
// Variance should be non-negative
if (variance < 0) {
printf("Variance validation failed:\n");
return 0;
}
return 1;
}
// Function to validate standard deviation calculation
int validate_standard_deviation(double standard_deviation) {
if (standard_deviation < 0) {
printf("Standard Deviation validation failed:\n");
return 0;
}
return 1;
}
int validate_median(double median) {
return 1; // Placeholder for validation
}
int main() {
const char *file_path = "data.txt"; // Path to your data file (assuming it's named data.txt)
struct IntArray intArray;
int n = Read_data_from_file(file_path, &intArray);
if (n < 0) {
printf("Error reading data from file.\n");
return 1;
}
int *data = intArray.arr;
// Check if the array size is valid
if (n <= 0) {
printf("Invalid data size.\n");
free(data); // Free allocated memory
return 1;
}
// Calculate mean
double mean = Calculate_mean(data, n);
// Validate mean calculation
if (!validate_mean(mean)) {
printf("Mean calculation failed.\n");
free(data); // Free allocated memory
return 1;
}
// Validate median calculation
double variance = Calculate_variance(data, n);
if (!validate_variance(variance)) {
printf("Variance calculation failed.\n");
free(data); // Free allocated memory
return 1;
}
double standard_deviation = Calculate_standard_deviation(variance);
if (!validate_standard_deviation(standard_deviation)) {
printf("Standard deviation calculation failed.\n");
free(data); // Free allocated memory
return 1;
}
double median = Calculate_median(data, n);
if (!validate_median(median)) {
printf("Median calculation failed.\n");
free(data); // Free allocated memory
return 1;
}
// Print test results
free(data); // Free allocated memory
printf("PASS. OK\n");
return 0;
}