Skip to content

Commit

Permalink
Added log
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob-C-Smith committed Nov 7, 2023
1 parent 3c69c52 commit 57187cb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 52 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "extern/sync"]
path = extern/sync
url = https://github.com/Jacob-C-Smith/sync
[submodule "extern/log"]
path = extern/log
url = https://github.com/Jacob-C-Smith/log
18 changes: 16 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ if ( NOT "${HAS_SYNC}")
set(HAS_SYNC true PARENT_SCOPE)
endif()

# Find the log module
if ( NOT "${HAS_LOG}")

# Log
message("[queue] Adding log project")

# Add the log directory
add_subdirectory(${CMAKE_SOURCE_DIR}/extern/log ${CMAKE_SOURCE_DIR}/build/log)

# Let any parent project know that this project has log
set(HAS_LOG true PARENT_SCOPE)
endif()


# Add the queue project
if ( NOT "${HAS_QUEUE}")

Expand All @@ -60,9 +74,9 @@ target_link_libraries(queue_example sync)

# Add source to the tester program.
add_executable (queue_test "queue.c" "queue_test.c")
add_dependencies(queue_test sync)
add_dependencies(queue_test sync log)
target_include_directories(queue_test PUBLIC include include/queue ${CMAKE_SOURCE_DIR}/extern/sync/include/)
target_link_libraries(queue_test sync)
target_link_libraries(queue_test sync log)

# Add source to this project's library
add_library (queue SHARED "queue.c")
Expand Down
1 change: 1 addition & 0 deletions extern/log
Submodule log added at acf7ce
29 changes: 2 additions & 27 deletions queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ int queue_dequeue ( queue *const p_queue, const void **const pp_value )
*pp_value = ret_m->content;

// Free the memory
if ( QUEUE_REALLOC(ret_m, 0) ) goto failed_to_free;
(void)QUEUE_REALLOC(ret_m, 0);

// Unlock
mutex_unlock(p_queue->_lock);
Expand All @@ -393,20 +393,6 @@ int queue_dequeue ( queue *const p_queue, const void **const pp_value )
return 0;
}

// Standard library errors
{
failed_to_free:
#ifndef NDEBUG
printf("[Standard Library] Call to \"realloc\" returned an erroneous value in call to function \"%s\"\n", __FUNCTION__);
#endif

// Unlock
mutex_unlock(p_queue->_lock);

// Error
return 0;
}

// Queue errors
{
queue_empty:
Expand Down Expand Up @@ -481,7 +467,7 @@ int queue_destroy ( queue **const pp_queue )
}

// Free the memory
if ( QUEUE_REALLOC(p_queue, 0) ) goto failed_to_free;
(void)QUEUE_REALLOC(p_queue, 0);

// Success
return 1;
Expand All @@ -499,16 +485,5 @@ int queue_destroy ( queue **const pp_queue )
// Error
return 0;
}

// Standard library errors
{
failed_to_free:
#ifndef NDEBUG
printf("[Standard Library] Call to \"realloc\" returned an erroneous value in call to function \"%s\"\n", __FUNCTION__);
#endif

// Error
return 0;
}
}
}
50 changes: 27 additions & 23 deletions queue_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <stdlib.h>
#include <stdbool.h>

#include <log/log.h>

#include <queue/queue.h>

// Possible elements
Expand Down Expand Up @@ -92,10 +94,14 @@ int main ( int argc, const char* argv[] )

// Initialize the timer library
timer_init();
log_init(0, true);

// Formatting
printf("|==============|\n| QUEUE TESTER |\n|==============|\n\n");

printf(
"╭──────────────╮\n"\
"│ queue tester │\n"\
"╰──────────────╯\n\n"
);
// Start
t0 = timer_high_precision();

Expand All @@ -106,9 +112,9 @@ int main ( int argc, const char* argv[] )
t1 = timer_high_precision();

// Report the time it took to run the tests
printf("queue took ");
log_info("queue took ");
print_time_pretty ( (double)(t1-t0)/(double)timer_seconds_divisor() );
printf(" to test\n");
log_info(" to test\n");

// Exit
return ( total_passes == total_tests ) ? EXIT_SUCCESS : EXIT_FAILURE;
Expand Down Expand Up @@ -146,27 +152,27 @@ int print_time_pretty ( double seconds )

// Print days
if ( days )
printf("%d D, ", days);
log_info("%d D, ", days);

// Print hours
if ( hours )
printf("%d h, ", hours);
log_info("%d h, ", hours);

// Print minutes
if ( minutes )
printf("%d m, ", minutes);
log_info("%d m, ", minutes);

// Print seconds
if ( __seconds )
printf("%d s, ", __seconds);
log_info("%d s, ", __seconds);

// Print milliseconds
if ( milliseconds )
printf("%d ms, ", milliseconds);
log_info("%d ms, ", milliseconds);

// Print microseconds
if ( microseconds )
printf("%d us", microseconds);
log_info("%d us", microseconds);

// Success
return 1;
Expand Down Expand Up @@ -396,7 +402,7 @@ int test_empty_queue(int(*queue_constructor)(queue **pp_queue), char *name)
// Call the queue constructor
queue_constructor(&p_queue);

printf("Scenario: %s\n", name);
log_info("Scenario: %s\n", name);

print_test(name, "queue_front" , test_front(queue_constructor, (void *)0, zero) );
print_test(name, "queue_rear" , test_rear(queue_constructor, (void *)0, zero) );
Expand All @@ -416,7 +422,7 @@ int test_one_element_queue ( int (*queue_constructor)(queue **), char *name, v
queue *p_queue = 0;


printf("Scenario: %s\n", name);
log_info("Scenario: %s\n", name);

print_test(name, "queue_front" , test_front(queue_constructor, elements[0], match) );
print_test(name, "queue_rear" , test_rear(queue_constructor, elements[0], match) );
Expand All @@ -434,13 +440,7 @@ int test_one_element_queue ( int (*queue_constructor)(queue **), char *name, v
int test_two_element_queue ( int (*queue_constructor)(queue **), char *name, void **elements )
{

// Initialized_data
queue *p_queue = 0;

// Call the queue constructor
queue_constructor(&p_queue);

printf("Scenario: %s\n", name);
log_info("Scenario: %s\n", name);

print_test(name, "queue_front" , test_front(queue_constructor, elements[1], match) );
print_test(name, "queue_rear" , test_rear(queue_constructor, elements[0], match) );
Expand All @@ -467,7 +467,7 @@ int test_three_element_queue ( int (*queue_constructor)(queue **), char *name,
// Call the queue constructor
queue_constructor(&p_queue);

printf("Scenario: %s\n", name);
log_info("Scenario: %s\n", name);

print_test(name, "queue_front" , test_front(queue_constructor, elements[2], match) );
print_test(name, "queue_rear" , test_rear(queue_constructor, elements[0], match) );
Expand Down Expand Up @@ -496,7 +496,11 @@ int print_test ( const char *scenario_name, const char *test_name, bool passed )
{

// Initialized data
printf("%s_test_%-17s %s\n",scenario_name, test_name, (passed) ? "PASS" : "FAIL");
if ( passed )
log_pass("[%s] %s %s\n", "PASS", scenario_name, test_name);
else
log_fail("[%s] %s %s\n", "FAIL", scenario_name, test_name);


// Increment the counters
{
Expand Down Expand Up @@ -525,8 +529,8 @@ int print_final_summary ()
total_fails += ephemeral_fails;

// Print
printf("\nTests: %d, Passed: %d, Failed: %d (%%%.3f)\n", ephemeral_tests, ephemeral_passes, ephemeral_fails, ((float)ephemeral_passes/(float)ephemeral_tests*100.f));
printf("Total: %d, Passed: %d, Failed: %d (%%%.3f)\n\n", total_tests, total_passes, total_fails, ((float)total_passes/(float)total_tests*100.f));
log_info("\nTests: %d, Passed: %d, Failed: %d (%%%.3f)\n", ephemeral_tests, ephemeral_passes, ephemeral_fails, ((float)ephemeral_passes/(float)ephemeral_tests*100.f));
log_info("Total: %d, Passed: %d, Failed: %d (%%%.3f)\n\n", total_tests, total_passes, total_fails, ((float)total_passes/(float)total_tests*100.f));

ephemeral_tests = 0;
ephemeral_passes = 0;
Expand Down

0 comments on commit 57187cb

Please sign in to comment.