Skip to content

Commit

Permalink
Merge pull request #3 from an-prata/dev-v2
Browse files Browse the repository at this point in the history
Scrutiny version 2!
  • Loading branch information
an-prata authored Jul 10, 2023
2 parents 67103e4 + 30980d6 commit 297fa68
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 1,572 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
examples/a.out
build/scrutiny.a
build/scrutiny.o
build
34 changes: 31 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
#!/usr/bin/bash

mkdir -p build/
gcc -Wall -Wextra -c scrutiny/scrutiny.c -o build/scrutiny.o
ar rcs build/scrutiny.a build/scrutiny.o
# script assumes that the basename of the current directory (repository root)
# is the same name as the folder containing the projects source files.

PROJECT_NAME="$(basename $(pwd))"
SOURCE_FILES=$(find "./$PROJECT_NAME/" -name "*.c")
HEADER_FILES=$(find "./$PROJECT_NAME/" -name "*.h")
HEADER_LIST=""

for HEADER in $HEADER_FILES; do
HEADER_LIST="$HEADER_LIST $HEADER"
done

mkdir build -p

for FILE in $SOURCE_FILES; do
gcc -Wall -Wextra -c $FILE -o "build/$(basename $FILE).o"

if [ "$?" != "0" ]; then
exit 1
fi
done

tar --create --file "build/$PROJECT_NAME-headers.tar" $HEADER_LIST

OBJECT_FILES=$(find "./build/" -name "*.o" -printf "%p ")

ar rcs "build/$PROJECT_NAME.a" $OBJECT_FILES

if [ "$?" != "0" ]; then
exit 1
fi

48 changes: 20 additions & 28 deletions examples/benchmarks.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,47 @@
#include "../scrutiny/scrutiny.h"
#include <time.h>
#include <unistd.h>

static bool is_prime(uint64_t n)
{
static bool is_prime(unsigned long n) {
if (n == 0 || n == 1)
return false;

for (uint64_t i = 2; i < n / 2; i++)
for (unsigned long i = 2; i < n / 2; i++)
if (n % i == 0)
return false;

return true;
}

SCRUTINY_BENCHMARK primes_to_n_benchmark(void)
{
const uint64_t n = 4096;
uint64_t largest_prime = 0;

/* Starts the benchmark timer. */
scrutiny_benchamrk_start();
SCRUTINY_BENCHMARK(primes_to_n) {
const unsigned long n = 4096;
unsigned long largest_prime = 0;

/* Benchmark code, here we find the largest prime up to n. */
for (uint64_t i = 0; i <= n; i++)
for (unsigned long i = 0; i <= n; i++)
if (is_prime(i))
largest_prime = i;

/* Ends the benchmark timer. */
scrutiny_benchmark_finish();
scrutiny_bench_return();
}

/* Use this space to free memory or I/O. */
// This benchmark will take a long time but much of it will be idle.
SCRUTINY_BENCHMARK(wait_1_sec) {
sleep(1);
scrutiny_bench_return();
}

int main()
{
scrutiny_benchmark_t benchmarks[] =
{
primes_to_n_benchmark,
primes_to_n,
wait_1_sec,
NULL
};

/* Run all given benchmarks once. */
//scrutiny_run_benchmarks(benchmarks);

/* Run all given benchmarks the given number of times. */
scrutiny_run_benchmarks_n_times(benchmarks, 16);

/* Output test results. */
scrutiny_output_benchmark_results(stdout);

/* Output a more parsable results text. */
//file_t* file = fopen("out.txt", "w");
//scrutiny_output_benchmark_results_parsable(file);
// Scrutiny will output both the actual time and CPU time used when running
// a benchmark. Unlike when unit testing scrutiny will not exit the program
// here.
scrutiny_run_benchmarks(benchmarks, 1);
}

22 changes: 8 additions & 14 deletions examples/runtime_assertion.c
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
#include "../scrutiny/scrutiny.h"
#include <stdio.h>

/*
* Shows how you can use scrutiny's assertions as an alternative to the
* standard library's during application runtime or debugging.
*/
// This time we dont define the `SCRUTINY_DEBUG` macro so that we can use
// scrutiny's assertions during the runtime of our program.

int main()
{
int main() {
int a = 8;
int b = 4;

scrutiny_assert_equal_int(a, b * 2);
scrutiny_assert_equal(a, b * 2);
printf("Passed One!\n");

/*
* If you want to make runtime assertions like this after running unit tests
* you will need to make a call to scrutiny_clear_results().
*/

int arr1[] = { 2, 4, 6, 8 };
int arr2[] = { 2, 4, 5, 7 };

scrutiny_assert_not_equal_array(arr1, arr2, sizeof(int), sizeof arr1);
scrutiny_assert_equal_int(arr1[2], arr2[2]);
for (unsigned int i = 0; i < sizeof(arr1) / sizeof(int); i++) {
scrutiny_assert_equal(arr1[i], arr2[i]);
}
}

124 changes: 29 additions & 95 deletions examples/unit_tests.c
Original file line number Diff line number Diff line change
@@ -1,107 +1,41 @@
#include "../scrutiny/scrutiny.h"

/* This macro expands to void. */
SCRUTINY_UNIT_TEST add_test(void)
{
int a = 3;
int b = 5;

/* Assert that a does not equal b. */
scrutiny_assert_false(a == b);

/* Asserts only document the failiure, they wont terminate execution. */

/*
* Assert that a + b is 8, scrutiny allows you to make more than one
* assertion in the same test, each assert is a "test case" and each given
* function is a "unit test". Scrutiny reports both in output.
*/

scrutiny_assert_equal_int(a + b, 8);
}

SCRUTINY_UNIT_TEST increment_test(void)
{
size_t limit = 128;

/* Because scrutiny allows for multiple asserts, you can run them in a loop. */
for (size_t i = 0; i < limit; i++)
scrutiny_assert_false(limit == i);
}

SCRUTINY_UNIT_TEST not_equal_test(void)
{
/*
* Scrutiny can make equality assertion on arrays as well if given the
* length to compare to.
*/

int arr1[] = { 1, 2, 3 };
int arr2[] = { 1, 4, 9 };
#include <stdlib.h>

/*
* We've seen alot of equality assertions, so how about we assert that the
* expected value is not equal to the actual value.
*/
// This macro makes scrutiny's assertions not abort the program on failure and
// instead return from the current function, which makes unit testing easier.
#define SCRUTINY_DEBUG

scrutiny_assert_not_equal_array(arr1, arr2, sizeof(int), 3);
}

/*
* For this next bit we need to create a struct and custom comparison.
*/

struct _MyStruct {
int value;
bool unequal;
};

typedef struct _MyStruct MyStruct;

Scrutiny_ComparisonResult compare_my_struct(MyStruct* left, MyStruct* right) {
if (left->unequal || right->unequal) {
return SCRUTINY_UNEQUAL;
}

if (left->value > right->value) {
return SCRUTINY_GREATOR_THAN;
}
#include "../scrutiny/scrutiny.h"

if (left->value < right->value) {
return SCRUTINY_LESS_THAN;
}
SCRUTINY_UNIT_TEST(addition_test) {
int a = 2;
int b = 3;

return SCRUTINY_EQUAL;
scrutiny_assert_greater_than(b, a);
scrutiny_assert_less_than(a, b);
scrutiny_assert_no_greater_than(a, b);
scrutiny_assert_equal(5, a + b);
}

SCRUTINY_UNIT_TEST custom_compare_test() {
MyStruct a = { .value = 3, .unequal = false };
MyStruct b = { .value = 0, .unequal = false };
// This test will fail.
SCRUTINY_UNIT_TEST(subtraction_test) {
int a = 2;
int b = 3;

scrutiny_assert_greator_than_struct(&a, &b, (Scrutiny_CompareFunction)compare_my_struct);

a.unequal = true;

scrutiny_assert_not_equal_struct(&a, &b, (Scrutiny_CompareFunction)compare_my_struct);
scrutiny_assert_equal(0, a - b);
}

int main()
{
Scrutiny_UnitTest scrutiny_unit_tests[] =
{
add_test, /* Pointer to our unit test. */
increment_test, /* Another test. */
not_equal_test,
NULL /* End the list. */
int main() {
scrutiny_test_t tests[] = {
subtraction_test,
addition_test,
NULL
};

/* Runs all tests until terminating NULL pointer. */
scrutiny_run_tests(scrutiny_unit_tests);

/* Outputs test results to standard out. */
scrutiny_output_test_results(stdout);

/* Outputs easily parsable test results to a file. */
scrutiny_output_test_results_parsable(fopen("out.txt", "w"));
}
scrutiny_run_tests(tests);

// Using this function will print some extra info at the end of the test run
// for when you need that 100% passed dopamine. You cant use both in the
// same run since they exit the program for you with a value inicative of
// test success.
// scrutiny_run_tests_with_stats(tests);
}
Loading

0 comments on commit 297fa68

Please sign in to comment.