Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
74 changes: 74 additions & 0 deletions testing/tests/c/embedded_systems_testing/test1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* 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;
}
23 changes: 23 additions & 0 deletions testing/tests/c/embedded_systems_testing/test2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
36 changes: 36 additions & 0 deletions testing/tests/c/embedded_systems_testing/test3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
15 changes: 15 additions & 0 deletions testing/tests/c/embedded_systems_testing/test4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// target.c
#include <stdio.h>
#include <unistd.h>

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;
}
25 changes: 25 additions & 0 deletions testing/tests/c/embedded_systems_testing/test5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <string.h>

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;
}
34 changes: 34 additions & 0 deletions testing/tests/c/embedded_systems_testing/test6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
__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;
}