diff --git a/testing/tests/c/embedded_systems_testing/ASPIS_REPORT_Russo_Corvace.pdf b/testing/tests/c/embedded_systems_testing/ASPIS_REPORT_Russo_Corvace.pdf new file mode 100644 index 0000000..82e8270 Binary files /dev/null and b/testing/tests/c/embedded_systems_testing/ASPIS_REPORT_Russo_Corvace.pdf differ diff --git a/testing/tests/c/embedded_systems_testing/test1.c b/testing/tests/c/embedded_systems_testing/test1.c new file mode 100644 index 0000000..c63ec33 --- /dev/null +++ b/testing/tests/c/embedded_systems_testing/test1.c @@ -0,0 +1,74 @@ +#include +#include +#include + +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +void fill(int arr[], int size, int max) +{ + int count; + srand(6); + + for (count = 0; count < size; count++) + arr[count] = (rand() % max + 1); +} + +void display(int arr[], int size) +{ + int count; + printf("\n"); + for (count = 0; count < size; count++) + printf ("%4d", arr[count]); +} + +void bubbleSort(int arr[], int size) +{ + int x, y; + int temp; + for (x = 0; x < size - 1; x++) { + for (y = 0; y < size - 1; y++) + if (arr[y] > arr[y + 1]) { + temp = arr[y]; + arr[y] = arr[y + 1]; + arr[y + 1] = temp; + } + } +} + +int search(int arr[], int start, int end, int value) +{ + for (int i = 0; i < end; i++){ + if (arr[i] == value){ + return i; + } + } + return -1; +} + +int main(void) { + int numPositions = 21; + int max = 100; + int searchVal = 0; + int foundIndex = -1; + + int numbers[numPositions]; + fill(numbers, numPositions, max); + bubbleSort(numbers, numPositions); + display(numbers, numPositions); + + printf("\nEnter the number to search for (input 6): "); + scanf("%d", &searchVal); + foundIndex = search(numbers, 0, numPositions, searchVal); + + if (foundIndex == -1) + printf("\nThe number %d is not present in the array", searchVal); + else + printf("\nThe number %d is present at position %d", searchVal, foundIndex); + + display(numbers, numPositions); + + printf("\n\n"); + system("PAUSE"); + + return 0; +} \ No newline at end of file diff --git a/testing/tests/c/embedded_systems_testing/test2.c b/testing/tests/c/embedded_systems_testing/test2.c new file mode 100644 index 0000000..b373194 --- /dev/null +++ b/testing/tests/c/embedded_systems_testing/test2.c @@ -0,0 +1,23 @@ +#include +#include + +float calculate_critical_pressure(float temp, float volume) { + float partial_temp = temp * 0.0821; + + return partial_temp / volume; // set temp variable = 9999 to exceed the threshold of 4.0 +} + +int main() { + float temperature = 100.5; + srand(6); + float volume = rand() % 10 + 0.1; // 10 + + float pressure = calculate_critical_pressure(temperature, volume); + + if (pressure > 4.0) { + printf("ALARM: Excessive pressure! (%.2f)\n", pressure); + } else { + printf("System stable. Pressure: %.2f\n", pressure); + } + return 0; +} \ No newline at end of file diff --git a/testing/tests/c/embedded_systems_testing/test3.c b/testing/tests/c/embedded_systems_testing/test3.c new file mode 100644 index 0000000..ece215c --- /dev/null +++ b/testing/tests/c/embedded_systems_testing/test3.c @@ -0,0 +1,36 @@ +#include +#include + +typedef struct { + float q; // Process noise + float r; // Measurement noise + float x; // Estimated value + float p; // Estimation error + float k; // Kalman gain +} KalmanFilter; + +int main() { + KalmanFilter f = {0.1, 0.5, 0.0, 1.0, 0.0}; + float measurements[10]; + srand(6); + + // Simulating 10 readings + for (int count = 0; count < 10; count++) + measurements[count] = (rand() % 100 + 1); + // Generated measurements: {42.0, 86.0, 13.0, 66.0, 9.0, 86.0, 87.0, 44.0, 3.0, 79.0}; + + printf("Starting trajectory estimation...\n"); + for (int i = 0; i < 10; i++) { + + f.p = f.p + f.q; + // Update phase + f.k = f.p / (f.p + f.r); + f.x = f.x + f.k * (measurements[i] - f.x); + + f.p = (1 - f.k) * f.p; + + printf("\tStep %d - Measurement: %.1f, Calculated estimate: %.2f\n", i, measurements[i], f.x); + } + + return 0; +} \ No newline at end of file diff --git a/testing/tests/c/embedded_systems_testing/test4.c b/testing/tests/c/embedded_systems_testing/test4.c new file mode 100644 index 0000000..afc12d7 --- /dev/null +++ b/testing/tests/c/embedded_systems_testing/test4.c @@ -0,0 +1,15 @@ +// target.c +#include +#include + +int main() { + int counter = 0; + + for (int i = 0; i < 10; i++) { + counter += 2; + printf("Iteration %d: counter = %d\n", i, counter); + } + + printf("Final result: %d\n", counter); + return 0; +} \ No newline at end of file diff --git a/testing/tests/c/embedded_systems_testing/test5.c b/testing/tests/c/embedded_systems_testing/test5.c new file mode 100644 index 0000000..57f7ec0 --- /dev/null +++ b/testing/tests/c/embedded_systems_testing/test5.c @@ -0,0 +1,25 @@ +#include +#include + +int main() { + + int var = 0; + + switch (var) + { + case 0: + printf("0\n"); + break; + case 1: + printf("1\n"); + break; + case 2: + printf("2\n"); + break; + default: + printf("default\n"); + break; + } + + return 0; +} \ No newline at end of file diff --git a/testing/tests/c/embedded_systems_testing/test6.c b/testing/tests/c/embedded_systems_testing/test6.c new file mode 100644 index 0000000..d82fbaa --- /dev/null +++ b/testing/tests/c/embedded_systems_testing/test6.c @@ -0,0 +1,34 @@ +#include +#include +__attribute__((annotate("to_duplicate"))) +int* createMalloc(){ + int* arr = (int*)malloc(5 * sizeof(int)); + return arr; +} +int main() { + int num_elements = 5; + int* arr = createMalloc(); + + if (arr == NULL) { + printf("Error: memory allocation failed!\n"); + return 1; + } + + for (int i = 0; i < num_elements; i++) { + arr[i] = i * 10; + } + + printf("Memory successfully allocated at address: %p\n", (void*)arr); + // set arr = arr + 1 + + printf("Reading values from the Heap:\n"); + + for (int i = 0; i < num_elements; i++) { + printf("arr[%d] = %d\n", i, arr[i]); + } + + free(arr); + printf("Memory freed. Execution finished.\n"); + + return 0; +} \ No newline at end of file