Initial commit

This commit is contained in:
Isaac Shoebottom 2023-11-17 12:40:31 -04:00
commit 001e3638fe
10 changed files with 211 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

8
.idea/cmake.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeSharedSettings">
<configurations>
<configuration PROFILE_NAME="Debug" ENABLED="true" GENERATION_DIR="build" CONFIG_NAME="Debug" />
</configurations>
</component>
</project>

2
.idea/foreach.iml Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/foreach.iml" filepath="$PROJECT_DIR$/.idea/foreach.iml" />
</modules>
</component>
</project>

7
.idea/vcs.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

7
CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.22)
project(foreach C)
set(CMAKE_C_STANDARD 99)
include_directories(.)
add_executable(foreach test.c)

1
README.md Normal file
View File

@ -0,0 +1 @@
# Simple foreach C macro

9
foreach.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
// Will determine the size of an array only if it is declared on the stack
#define foreach(type, item, array) \
for (type* item = array; item < array + (sizeof(array) / sizeof(type)); item++)
// Need to pass in the size of the array, since heap allocated arrays don't work with sizeof
#define foreach_p(type, item, array, size) \
for (type* item = array; item < array + size; item++)

157
test.c Normal file
View File

@ -0,0 +1,157 @@
// Ignore rand warning
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cert-msc50-cpp"
#pragma ide diagnostic ignored "cert-msc51-cpp"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "foreach.h"
int rand_range(int min, int max) {
return rand() % (max - min + 1) + min;
}
bool test_simpleArray() {
int arr[] = {1, 2, 3, 4, 5};
int test[5];
int counter = 0;
foreach(int, item, arr) {
test[counter] = *item;
counter++;
}
if (memcmp(arr, test, sizeof(arr)) == 0) {
return true;
} else {
return false;
}
}
bool test_randomNumbers() {
int arr1[10];
srand(0);
foreach(int, item, arr1) {
*item = rand_range(0, 100);
}
int arr2[10];
srand(0);
for (int i = 0; i < sizeof(arr2) / sizeof(int); i++) {
arr2[i] = rand_range(0, 100);
}
if (memcmp(arr1, arr2, sizeof(arr1)) == 0) {
return true;
} else {
return false;
}
}
bool test_simpleString() {
char *str = "Hello, World!";
// Plus one for the null terminator
char test[strlen(str) + 1];
int counter = 0;
foreach_p(char, item, str, strlen(str) + 1) {
test[counter] = *item;
counter++;
}
if (strcmp(str, test) == 0) {
return true;
} else {
return false;
}
}
bool test_randomNumbersAsPointers() {
int arr1[10];
srand(0);
foreach_p(int, item, arr1, sizeof(arr1) / sizeof(int)) {
*item = rand_range(0, 100);
}
int arr2[10];
srand(0);
for (int i = 0; i < sizeof(arr2) / sizeof(int); i++) {
arr2[i] = rand_range(0, 100);
}
if (memcmp(arr1, arr2, sizeof(arr1)) == 0) {
return true;
} else {
return false;
}
}
bool test_randomNumbersOnHeap() {
int *arr1 = malloc(sizeof(int) * 10);
srand(0);
foreach_p(int, item, arr1, 10) {
*item = rand_range(0, 100);
}
int *arr2 = malloc(sizeof(int) * 10);
srand(0);
for (int i = 0; i < 10; i++) {
arr2[i] = rand_range(0, 100);
}
bool result = memcmp(arr1, arr2, 10) == 0;
free(arr1);
free(arr2);
return result;
}
bool test_stringOnHeap() {
char *str = malloc(sizeof(char) * 14);
strcpy(str, "Hello, World!");
char *test = malloc(sizeof(char) * 14);
int counter = 0;
foreach_p(char, item, str, 14) {
test[counter] = *item;
counter++;
}
bool result = strcmp(str, test) == 0;
free(str);
free(test);
return result;
}
int main() {
void* foreach_functions[] = {
test_simpleArray,
test_randomNumbers,
};
void* foreach_p_functions[] = {
test_simpleString,
test_randomNumbersAsPointers,
test_randomNumbersOnHeap,
test_stringOnHeap
};
printf("Foreach tests\n");
for (int i = 0; i < sizeof(foreach_functions) / sizeof(void*); i++) {
bool result = ((bool (*)()) foreach_functions[i])();
if (result) {
printf("Test %d passed\n", i);
} else {
printf("Test %d failed\n", i);
}
}
printf("Foreach_p tests\n");
for (int i = 0; i < sizeof(foreach_p_functions) / sizeof(void*); i++) {
bool result = ((bool (*)()) foreach_p_functions[i])();
if (result) {
printf("Test %d passed\n", i);
} else {
printf("Test %d failed\n", i);
}
}
return EXIT_SUCCESS;
}
#pragma clang diagnostic pop