Initial commit: Pi calculation benchmark with 34 languages
- Added implementations for: bash, brainfuck, c, cpp, crystal, csharp, d, dart, elixir, erlang, fortran, go, haskell, java, javascript, julia, kotlin, objective-c, scala, typescript, lua, nim, odin, perl, php, python, r, ruby, rust, swift, zig, assembly, vimscript, wolfram - All implementations use Machin's formula: π/4 = 4*arctan(1/5) - arctan(1/239) - Build system with ./build.sh, test system with ./test.sh - Performance testing with ./run_all.sh - Comprehensive README.md explaining performance differences - Test framework verifies correctness against known π values
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define SCRIPT_PATH "/Users/einand/Code/test/c/print_hej"
|
||||
|
||||
char* run_script(const char* args) {
|
||||
char cmd[256];
|
||||
if (args && strlen(args) > 0) {
|
||||
snprintf(cmd, sizeof(cmd), "%s %s", SCRIPT_PATH, args);
|
||||
} else {
|
||||
snprintf(cmd, sizeof(cmd), "%s", SCRIPT_PATH);
|
||||
}
|
||||
|
||||
FILE* pipe = popen(cmd, "r");
|
||||
if (!pipe) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* result = malloc(100000);
|
||||
if (!result) {
|
||||
pclose(pipe);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fgets(result, 100000, pipe) == NULL) {
|
||||
free(result);
|
||||
pclose(pipe);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Remove trailing newline
|
||||
size_t len = strlen(result);
|
||||
if (len > 0 && result[len-1] == '\n') {
|
||||
result[len-1] = '\0';
|
||||
}
|
||||
|
||||
pclose(pipe);
|
||||
return result;
|
||||
}
|
||||
|
||||
void test_10_decimals() {
|
||||
char* result = run_script("10");
|
||||
const char* expected = "3.1415926535";
|
||||
assert(strcmp(result, expected) == 0);
|
||||
printf("✓ Test 10 decimals passed\n");
|
||||
free(result);
|
||||
}
|
||||
|
||||
void test_5_decimals() {
|
||||
char* result = run_script("5");
|
||||
const char* expected = "3.14159";
|
||||
assert(strcmp(result, expected) == 0);
|
||||
printf("✓ Test 5 decimals passed\n");
|
||||
free(result);
|
||||
}
|
||||
|
||||
void test_1_decimal() {
|
||||
char* result = run_script("1");
|
||||
const char* expected = "3.1";
|
||||
assert(strcmp(result, expected) == 0);
|
||||
printf("✓ Test 1 decimal passed\n");
|
||||
free(result);
|
||||
}
|
||||
|
||||
void test_100_decimals() {
|
||||
char* result = run_script("100");
|
||||
const char* expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
|
||||
assert(strcmp(result, expected) == 0);
|
||||
printf("✓ Test 100 decimals passed\n");
|
||||
free(result);
|
||||
}
|
||||
|
||||
void test_default_100_decimals() {
|
||||
char* result = run_script("");
|
||||
const char* expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
|
||||
assert(strcmp(result, expected) == 0);
|
||||
printf("✓ Test default 100 decimals passed\n");
|
||||
free(result);
|
||||
}
|
||||
|
||||
void test_10000_decimals() {
|
||||
char* result = run_script("10000");
|
||||
// Check length: "3." + 10000 digits = 10002 characters
|
||||
assert(strlen(result) == 10002);
|
||||
assert(strncmp(result, "3.14159", 7) == 0);
|
||||
printf("✓ Test 10000 decimals passed\n");
|
||||
free(result);
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("Running C unit tests...\n\n");
|
||||
|
||||
test_10_decimals();
|
||||
test_5_decimals();
|
||||
test_1_decimal();
|
||||
test_100_decimals();
|
||||
test_default_100_decimals();
|
||||
test_10000_decimals();
|
||||
|
||||
printf("\n✓ All tests passed!\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user