diff --git a/arena.c b/arena.c index 12d7d8f..c3eff2a 100644 --- a/arena.c +++ b/arena.c @@ -1,3 +1,14 @@ +// Written by retoor@molodetz.nl + +// The program tests the "arena" memory allocation functionality +// by constructing, initializing, and performing operations such as +// allocation and validation of memory pointers within a dynamic memory arena. + +// Includes non-standard libraries "arena.h" for arena memory management +// and "rtest.h" for testing assertions and outcomes. + +// MIT License + #include "arena.h" #include "rtest.h" #include <stdio.h> @@ -5,31 +16,38 @@ int main() { rtest_banner("testing arena"); + arena_t *arena = arena_construct(); - // Test initial data + rtest_banner("Initial values"); rtest_assert(arena->memory == NULL); rtest_assert(arena->size == 0); rtest_assert(arena->pointer == 0); + arena_free(arena); - // New instance test + rtest_banner("New instance defaults"); arena = arena_new(1024); rtest_assert(arena->memory != NULL); rtest_assert(arena->size == 1024); rtest_assert(arena->pointer == 0); + arena_free(arena); - // Allocate test + rtest_banner("Allocate"); arena = arena_new(1024); + int *int_one = (int *)arena_alloc(arena, sizeof(int)); *int_one = 10; rtest_assert(*int_one == 10); rtest_assert(arena->pointer == sizeof(int)); + int *int_two = (int *)arena_alloc(arena, sizeof(int)); *int_two = 20; rtest_assert(*int_two == 20); rtest_assert(arena->pointer == sizeof(int) * 2); + arena_free(arena); + return rtest_end(""); } \ No newline at end of file diff --git a/nsock.c b/nsock.c index 1fa4f0b..09a22f9 100644 --- a/nsock.c +++ b/nsock.c @@ -1,11 +1,21 @@ +// Written by retoor@molodetz.nl + +// This program sets up a network socket server using `nsock` and hooks up functions to handle events like connection, data reception, and +// closing. + +// External dependency used: nsock + +// MIT License + #include "nsock.h" void on_connect(int fd) { printf("connect\n"); } + void on_data(int fd) { printf("data\n"); } + void on_close(int fd) { printf("close\n"); } int main() { - nsock(9999, on_connect, on_data, on_close); return 0; } \ No newline at end of file diff --git a/nsock.h b/nsock.h index 332d218..55cc84f 100644 --- a/nsock.h +++ b/nsock.h @@ -1,5 +1,6 @@ #ifndef NSOCK_H #define NSOCK_H +#include <unistd.h> #include "rmalloc.h" #include <arpa/inet.h> #include <errno.h> diff --git a/r.c b/r.c index 95cb412..389086e 100644 --- a/r.c +++ b/r.c @@ -1,3 +1,13 @@ +// Written by retoor@molodetz.nl + +// This program defines a dummy function that multiplies each number in a loop by two. It also sets up a benchmarking tool, executes it a +// number of times, and cleans up the used resources. + +// Includes the "rlib.h" library to utilize benchmarking functions. + +// MIT License: This software is provided "as-is", without any express or implied warranty. In no event will the authors be held liable for +// any damages arising from the use of this software. + #include "rlib.h" void dummy_function() { @@ -10,11 +20,8 @@ int main() { rbench_t *r = rbench_new(); r->add_function(r, "function", "dummy_function", dummy_function); r->execute(r, 10000); - rbench_free(r); for (int i = 0; i < 10000; i++) { - - // rprintr("\\l\\T message\n"); } } \ No newline at end of file diff --git a/rautocomplete.c b/rautocomplete.c index 6ae4c61..35247de 100644 --- a/rautocomplete.c +++ b/rautocomplete.c @@ -1,7 +1,34 @@ +// Written by retoor@molodetz.nl + +// This code snippet demonstrates a simple usage of an autocomplete structure. It creates a new autocomplete object, adds several strings to +// it, attempts to find a string using the autocomplete functionality, and then cleans up by freeing the allocated memory. + +// Summary of used imports: "rautocomplete.h" - assumed to provide the necessary functions and data structures for the autocomplete +// functionality. + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + #include "rautocomplete.h" int main() { - rautocomplete_t *ac = rautocomplete_new(); rstring_list_add(ac, "first"); rstring_list_add(ac, "test2"); @@ -23,10 +50,14 @@ int main() { rstring_list_add(ac, "test18"); rstring_list_add(ac, "test19"); rstring_list_add(ac, "test20"); - printf(r4_escape("test")); + + printf("%s", r4_escape("test")); + char *str = rautocomplete_find(ac, "firsta"); - if (str) + if (str) { printf("%s\n", str); + } + rautocomplete_free(ac); return 0; } \ No newline at end of file diff --git a/rbench.c b/rbench.c index ced4a0a..3ea8c32 100644 --- a/rbench.c +++ b/rbench.c @@ -1,3 +1,33 @@ +// Written by retoor@molodetz.nl + +// This source code provides benchmarking functions for different string manipulation, comparison, and mathematical operations. It evaluates +// the performance of various implementations of operations such as formatting numbers, starting and ending string matching, string moving, +// and mathematical operations like addition and subtraction. The results are printed, showing total execution time and number of operations +// performed. + +// This program uses custom libraries like rbench for benchmarking, rtest for assertions, rtree for handling tree structures, rhashtable for +// hash table operations, and rtime for time-related operations. External C standard library functions are also used. + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include "rbench.h" #include "rtest.h" #include "rtree.h" @@ -9,15 +39,13 @@ char *format_number_retoor(long lnumber) { static char formatted[1024]; char number[1024]; - number[0] = 0; sprintf(number, "%ld", lnumber); size_t len = strlen(number); int comma_count = len / 3; int count = 0; int offset = 0; - int i; formatted[comma_count + len] = 0; - for (i = len + comma_count; i > 0; i--) { + for (int i = len + comma_count; i > 0; i--) { formatted[i - offset] = number[i - comma_count]; if (count == 3) { count = 0; @@ -30,6 +58,7 @@ char *format_number_retoor(long lnumber) { } return formatted; } + char *format_number_yurii(long long num) { static char buf[1024]; char *buff = buf; @@ -58,26 +87,21 @@ char *format_number_yurii(long long num) { char *format_number_gpt(long lnumber) { static char formatted[1024]; - char number[1024]; sprintf(number, "%ld", lnumber); - int len = strlen(number); - int commas_needed = (len - 1) / 3; // Determine how many dots are needed - int new_len = len + commas_needed; // New length with dots included - - formatted[new_len] = '\0'; // Null-terminate the formatted string - - int i = len - 1; // Index for original number - int j = new_len - 1; // Index for formatted number - int count = 0; // Counter for placing dots - + int commas_needed = (len - 1) / 3; + int new_len = len + commas_needed; + formatted[new_len] = '\0'; + int i = len - 1; + int j = new_len - 1; + int count = 0; while (i >= 0) { if (count == 3) { - formatted[j--] = '.'; // Insert dot after every 3 digits - count = 0; // Reset the counter + formatted[j--] = '.'; + count = 0; } - formatted[j--] = number[i--]; // Copy digit from the original number + formatted[j--] = number[i--]; count++; } return formatted; @@ -90,28 +114,25 @@ int rstrcmp(char *l, char *r) { } return *l - *r; } + int strcmp_gpt(const char *str1, const char *str2) { while (*str1 && (*str1 == *str2)) { str1++; str2++; } - return *(unsigned char *)str1 - *(unsigned char *)str2; } -int strcmp_clib(p1, p2) const char *p1; -const char *p2; -{ + +int strcmp_clib(const char *p1, const char *p2) { register const unsigned char *s1 = (const unsigned char *)p1; register const unsigned char *s2 = (const unsigned char *)p2; unsigned c1, c2; - do { c1 = (unsigned char)*s1++; c2 = (unsigned char)*s2++; if (c1 == '\0') return c1 - c2; } while (c1 == c2); - return c1 - c2; } @@ -124,12 +145,12 @@ bool bench_ends_with_r(const char *s1, const char *s2) { return rstrendswith(s1, bool bench_starts_with_gpt(const char *str, const char *prefix) { while (*prefix) { if (*str != *prefix) { - return false; // Mismatch found + return false; } str++; prefix++; } - return true; // All characters matched + return true; } int bench_starts_with_yurii(const char *str, const char *start) { @@ -137,41 +158,32 @@ int bench_starts_with_yurii(const char *str, const char *start) { return start == NULL; if (str == start || start == NULL || *start == '\0') return 1; - return strncmp(str, start, strlen(start)) == 0; } bool bench_ends_with_gpt(const char *str, const char *suffix) { size_t str_len = strlen(str); size_t suffix_len = strlen(suffix); - - // If the suffix is longer than the string, it can't be a suffix if (suffix_len > str_len) { return false; } - - // Start comparing from the end of both strings const char *str_end = str + str_len - suffix_len; while (*suffix) { if (*str_end != *suffix) { - return false; // Mismatch found + return false; } str_end++; suffix++; } - - return true; // All characters matched + return true; } int bench_ends_with_yurii(const char *str, const char *end) { - size_t end_len; - if (str == NULL) return end == NULL; if (str == end || end == NULL || *end == '\0') return 1; - - end_len = strlen(end); + size_t end_len = strlen(end); return strncmp(str + (strlen(str) - end_len), end, end_len) == 0; } @@ -196,9 +208,6 @@ void bench_rstrmove_gpt() { rasserts(!strcmp(to_move_1, "?defgabcaa")); char to_move_2[] = "?defgabcaa"; rstrmove(to_move_2, 0, 5, 2); - // printf("BECAME: %s\n",to_move_2); - // Goes wrong! - // rasserts(!strcmp(to_move_2, "ab?defgcaa")); char to_move_3[] = "?defgabcaa"; rstrmove(to_move_3, 0, 5, 7); rasserts(!strcmp(to_move_3, "abc?defgaa")); @@ -230,6 +239,7 @@ void rbench_table_rhashtable() { nsecs_t total_execution_time = 0; long total_times = 0; bool show_progress = 1; + void bench_format_number(long times, long number) { rbench_t *r; rprint("\\T B\\l Times: %ld\n", times); @@ -269,6 +279,7 @@ void bench_rstrmove(long times) { total_times += times * 2; rbench_free(r); } + void bench_math(long times) { rbench_t *r = rbench_new(); r->show_progress = show_progress; @@ -280,6 +291,7 @@ void bench_math(long times) { total_times += times * 2; rbench_free(r); } + void bench_strcmp(long times) { rbench_t *r = rbench_new(); r->stdout = false; @@ -302,6 +314,7 @@ void printf_strcat() { } printf("%s", buffer); } + void printf_raw() { for (int i = 0; i < 1000; i++) { printf("%s", "a"); @@ -363,26 +376,21 @@ void bench_endswith(long times) { int main() { show_progress = true; long times = 900000000; - printf("With %% progress times:\n"); BENCH(times, { bench_starts_with_yurii("abcdefghijklmnopqrstuvw", "abcdef"); }); BENCH(times, { bench_ends_with_yurii("abcdefghijklmnopqrstuvw", "uvw"); }); - printf("Without %% progress times:\n"); BENCH(times * 1000, { bench_starts_with_yurii("abcdefghijklmnopqrstuvw", "abcdef"); }); BENCH(times * 1000, { bench_ends_with_yurii("abcdefghijklmnopqrstuvw", "uvw"); }); - bench_table(times / 10000); bench_sprintf(times / 10000); bench_format_number(times / 100, 123456789); bench_rstrmove(times / 100); bench_math(times); bench_strcmp(times / 100); - bench_startswith(times / 10); bench_endswith(times / 10); printf("\nTotal execution time:%s\n", format_time(total_execution_time)); printf("Total times: %s\n", rformat_number(total_times)); - return 0; } \ No newline at end of file diff --git a/rbuffer.c b/rbuffer.c index f8df937..d5d4b59 100644 --- a/rbuffer.c +++ b/rbuffer.c @@ -1,5 +1,29 @@ -#include "rtest.h" -#include "rbuffer.h" +// Written by retoor@molodetz.nl + +// This code is a test program for the buffer manipulation functionalities provided by the "rbuffer" library. + +#include "rtest.h" // Provides test utilities and assertions for testing purposes +#include "rbuffer.h" // Provides buffer handling functions like creation, consumption and conversion + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. int main() { rtest_banner("rbuffer"); diff --git a/rcase.c b/rcase.c index ac872be..6429ecd 100644 --- a/rcase.c +++ b/rcase.c @@ -1,3 +1,12 @@ +// Written by retoor@molodetz.nl + +// This source code defines the main function for a program, passing the command-line arguments to another function called rcase_main and +// returning its result. + +// #include "rcase.h": This include statement brings in the rcase.h header file which is not part of the standard library. + +// MIT License + #include "rcase.h" int main(int argc, char *argv[]) { return rcase_main(argc, argv); } \ No newline at end of file diff --git a/rcat.c b/rcat.c index 3d4c90b..fa6d039 100644 --- a/rcat.c +++ b/rcat.c @@ -1,3 +1,29 @@ +// Written by retoor@molodetz.nl + +// This program serves as an entry point for the application, calling `rcat_main` function with command line arguments. + +// Included "rcat.h" to use the `rcat_main` function. + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include "rcat.h" -int main(int argc, char *argv[]) { return rcat_main(argc, argv); } +int main(int argc, char *argv[]) { return rcat_main(argc, argv); } \ No newline at end of file diff --git a/rcov.c b/rcov.c index b2db4ed..de70ac1 100644 --- a/rcov.c +++ b/rcov.c @@ -1,3 +1,12 @@ +// Written by retoor@molodetz.nl + +// Main entry point for the program which calls the rcov_main function with +// command line arguments. + +// Includes the header file "rcov.h" for accessing the prototype of rcov_main. + +// MIT License + #include "rcov.h" -int main(int argc, char *argv[]) { return rcov_main(argc, argv); } +int main(int argc, char *argv[]) { return rcov_main(argc, argv); } \ No newline at end of file diff --git a/reditor.c b/reditor.c index 1e17271..9e00c30 100644 --- a/reditor.c +++ b/reditor.c @@ -1,3 +1,24 @@ +// Written by retoor@molodetz.nl + +// This code implements terminal manipulation functions including getting terminal size, setting raw mode, handling cursor position, and +// capturing keyboard input. It allows navigation using arrow keys and displays typed characters on the screen, exiting on 'q' key press. + +// External libraries used: <sys/ioctl.h> for interacting with terminal interfaces. + +// The MIT License (MIT) + +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to +// the following conditions: + +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR +// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + #include <stdio.h> #include <stdlib.h> #include <sys/ioctl.h> @@ -7,41 +28,25 @@ void rget_terminal_size(int *x, int *y) { struct winsize w; - - // Get terminal size ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - - // Print terminal size - // printf("Rows: %d, Columns: %d\n", w.ws_row, w.ws_col); - - // You can use this width in your program logic int terminal_width = w.ws_col; *x = w.ws_col; *y = w.ws_row; - - // printf("Setting content width to half the terminal width:\n"); - - // Example content that fits within half the terminal width - // printf("%.*s\n", terminal_width / 2, "This text is formatted to half - // the terminal width."); } struct termios rorig_termios; -// Restore original terminal settings void reset_terminal_mode() { tcsetattr(STDIN_FILENO, TCSANOW, &rorig_termios); } void set_raw_mode() { struct termios new_termios; - tcgetattr(STDIN_FILENO, &rorig_termios); // Get current terminal settings - atexit(reset_terminal_mode); // Ensure original settings are restored on exit - + tcgetattr(STDIN_FILENO, &rorig_termios); + atexit(reset_terminal_mode); new_termios = rorig_termios; - new_termios.c_lflag &= ~(ICANON | ECHO); // Disable canonical mode and echoing - new_termios.c_cc[VMIN] = 1; // Minimum number of characters for noncanonical read - new_termios.c_cc[VTIME] = 0; // Timeout in deciseconds for noncanonical read - - tcsetattr(STDIN_FILENO, TCSANOW, &new_termios); // Apply new settings + new_termios.c_lflag &= ~(ICANON | ECHO); + new_termios.c_cc[VMIN] = 1; + new_termios.c_cc[VTIME] = 0; + tcsetattr(STDIN_FILENO, TCSANOW, &new_termios); } unsigned int read_key() { @@ -52,23 +57,14 @@ unsigned int read_key() { return c; } -void rrclear() { - printf("\033[2J"); // Clear screen -} +void rrclear() { printf("\033[2J"); } -void rset_cursor_position(int x, int y) { - // rrclear(); - printf("\033[%d;%dH", y, x); -} +void rset_cursor_position(int x, int y) { printf("\033[%d;%dH", y, x); } void get_cursor_position(int *cols, int *rows) { char buf[32]; unsigned int i = 0; - - // Request cursor position printf("\033[6n"); - - // Read the response: ESC [ rows ; cols R while (i < sizeof(buf) - 1) { if (read(STDIN_FILENO, buf + i, 1) != 1) break; @@ -77,8 +73,6 @@ void get_cursor_position(int *cols, int *rows) { i++; } buf[i] = '\0'; - - // Parse the response if (buf[0] == '\033' && buf[1] == '[') { sscanf(buf + 2, "%d;%d", rows, cols); } @@ -87,55 +81,41 @@ void get_cursor_position(int *cols, int *rows) { void run() { int c; int x = 3; - int y = 3; // Initial y position + int y = 3; int file_index = 0; set_raw_mode(); - printf("\033[2J"); // Clear screen + printf("\033[2J"); rset_cursor_position(x, y); char screen_data[1024]; int width, height; rget_terminal_size(&width, &height); - screen_data[0] = 0; - for (int i = 0; i < width * height; i++) { // screen_data[i] = '\0'; - // screen_data[i] = 0; - } memset(&screen_data, 0, 2048); - // printf(screen_data); while (1) { c = read_key(); - if (c == '\033') { // If the first character is ESC - - if (read_key() == '[') { // If the second character is '[' + if (c == '\033') { + if (read_key() == '[') { rrclear(); c = read_key(); - if (c == 'A') { - if (y) { - y--; - } + if (c == 'A' && y) { + y--; rset_cursor_position(x, y); - } else if (c == 'B') { - if (y) { - y++; - } + } else if (c == 'B' && y) { + y++; rset_cursor_position(x, y); } else if (c == 'C') { - x++; - rset_cursor_position(x, y); - } else if (c == 'D') { x--; - rset_cursor_position(x, y); } printf(screen_data); } } else if (c == 'q') { - break; // Press 'q' to quit + break; } else { for (int i = 0; i < file_index; i++) { if (screen_data[i] == '\0') { @@ -143,20 +123,15 @@ void run() { } } screen_data[file_index] = c; - // file_index++; get_cursor_position(&x, &y); file_index = x * y; x++; - // putc(c, stdout); - // rrclear(); rset_cursor_position(1, 1); - // ss x++; printf(screen_data); rset_cursor_position(x, y); - fflush(stdout); } } } -int main() { run(); } +int main() { run(); } \ No newline at end of file diff --git a/remo.c b/remo.c index 8ff3ae3..9143b00 100644 --- a/remo.c +++ b/remo.c @@ -1,3 +1,11 @@ +// Written by retoor@molodetz.nl + +// This source code demonstrates a simple main function that utilizes functions from the "remo" library to print and retrieve a string. + +// Includes: "remo.h" (not part of the standard library) + +// MIT License + #include "remo.h" int main() { diff --git a/rfalloc.c b/rfalloc.c index 40f1f5b..328f502 100644 --- a/rfalloc.c +++ b/rfalloc.c @@ -1,3 +1,31 @@ +// Written by retoor@molodetz.nl + +// This code provides a mechanism to initialize and manage file-backed memory using memory mapping and file operations for storing large +// data sets temporarily in a file system. + +// The code uses `fcntl.h`, `stdbool.h`, `stdio.h`, `stdlib.h`, `string.h`, `sys/mman.h`, and `unistd.h` for file handling, memory +// management, and basic input/output operations. + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include <fcntl.h> #include <stdbool.h> #include <stdio.h> @@ -6,7 +34,6 @@ #include <sys/mman.h> #include <unistd.h> -// r file memory typedef struct rfm_t { char path[4096]; void *data; @@ -17,97 +44,47 @@ typedef struct rfm_t { } rfm_t; rfm_t rfm; -bool _initialized = false; +bool initialized = false; void rfm_destroy(rfm_t *r) { if (munmap(r->data, r->size) == -1) { perror("munmap"); exit(EXIT_FAILURE); } - close(rfm.fd); + close(r->fd); } void *falloc(size_t s) { rfm_t *rr = &rfm; - - printf("hier\n"); char *data = (char *)rr->data + rfm.ptr; - // data+= rfm.ptr; rfm.ptr += s; return data; } void *finit(char *path, size_t size) { - - printf("HIERR\n"); - if (!_initialized) { - rfm.ptr = 0; - _initialized = true; - rfm.size = size; - - printf("HIERR\n"); + if (!initialized) { + initialized = true; memset(&rfm, 0, sizeof(rfm_t)); rfm.size = size; - rfm.ptr = 0; - - printf("HIERR\n"); - // rfm.fd = open(path, O_RDWR); - // ftruncate(rfm.fd, size); if (path) { - printf("HIERR\n"); - rfm.path[0] = 0; strcpy(rfm.path, path); - // creat(path,F_) rfm.fd = open(path, O_RDWR); - printf("OPEN %s\n", path); - } - if (!path) { + } else { rfm.f = tmpfile(); rfm.fd = fileno(rfm.f); } - // if (ftruncate(rfm.fd, size) == -1) - // { - // perror("ftruncate"); - // exit(EXIT_FAILURE); - // } - // rfensurefile(path,1024*1024); + rfm.data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, rfm.fd, 0); - printf("HIERR\n"); } - /* - if (rfm.data == MAP_FAILED) - { - perror("mmap"); - exit(EXIT_FAILURE); - } - char *data = (char *)rfm.data; - printf(data); - if (munmap(rfm.data, size) == -1) - { - perror("munmap"); - exit(EXIT_FAILURE); - } - fclose(rfm.f); - */ } + int main() { - // Step 1: Create a temporary file finit("tast.dat", 1024 * 1024 * 100); - printf("gaa\n"); char *data = (char *)falloc(10); strcpy(data, "ab"); char *data2 = (char *)falloc(30); strcpy(data2, "ff\n"); - - // for(int i = 0; i < 333333; i++) - // strcat(data,"ggggggggggggggggg"); - printf("%s\n", data); - - // printf("%s\n",data);; - - // strcpy(data2,"aaaa"); - return 0; } \ No newline at end of file diff --git a/rhashtable.c b/rhashtable.c index 55dda76..b5e4ec9 100644 --- a/rhashtable.c +++ b/rhashtable.c @@ -1,12 +1,19 @@ +// Written by retoor@molodetz.nl + +// This code demonstrates the creation and testing of a hashtable with random keys and ensures the correct retrieval of values. + +// Importing rhashtable.h, rtest.h, and rstring.h + +// The content is licensed under MIT License. + #include "rhashtable.h" #include "rtest.h" #include "rstring.h" int main() { - for (int i = 0; i < 1000; i++) { char *key = rgenerate_key(); - rset(key, "tast"); - rasserts(!strcmp(rget(key), "tast")); + rset(key, "test"); + rasserts(strcmp(rget(key), "test") == 0); } } \ No newline at end of file diff --git a/rhttp.c b/rhttp.c index 158a5bd..67769d8 100644 --- a/rhttp.c +++ b/rhttp.c @@ -1,3 +1,13 @@ +// Written by retoor@molodetz.nl + +// This C program initializes a multi-threaded HTTP server. It can selectively operate in test mode or serve mode, handling HTTP requests +// and sending responses. The server supports optional quiet mode configuration. + +// Includes external libraries: +// rmalloc.h, rhttp.h, rtest.h, and pthread.h for memory management, HTTP serving, testing utilities, and creating threads, respectively. + +// MIT License + #include "rmalloc.h" #include "rhttp.h" #include "rtest.h" @@ -61,12 +71,10 @@ int main(int argc, char *argv[]) { if (do_test) { rassert(!strcmp(response, "Ok!")); pthread_cancel(st); - // cleanup } else { pthread_join(st, NULL); } - // rhttp_main(argc, argv); if (do_test) return rtest_end(""); return 0; -} +} \ No newline at end of file diff --git a/rhttpc.c b/rhttpc.c index 2590054..009c908 100644 --- a/rhttpc.c +++ b/rhttpc.c @@ -1,11 +1,20 @@ +// Written by retoor@molodetz.nl + +// This program repeatedly sends an HTTP GET request to a server running on localhost at port 8888 and prints the server's response. + +// The program uses a custom library "rhttp.h" for HTTP requests, which is not a standard C library. + +// MIT License + #include "rhttp.h" #include <stdio.h> int main() { while (1) { char *response = rhttp_client_get("127.0.0.1", 8888, "/"); - if (response) + if (response) { printf("%s\n", response); + } } return 0; -} +} \ No newline at end of file diff --git a/ricli.c b/ricli.c index 60b12d4..62de45a 100644 --- a/ricli.c +++ b/ricli.c @@ -1,3 +1,12 @@ +// Written by retoor@molodetz.nl + +// This code implements a terminal-based command line interface with features like autocompletion, line management, and input handling for +// user commands. + +// The source code relies on external libraries, "rterm.h", "rstring.h", "rrex4.h", and "rautocomplete.h". + +// MIT License + #include "rterm.h" #include <stdlib.h> #include <stdbool.h> @@ -14,7 +23,7 @@ typedef struct ricli_line_t { } ricli_line_t; ricli_line_t *ricli_line_new() { - ricli_line_t *line = (ricli_line_t *)malloc(sizeof(ricli_line_t)); + ricli_line_t *line = malloc(sizeof(ricli_line_t)); line->index = 0; memset(line->type, 0, sizeof(line->type)); line->length = 0; @@ -23,8 +32,7 @@ ricli_line_t *ricli_line_new() { } char *rscli_line_to_json(ricli_line_t *line) { - - char *json = (char *)malloc(sizeof(line->type) + strlen(line->content) * 2 + 10); + char *json = malloc(sizeof(line->type) + strlen(line->content) * 2 + 10); json[0] = 0; strcpy(json, "{\"type\":\""); strcat(json, line->type); @@ -36,6 +44,7 @@ char *rscli_line_to_json(ricli_line_t *line) { strcat(json, "\"}"); return json; } + typedef struct ricli_t { ricli_line_t **lines; int line_count; @@ -58,6 +67,7 @@ void ricli_keypress(rterm_t *rt); void ricli_before_draw(rterm_t *rt); void ricli_save(ricli_t *cli, char *path); void ricli_autocomplete_execute(ricli_t *cli); + void ricli_add_autocomplete(ricli_t *cli, char *str) { if (rautocomplete_contains(cli->autocomplete, str)) return; @@ -77,7 +87,7 @@ void ricli_after_draw(rterm_t *rt) { } ricli_t *ricli_terminal_new() { - ricli_t *terminal = (ricli_t *)malloc(sizeof(ricli_t)); + ricli_t *terminal = malloc(sizeof(ricli_t)); terminal->lines = NULL; terminal->line_count = 0; terminal->line_numbers = false; @@ -90,7 +100,7 @@ ricli_t *ricli_terminal_new() { terminal->auto_save = true; terminal->x = 0; memset(terminal->input, 0, sizeof(terminal->input)); - terminal->term = (rterm_t *)malloc(sizeof(rterm_t)); + terminal->term = malloc(sizeof(rterm_t)); rterm_init(terminal->term); terminal->line_numbers = true; @@ -101,7 +111,9 @@ ricli_t *ricli_terminal_new() { terminal->term->session = (void *)terminal; return terminal; } + void ricli_set_input(ricli_t *cli, const char *content); + void ricli_autocomplete_execute(ricli_t *r) { char *result = rautocomplete_find(r->autocomplete, r->input); unsigned int original_x = r->term->cursor.x; @@ -113,11 +125,11 @@ void ricli_autocomplete_execute(ricli_t *r) { cursor_set(r->term, original_x, original_y); } } -void ricli_add_line(ricli_t *r, char *type, char *content) { +void ricli_add_line(ricli_t *r, char *type, char *content) { ricli_line_t *line = ricli_line_new(); strcpy(line->type, type ? type : ""); - line->content = (char *)malloc(strlen(content ? content : "") + 1); + line->content = malloc(strlen(content ? content : "") + 1); strcpy(line->content, content ? content : ""); line->length = strlen(line->content); if (line->length && line->content[line->length - 1] == '\n') { @@ -126,7 +138,6 @@ void ricli_add_line(ricli_t *r, char *type, char *content) { } if (line->length) ricli_add_autocomplete(r, line->content); - strcpy(line->type, type ? type : ""); line->index = r->line_count; r->lines = realloc(r->lines, sizeof(ricli_line_t *) * (r->line_count + 1)); r->lines[r->line_count] = line; @@ -167,6 +178,7 @@ void ricli_clear_input(ricli_t *cli) { line[sizeof(line) - 1] = 0; cursor_set(cli->term, 0, cli->term->cursor.y); } + void ricli_set_input(ricli_t *cli, const char *content) { if (cli->input != content) { memset(cli->input, 0, sizeof(cli->input)); @@ -177,22 +189,18 @@ void ricli_set_input(ricli_t *cli, const char *content) { rterm_print_status_bar(cli->term, 'c', cli->input); cursor_set(cli->term, cli->x, cli->term->size.ws_row); } + void ricli_put_input(ricli_t *cli, char c) { bool was_zero = cli->input[cli->x] == 0; if (was_zero) { - cli->input[cli->x] = c; cli->input[cli->x + 1] = 0; } else { char line_first[strlen(cli->input) + 5]; - memset(line_first, 0, sizeof(line_first)); - line_first[0] = 0; strncpy(line_first, cli->input, cli->x); - char line_end[strlen(cli->input) + 2]; memset(line_end, 0, sizeof(line_end)); - char *input_ptr = cli->input; strcpy(line_end, input_ptr + cli->x); char new_char[] = {c, 0}; @@ -203,7 +211,6 @@ void ricli_put_input(ricli_t *cli, char c) { } cli->history_index = cli->line_count; rterm_print_status_bar(cli->term, 'c', cli->input); - if (cli->x >= strlen(cli->input)) cli->x = strlen(cli->input) - 1; cli->x++; @@ -214,7 +221,6 @@ void ricli_load(ricli_t *cli, char *path) { strcpy(cli->history_file, path); size_t size = rfile_size(path); if (size == 0) { - return; } char *data = malloc(size + 1); @@ -231,6 +237,7 @@ void ricli_load(ricli_t *cli, char *path) { r4_free(r); free(data); } + void ricli_save(ricli_t *cli, char *path) { FILE *f = fopen(path, "w+"); for (int i = 0; i < cli->line_count; i++) { @@ -246,7 +253,7 @@ void ricli_save(ricli_t *cli, char *path) { fclose(f); } -ricli_delete_input(ricli_t *cli, unsigned int index) { +void ricli_delete_input(ricli_t *cli, unsigned int index) { if (cli->input[index + 1] == 0) { cli->input[index] = 0; } else { @@ -280,13 +287,10 @@ void ricli_keypress(rterm_t *rt) { } else if (rt->key.escape && rt->key.c == 'A') { if (cli->history_index != 0) cli->history_index--; - strcpy(cli->input, cli->lines[cli->history_index]->content); cli->x = strlen(cli->input); ricli_set_input(cli, cli->lines[cli->history_index]->content); - } else if (rt->key.c == 127) { - if (cli->x > 0) { cli->x--; cli->input[cli->x] = 0; @@ -295,17 +299,14 @@ void ricli_keypress(rterm_t *rt) { } else if (rt->key.escape && rt->key.c == 'B') { if (cli->history_index < cli->line_count - 1) { cli->history_index++; - strcpy(cli->input, cli->lines[cli->history_index]->content); cli->x = strlen(cli->input); ricli_set_input(cli, cli->lines[cli->history_index]->content); - } else { cli->x = 0; ricli_set_input(cli, ""); cli->history_index = cli->line_count; } - } else if (rt->key.escape && rt->key.c == 'D') { cli->x = rt->cursor.x; cursor_set(rt, cli->x, rt->cursor.y); @@ -322,13 +323,11 @@ void ricli_keypress(rterm_t *rt) { ricli_put_input(cli, rt->key.c); ricli_autocomplete_execute(cli); } - // rterm_print_status_bar(rt, 0, 0); } void ricli_loop(ricli_t *r) { rterm_loop(r->term); } int main() { - ricli_t *cli = ricli_terminal_new(); ricli_load(cli, "/tmp/.ricli.json"); ricli_loop(cli); diff --git a/rinterp.c b/rinterp.c index 050e022..ab77ab0 100644 --- a/rinterp.c +++ b/rinterp.c @@ -1,4 +1,13 @@ -#include "rlexer.c"; +// Written by retoor@molodetz.nl + +// This code defines an abstract syntax tree (AST) for a scripting language, implements AST node creation, and attempts to parse tokens from +// a lexer. + +// No additional imports or includes are used outside of the provided rlexer.c file. + +// MIT License + +#include "rlexer.c" typedef enum rip_ast_type_t { RIP_NONE = 0, RIP_BLOCK, RIP_CALL, RIP_LITERAL } rip_ast_type_t; @@ -10,7 +19,7 @@ typedef struct rip_ast_t { } rip_ast_t; rip_ast_t *rip_ast_new() { - rip_ast_t *ast = (rip_ast_t *)malloc(sizeof(rip_ast_t)); + rip_ast_t *ast = malloc(sizeof(rip_ast_t)); ast->children = NULL; ast->next = NULL; ast->previous = NULL; @@ -22,19 +31,21 @@ rip_ast_t *rip_parse() { rtoken_t token = rlex_next(); if (token.type == RT_CURLY_BRACE_OPEN) { rip_ast_t *ast = rip_ast_new(); - while () + while (1) { rip_ast_t *statement = rip_parse(); + } } + return NULL; } int main() { - char *script = "{print(\"test\")}"; rlex(script); - while (true) { + while (1) { rtoken_t token = rlex_next(); - if (token.type = RT_CURLY_BRACE_OPEN) { - rclos + if (token.type == RT_CURLY_BRACE_OPEN) { + // handle open } } + return 0; } \ No newline at end of file diff --git a/rio.c b/rio.c index 1e8c61a..8b66f10 100644 --- a/rio.c +++ b/rio.c @@ -1,9 +1,35 @@ +// Written by retoor@molodetz.nl + +// This program uses a function, 'rforfile', to iterate over files in the '/tmp' directory and prints their names using the callback +// function 'cb'. + +// External import: "rio.h" + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include "rio.h" void cb(char *str) { printf("%s\n", str); } int main() { rforfile("/tmp", cb); - return 0; } \ No newline at end of file diff --git a/rjson.c b/rjson.c index d2e03fe..52355f2 100644 --- a/rjson.c +++ b/rjson.c @@ -1,19 +1,33 @@ +// Written by retoor@molodetz.nl + +// This program initializes a JSON structure using the functions provided by the "rjson" library, adds different types of values to it, and +// then validates the JSON content. It concludes by freeing resources and ending the test. + +// This source code uses the "rjson" library to handle the creation and manipulation of JSON data. + +// MIT License + #include "rjson.h" int main() { rtest_banner("rjson"); + rjson_t *json = rjson(); rjson_array_start(json); rjson_object_start(json); + rjson_kv_string(json, "string", "value"); rjson_kv_number(json, "number", 1337421984); rjson_kv_duration(json, "duration", 1337421984); rjson_kv_int(json, "ulonglong", 1337420); rjson_kv_bool(json, "bool", true); + rjson_object_close(json); rjson_array_close(json); - rassert(!strcmp(json->content, "[{\"string\":\"value\",\"number\":\"1.337.421.984\",\"duration\":\"1." - "34s\",\"ulonglong\":1337420,\"bool\":true}]")); + + rassert(!strcmp(json->content, + "[{\"string\":\"value\",\"number\":\"1.337.421.984\",\"duration\":\"1.34s\",\"ulonglong\":1337420,\"bool\":true}]")); rjson_free(json); + return rtest_end(""); } \ No newline at end of file diff --git a/rkeytable.c b/rkeytable.c index 177e526..c0e30c9 100644 --- a/rkeytable.c +++ b/rkeytable.c @@ -1,12 +1,21 @@ +// Written by retoor@molodetz.nl + +// This code generates random keys and stores them in a key-value store, then asserts that the retrieved value matches the expected value +// "tast". + +// Includes: rkeytable.h, rtest.h, rstring.h + +// MIT License + #include "rkeytable.h" #include "rtest.h" #include "rstring.h" int main() { - for (int i = 0; i < 1000; i++) { char *key = rgenerate_key(); rkset(key, "tast"); rasserts(!strcmp(rkget(key), "tast")); } + return 0; } \ No newline at end of file diff --git a/rlexer.c b/rlexer.c index 17c7756..0faa451 100644 --- a/rlexer.c +++ b/rlexer.c @@ -1,3 +1,13 @@ +// Written by retoor@molodetz.nl + +// This code is a test suite for a lexer and formatter, involving functions to safely parse and format strings as per the predefined rules +// and tests for different token types. It checks numbers, symbols, strings, operators, punctuation, and code block delimiters among others, +// alongside testing grouping and line numbers. + +// External imports that are not included in the core language involve "rlexer.h", "rio.h", and "rtest.h". + +// MIT License + #include "rlexer.h" #include "rio.h" #include "rtest.h" @@ -19,14 +29,17 @@ void test_lexer() { "--++-+/*<>!@#$%^&*(){}?[]" "\n" "()"); + rtest_banner("Number"); rtoken_t token = rlex_next(); rtest_assert(token.type == RT_NUMBER); rtest_assert(!strcmp(token.value, "123")); + rtest_banner("Negative number"); token = rlex_next(); rtest_assert(token.type == RT_NUMBER); rtest_assert(!strcmp(token.value, "-123")); + rtest_banner("Decimal Number"); token = rlex_next(); rtest_assert(token.type == RT_NUMBER); @@ -37,10 +50,12 @@ void test_lexer() { token = rlex_next(); rtest_assert(token.type == RT_NUMBER); rtest_assert(!strcmp(token.value, "123.33")); + rtest_banner("Decimal Negative number"); token = rlex_next(); rtest_assert(token.type == RT_NUMBER); rtest_assert(!strcmp(token.value, "-123.33")); + rtest_banner("Symbol"); token = rlex_next(); rtest_assert(token.type == RT_SYMBOL); @@ -49,14 +64,12 @@ void test_lexer() { rtest_assert(token.type == RT_SYMBOL); rtest_assert(!strcmp(token.value, "_abc")); token = rlex_next(); - rtest_assert(token.type == RT_SYMBOL); rtest_assert(!strcmp(token.value, "abc_")); - token = rlex_next(); - rtest_assert(token.type == RT_SYMBOL); rtest_assert(!strcmp(token.value, "a_a")); + rtest_banner("String"); token = rlex_next(); rtest_assert(token.type == RT_STRING); @@ -70,11 +83,11 @@ void test_lexer() { rtest_banner("Operator"); token = rlex_next(); - rtest_assert(token.type == RT_OPERATOR); rtest_assert(!strcmp(token.value, "--++-+/*<>")); - rtest_banner("Punct") token = rlex_next(); + rtest_banner("Punct"); + token = rlex_next(); rtest_assert(token.type == RT_PUNCT); rtest_assert(!strcmp(token.value, "!@#$%^")); token = rlex_next(); @@ -85,27 +98,21 @@ void test_lexer() { token = rlex_next(); rtest_assert(token.type == RT_BRACE_OPEN); rassert(!strcmp(token.value, "(")); - token = rlex_next(); rtest_assert(token.type == RT_BRACE_CLOSE); rassert(!strcmp(token.value, ")")); - token = rlex_next(); rtest_assert(token.type == RT_CURLY_BRACE_OPEN); rassert(!strcmp(token.value, "{")); - token = rlex_next(); rtest_assert(token.type == RT_CURLY_BRACE_CLOSE); rassert(!strcmp(token.value, "}")); - token = rlex_next(); rtest_assert(token.type == RT_PUNCT); rassert(!strcmp(token.value, "?")); - token = rlex_next(); rtest_assert(token.type == RT_BRACKET_OPEN); rassert(!strcmp(token.value, "[")); - token = rlex_next(); rtest_assert(token.type == RT_BRACKET_CLOSE); rassert(!strcmp(token.value, "]")); @@ -143,25 +150,20 @@ void test_formatter() { free(formatted); formatted = rlex_format("\"123\",66,true,(1,2,3)"); char *expected_comma = "\"123\", 66, true, (1, 2, 3)"; - rtest_assert(!strcmp(formatted, expected_comma)); free(formatted); - formatted = rlex_format("lala lolo"); char *expected_new_lines1 = "lala\nlolo"; rtest_assert(!strcmp(formatted, expected_new_lines1)); free(formatted); - formatted = rlex_format("lala=lolo"); char *expected_new_lines2 = "lala = lolo"; rtest_assert(!strcmp(formatted, expected_new_lines2)); free(formatted); - formatted = rlex_format("lala+lolo=(1,2,3)"); char *expected_new_lines3 = "lala + lolo = (1, 2, 3)"; rtest_assert(!strcmp(formatted, expected_new_lines3)); free(formatted); - formatted = rlex_format("lala+lolo=(1,2,3) little.test=(4,5,6)"); char *expected_new_lines4 = "lala + lolo = (1, 2, 3)\nlittle.test = (4, 5, 6)"; rtest_assert(!strcmp(formatted, expected_new_lines4)); @@ -179,9 +181,7 @@ int main(int argc, char *argv[]) { } unsigned int length = rfile_size(argv[1]); char content[length + 1]; - length = rfile_readb(argv[1], content, length); - content[length] = 0; char *formatted = rlex_format(content); printf("%s", formatted); diff --git a/rlib.c b/rlib.c index 25542ed..d47fcfc 100644 --- a/rlib.c +++ b/rlib.c @@ -1,3 +1,12 @@ +// Written by retoor@molodetz.nl + +// This code serves as the main entry point for a program using the `rlib` library. It takes command-line arguments and passes them to the +// `rlib_main` function, initiating the program’s execution with these parameters. + +// Summary of used imports: Includes "rlib.h" which provides functionalities from the rlib library. + +// MIT License + #include "rlib.h" int main(int argc, char **argv) { return rlib_main(argc, argv); } \ No newline at end of file diff --git a/rlib.h b/rlib.h index 4387b52..0c50255 100644 --- a/rlib.h +++ b/rlib.h @@ -1,4 +1,4 @@ -// RETOOR - Dec 5 2024 +// RETOOR - Mar 16 2025 // MIT License // =========== @@ -65,6 +65,7 @@ typedef unsigned char byte; #ifndef NSOCK_H #define NSOCK_H +#include <unistd.h> #ifndef RMALLOC_H #define RMALLOC_H #ifndef RMALLOC_OVERRIDE diff --git a/rlibrlibso.c b/rlibrlibso.c index 4387b52..0c50255 100644 --- a/rlibrlibso.c +++ b/rlibrlibso.c @@ -1,4 +1,4 @@ -// RETOOR - Dec 5 2024 +// RETOOR - Mar 16 2025 // MIT License // =========== @@ -65,6 +65,7 @@ typedef unsigned char byte; #ifndef NSOCK_H #define NSOCK_H +#include <unistd.h> #ifndef RMALLOC_H #define RMALLOC_H #ifndef RMALLOC_OVERRIDE diff --git a/rlibso.c b/rlibso.c index 6a9dbf1..ab1a211 100644 --- a/rlibso.c +++ b/rlibso.c @@ -1,3 +1,11 @@ +// Written by retoor@molodetz.nl + +// This source code demonstrates memory allocation, deallocation, and formatted output using custom functions. + +// Custom functions/modules from external sources are not used in this example. + +// MIT License + #include <stdlib.h> #include <string.h> #include <stdio.h> @@ -8,11 +16,15 @@ int rtest_end(char *); void rprintgf(FILE *f, char *format, ...); int main() { - for (int i = 0; i < 100; i++) { + for (int i = 0; i < 100; ++i) { void *data = rmalloc(5000); + if (data == NULL) { + fprintf(stderr, "Memory allocation failed\n"); + return 1; + } memset(data, 0, 5000); rfree(data); } - rprintgf(stdout, "Hello from .so library!"); + rprintgf(stdout, "Hello from .so library!\n"); return rtest_end(""); } \ No newline at end of file diff --git a/rliza.c b/rliza.c index eafcfc5..1f2994f 100644 --- a/rliza.c +++ b/rliza.c @@ -1,13 +1,25 @@ +// Written by retoor@molodetz.nl + +// This C code performs JSON processing and validation using the rliza library, conducting performance tests, serialization, and +// deserialization of JSON data, and manually building JSON objects for testing purposes. It also ensures that data read operations are +// correctly performed from an external JSON file. + +// Imported libraries used in this code are rtest.h, rliza.h, rio.h, and rbench.h for testing, JSON handling, file operations, and +// benchmarking respectively. + +// MIT License + #define RMALLOC_OVERRIDE 1 #include "rtest.h" #include "rliza.h" #include "rio.h" #include "rbench.h" + void performance_test() { size_t size = rfile_size("resources/large.json"); char *data = malloc(size + 1); rfile_readb("resources/large.json", data, size); - data[size] = 0; + data[size] = '\0'; RBENCH(1, { int length = rliza_validate(data); (void)length; @@ -29,24 +41,8 @@ int main() { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\"}}\", null, \"AIOHTTP_SESSION_5a2510809b85492b8f14e8d3e2f11da3\"]}"; + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\"}}\", null, " + "\"AIOHTTP_SESSION_5a2510809b85492b8f14e8d3e2f11da3\"]}"; rassert(rliza_validate(long_data)); rliza_t *a = rliza_loads(&long_data); rliza_free(a); @@ -55,7 +51,6 @@ int main() { rliza_t *a = rliza_loads(&nested_obj); char *b = rliza_dumps(a); printf("%s\n", b); - // rliza_dumpss(a); rliza_free(a); free(b); @@ -103,7 +98,6 @@ int main() { rliza_free(to_object); - // rliza_free(to_object); rtest_banner("manually building new object"); rliza_t *rliza = rliza_new(RLIZA_OBJECT); rliza_set_integer(rliza, "a", 1); @@ -113,35 +107,31 @@ int main() { rliza_set_integer(rliza, "e", 5); rliza_set_integer(rliza, "f", 6); rliza_set_string(rliza, "str1", "str1value"); - rliza_set_null(rliza, "q"); char *original_content = rliza_dumps(rliza); printf("1:%s\n", original_content); char *content = original_content; - - printf("2:%s %d\n", content, content[strlen((char *)content)] == 0); + printf("2:%s %d\n", content, content[strlen(content)] == '\0'); rliza_t *rliza2 = rliza_loads(&content); - printf("HAAAh\n"); char *content2 = rliza_dumps(rliza2); printf("HAAAh\n"); content = original_content; - rassert(!(strcmp((char *)content, - (char *)content2))); // strcmp(content,content2); + rassert(!(strcmp(content, content2))); char *content2p = original_content; content = original_content; rliza_t *rliza3 = rliza_loads((char **)&content2p); char *content3 = rliza_dumps(rliza2); - rtest_banner("compare several serilizations. Should be equal.\n"); + rtest_banner("compare several serializations. Should be equal.\n"); content = original_content; printf("content1:<%s>\n", content); printf("content2:<%s>\n", content2); printf("content3:<%s>\n", content3); - rassert(!strncmp(content2, content3, strlen((char *)content2))); - rassert(!strncmp(content, content2, strlen((char *)content))); + rassert(!strncmp(content2, content3, strlen(content2))); + rassert(!strncmp(content, content2, strlen(content))); rliza_free(rliza2); rliza_free(rliza3); free(original_content); @@ -153,4 +143,4 @@ int main() { rliza_free(rliza); return rtest_end(""); -} +} \ No newline at end of file diff --git a/rmalloc.c b/rmalloc.c index 4a8d3db..7e83acd 100644 --- a/rmalloc.c +++ b/rmalloc.c @@ -1,5 +1,33 @@ -#include "rtest.h" +// Written by retoor@molodetz.nl +// This source code demonstrates a test suite for memory allocation using custom `malloc`, `realloc`, and `calloc` in the `rtest` framework. +// It performs a series of memory allocation and deallocation operations, checking the count of allocations and deallocations to ensure +// proper memory management and detect any leaks or errors. + +// Dependencies: +// This code relies on the custom libraries `rtest` and `rmalloc` specific to this testing framework. + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include "rtest.h" #include "rmalloc.h" void rtest_malloc() { @@ -12,27 +40,33 @@ void rtest_malloc() { void *w = calloc(1, 10); rtest_true(rmalloc_alloc_count == 5); - rtest_banner("free") x = free(x); + rtest_banner("free"); + x = free(x); rtest_true(x == NULL); rtest_true(rmalloc_count == 4); - rtest_banner("another free") y = free(y); + rtest_banner("another free"); + y = free(y); rtest_true(y == NULL); rtest_true(rmalloc_count == 3); - rtest_banner("third free") z = free(z); + rtest_banner("third free"); + z = free(z); rtest_true(z == NULL); rtest_true(rmalloc_count == 2); - rtest_banner("third four") w = free(w); + rtest_banner("third four"); + w = free(w); rtest_true(w == NULL); rtest_true(rmalloc_count == 1); - rtest_banner("third five") ptr = free(ptr); + rtest_banner("third five"); + ptr = free(ptr); rtest_true(ptr == NULL); rtest_true(rmalloc_count == 0); - rtest_banner("totals") rtest_true(rmalloc_alloc_count == 5); + rtest_banner("totals"); + rtest_true(rmalloc_alloc_count == 5); rtest_true(rmalloc_free_count == 5); rtest_true(rmalloc_count == 0); } diff --git a/rmerge.c b/rmerge.c index 064d4dd..c1e2ae4 100644 --- a/rmerge.c +++ b/rmerge.c @@ -1,3 +1,30 @@ +// Written by retoor@molodetz.nl + +// This code is the entry point for a program that utilizes the `rmerge` library. It passes command line arguments to `rmerge_main` for +// further processing. + +// External library included: "rmerge.h" + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include "rmerge.h" -int main(int argc, char *argv[]) { return rmerge_main(argc, argv); } +int main(int argc, char *argv[]) { return rmerge_main(argc, argv); } \ No newline at end of file diff --git a/rnet.c b/rnet.c index f471df7..9f9183c 100644 --- a/rnet.c +++ b/rnet.c @@ -1,36 +1,53 @@ +// Written by retoor@molodetz.nl + +// This source code sets up a simple server using the rnet library to handle client connections. The server waits for client connections, +// reads data, and responds with simple HTTP headers. It closes the connection if a "GET" request is received. + +// The non-standard import in this code is "rnet.h". + +// MIT License + #include "rnet.h" void on_client_connect(rnet_socket_t *sock) { printf("%s connected\n", sock->name); } + void on_client_read(rnet_socket_t *sock) { unsigned char *data = net_socket_read(sock, 4096); if (!data) return; - char *http_headers = "HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\n"; + + const char *http_headers = "HTTP/1.1 200 OK\r\nContent-Length: 10\r\nConnection: close\r\n\r\n"; net_socket_write(sock, (unsigned char *)http_headers, strlen(http_headers)); + rnet_safe_str((char *)data, sock->bytes_received); - // data[11] = 0; printf("%s: %.30s\n", sock->name, data); + net_socket_write(sock, data, strlen((char *)data)); - if (!strncmp((char *)data, "GET ", 4)) + if (!strncmp((char *)data, "GET ", 4)) { net_socket_close(sock); + } } + void on_client_close(rnet_socket_t *sock) { printf("%s disconnected\n", sock->name); } int main(int argc, char *argv[]) { if (argc < 2) { - printf("usage: [port].\n"); + printf("Usage: [port].\n"); return 1; } + for (int i = 0; i < argc; i++) { if (strcmp(argv[i], "test") == 0) { printf("Skipping rnet tests.\n"); return 0; } } + rnet_server_t *server = net_socket_serve((unsigned int)atoi(argv[1]), 10); server->on_connect = on_client_connect; server->on_read = on_client_read; server->on_close = on_client_close; + while (true) { if (net_socket_select(server)) { printf("Handled all events.\n"); @@ -38,5 +55,6 @@ int main(int argc, char *argv[]) { printf("No events to handle.\n"); } } + return 0; -} +} \ No newline at end of file diff --git a/rprint.c b/rprint.c index b297293..d43dae0 100644 --- a/rprint.c +++ b/rprint.c @@ -1,21 +1,35 @@ +// Written by retoor@molodetz.nl + +// This program benchmarks different methods for outputting or printing text in C, using a custom benchmarking utility defined in rbench.h. + +// The code uses rbench.h and rprint.h for benchmarking and custom printing functions beyond standard C functions. + +// MIT License +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to +// the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions +// of the Software. + #include "rprint.h" #include "rbench.h" void test_putc(void *arg) { char *str = (char *)arg; - char c; - while ((c = *str++)) { + while (*str) { putc('\r', stdout); + str++; } } void test_putchar(void *arg) { char *str = (char *)arg; - char c; - while ((c = *str++)) { + while (*str) { putchar('\r'); + str++; } } + void test_fwrite(void *arg) { int length; if (rbf->first) { @@ -26,8 +40,11 @@ void test_fwrite(void *arg) { } fwrite((char *)arg, 1, length, stdout); } + void test_printf(void *arg) { printf("%s", (char *)arg); } + void test_rprint(void *arg) { rprint("%s", (char *)arg); } + void test_rprintr(void *arg) { rprintr("%s", (char *)arg); } int main() { @@ -44,4 +61,4 @@ int main() { r->execute1(r, times, " \r"); r->execute1(r, times, "\\c\\T\\l\\L \r"); return rtest_end(""); -} +} \ No newline at end of file diff --git a/rrex3.c b/rrex3.c index 80ab15f..daf74ba 100644 --- a/rrex3.c +++ b/rrex3.c @@ -1,3 +1,11 @@ +// Written by retoor@molodetz.nl + +// This program tests the `rrex3` regular expression parser using the `rrex3_test` function and prints a message indicating the testing +// phase. + +// Includes rrex3.h and rtest.h for the regular expression parser and the testing functionalities. + +// MIT License #include "rrex3.h" #include "rtest.h" @@ -6,5 +14,4 @@ int main() { printf("Testing rrex3 regular expression parser."); rrex3_test(); return 0; - // return rtest_end(""); -} +} \ No newline at end of file diff --git a/rrex4.c b/rrex4.c index a37b08a..8266371 100644 --- a/rrex4.c +++ b/rrex4.c @@ -1,3 +1,13 @@ +// Written by retoor@molodetz.nl + +// This source code provides benchmarking functions to compare performance between custom regex engine 'r4' and the C standard library +// regex, and includes tests and statistics collection for regex matches. + +// Used external imports: "rrex4.h", "rtest.h", "rbench.h". They provide custom regex and benchmarking functionality beyond the standard +// 'regex.h'. + +// MIT License + #include "rrex4.h" #include "rtest.h" #include "rbench.h" @@ -6,13 +16,10 @@ bool bench_r4(unsigned int times, char *str, char *expr) { RBENCH(times, { r4_t *r = r4(str, expr); - - if (r->valid == false) { - + if (!r->valid) { printf("Bench r4 error\n"); exit(1); } - r4_free(r); }); return true; @@ -30,7 +37,6 @@ void bench_c(unsigned int times, char *str, char *expr) { exit(1); } }); - regfree(®ex); } @@ -51,26 +57,21 @@ void test_r4_next() { assert(r->valid); assert(r->match_count == 1); assert(!strcmp(r->matches[0], "abcd")); - // Again with same regex as parameter r = r4_next(r, reg); assert(r->valid); assert(r->match_count == 1); assert(!strcmp(r->matches[0], "efgh")); - // Again with same regex as parameter r = r4_next(r, reg); assert(r->valid); assert(r->match_count == 1); assert(!strcmp(r->matches[0], "ijkl")); - // Reuse expression, NULL parameter r = r4_next(r, NULL); assert(r->valid); assert(r->match_count == 1); assert(!strcmp(r->matches[0], "mnop")); - // No results using r4_next r = r4_next(r, NULL); assert(r->valid); assert(r->match_count == 0); - // Again no results using r4_next, Shouldn't crash r = r4_next(r, NULL); assert(r->valid); assert(r->match_count == 0); @@ -78,9 +79,7 @@ void test_r4_next() { } void bench_all(unsigned int times) { - assert(bench(times, "suvw", - "[abcdefghijklmnopqrstuvw][abcdefghijklmnopqrstuvw][" - "abcdefghijklmnopqrstuvw][abcdefghijklmnopqrstuvw]")); + assert(bench(times, "suvw", "[abcdefghijklmnopqrstuvw][abcdefghijklmnopqrstuvw][abcdefghijklmnopqrstuvw][abcdefghijklmnopqrstuvw]")); assert(bench(times, "ponyyy", "^p+o.*yyy$$$$")); assert(bench(times, " ponyyzd", "p+o.*yyzd$$$$")); assert(bench(times, "abc", "def|gek|abc")); @@ -100,12 +99,9 @@ bool r4_match_stats(char *str, char *expr) { } int main() { - assert(r4_match_stats("NL18RABO0322309700", "(\\w{2})(\\d{2})(\\w{4}\\d)(\\d{10})")); - unsigned int times = 1000; bench_all(times); - RBENCH(1, { assert(r4_match_stats("#define DEFINETEST 1\n", "#define\\s+(\\w[\\d\\w_]+)\\s+([\\w\\d_]+)")); assert(r4_match_stats("#define DEFINETEST 1\n", "#define\\s+(\\w[\\d\\w_]+)\\s+([\\w\\d_]+)")); @@ -118,7 +114,6 @@ int main() { assert(r4_match_stats("1234567", "12(.*)67$")); assert(r4_match_stats("12111678993", "12(.*)67(.*)3$")); assert(r4_match_stats("NL17RABO0322309700", "NL(.*)R(.*)0(.*)0(.*)0$")); - assert(r4_match_stats("NL18RABO0322309700", "(\\w{2})(\\d{2})(\\w{4}\\d)(\\d+)$")); assert(r4_match_stats("NL18RABO0322309700garbage", "(\\w{2})(\\d{2})(\\w{4}\\d)(\\d+)")); assert(r4_match_stats("NL18RABO0322309700", "(\\w{2})(\\d{2})(\\w{4}\\d)(\\d+)$")); @@ -146,13 +141,11 @@ int main() { assert(r4_match_stats("abc", "def|gek|abc")); assert(!r4_match_stats("abc", "def|gek|abd")); assert(r4_match_stats("abc", "def|abc|def")); - assert(r4_match_stats("suwv", "[abcdesfghijklmnopqrtuvw][abcdefghijklmnopqrstuvw][" - "abcdefghijklmnopqrstuvw][abcdefghijklmnopqrstuvw]")); + assert( + r4_match_stats("suwv", "[abcdesfghijklmnopqrtuvw][abcdefghijklmnopqrstuvw][abcdefghijklmnopqrstuvw][abcdefghijklmnopqrstuvw]")); test_r4_next(); r4_enable_debug(); - assert(r4_match_stats("123", "(.*)(.*)(.*)")); }); - return 0; -} +} \ No newline at end of file diff --git a/rstring.c b/rstring.c index 96404ca..6656861 100644 --- a/rstring.c +++ b/rstring.c @@ -1,3 +1,12 @@ +// Written by retoor@molodetz.nl + +// This source code contains a suite of tests for string manipulation functions such as removing whitespace, tokenizing lines, sorting +// strings, formatting numbers, and more. It ensures these functions behave as expected with various input cases. + +// It includes the "rstring.h" and "rtest.h" libraries, which are assumed to provide functionality for string manipulation and a testing +// framework, respectively. + +// MIT License #include "rstring.h" #include "rtest.h" @@ -19,7 +28,6 @@ void rstring_test_whitespace() { rtest_banner("rstrip_whitespace"); char output[1024]; - // Test 1 char *string1 = " Test 1"; rstrip_whitespace(string1, output); rassert(strlen(output) == 6); @@ -42,11 +50,9 @@ void rstring_test_rstrtokline() { char lines[1024] = "Line 1\nLine 2\nLine 3\nLine 4\n333.29\n3.2221"; char line[1024]; size_t offset = 0; - // Test 1 while ((offset = rstrtokline(lines, line, offset, true)) && *line) { rassert(strlen(line) == 6); } - // Test 2 offset = 0; int count = 0; while ((offset = rstrtokline(lines, line, offset, false)) && *line) { @@ -54,7 +60,6 @@ void rstring_test_rstrtokline() { count++; rassert(strlen(line) == expected_length); } - // Test 3 offset = 0; strcat(lines, "\n"); count = 0; @@ -99,7 +104,7 @@ void rstring_test_rstraddslashes() { char input[] = "\r\t\n\b\f test"; char output[100]; rstraddslashes(input, output); - rassert(!strcmp((char *)output, "\\r\\t\\n\\b\\f test")); + rassert(!strcmp(output, "\\r\\t\\n\\b\\f test")); } void rstring_test_rstrstripslashes() { @@ -107,7 +112,7 @@ void rstring_test_rstrstripslashes() { char input[] = "\\r\\t\\n\\b\\f\" test"; char output[100]; rstrstripslashes(input, output); - rassert(!strcmp((char *)output, "\r\t\n\b\f\" test")); + rassert(!strcmp(output, "\r\t\n\b\f\" test")); } void rstring_test_rstrstartswith() { @@ -135,30 +140,26 @@ void rstring_test_rstrendswith() { void rstring_test_rstrmove() { rtest_banner("Move str"); - // Test 1 char to_move_1[] = "abc?"; rstrmove(to_move_1, 3, 1, 0); rassert(!strcmp(to_move_1, "?abc")); - // Test 2 + char to_move_2[] = "abc?defgabc"; rstrmove(to_move_2, 3, 5, 0); rassert(!strcmp(to_move_2, "?defgabcabc")); - // Test 3 + char to_move_3[] = "abc?defg"; rstrmove(to_move_3, 0, 3, 7); rassert(!strcmp(to_move_3, "?defgabc")); - // Test 4 char to_move_4[] = "abc?defgaa"; rstrmove2(to_move_4, 3, 5, 0); rassert(!strcmp(to_move_4, "?defgabcaa")); - // Test 5 char to_move_5[] = "?defgabcaa"; rstrmove2(to_move_5, 0, 5, 3); rassert(!strcmp(to_move_5, "abc?defgaa")); - // Test 6 char to_move_6[] = "?defgabcaa"; rstrmove2(to_move_6, 0, 5, 6); rassert(!strcmp(to_move_6, "abcaa?defg")); @@ -176,4 +177,4 @@ int main() { rstring_test_rstrendswith(); rstring_test_rstrmove(); return rtest_end(""); -} +} \ No newline at end of file diff --git a/rstring_list.c b/rstring_list.c index c87526c..fd36a90 100644 --- a/rstring_list.c +++ b/rstring_list.c @@ -1,3 +1,12 @@ +// Written by retoor@molodetz.nl + +// This source code is a simple testing routine for a string list implementation, verifying the addition, containment, and proper memory +// management functionality. + +// The code uses custom imports: "rstring_list.h" and "rtest.h" for managing the string list and handling test scenarios respectively. + +// MIT License: This code is free to use and distribute with attribution to the original author. No warranty is provided express or implied. + #include "rstring_list.h" #include "rtest.h" @@ -5,18 +14,18 @@ void test_rstring_list() { rtest_banner("new"); rstring_list_t *rsl = rstring_list_new(); rassert(rsl->count == 0); - rassert(rsl->count == 0); + rtest_banner("add"); rstring_list_add(rsl, "test1"); rassert(rsl->count == 1); - rassert(rsl->count == 1); rstring_list_add(rsl, "test2"); rassert(rsl->count == 2); - rassert(rsl->count == 2); + rtest_banner("contains"); rassert(rstring_list_contains(rsl, "test1")); rassert(rstring_list_contains(rsl, "test2")); rassert(!rstring_list_contains(rsl, "test3")); + rtest_banner("free"); rstring_list_free(rsl); } @@ -24,4 +33,4 @@ void test_rstring_list() { int main() { test_rstring_list(); return rtest_end(""); -} +} \ No newline at end of file diff --git a/rterm.c b/rterm.c index f25c74b..6d43864 100644 --- a/rterm.c +++ b/rterm.c @@ -1,21 +1,40 @@ +// Written by retoor@molodetz.nl + +// This program interfaces with the 'rterm' library to handle terminal behaviors such as cursor movement, key presses, and updates with a +// periodic tick function. + +// The code utilizes the 'rterm' library through the import "rterm.h" for managing terminal state and user interactions. + +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include "rterm.h" -void before_cursor_move(rterm_t *rterm) { - // printf("Before cursor update: %d:%d\n",rterm->cursor.x,rterm->cursor.y); -} -void after_cursor_move(rterm_t *rterm) { +void before_cursor_move(rterm_t *rterm) {} - // rterm->cursor.x++; -} -void before_key_press(rterm_t *rterm) { +void after_cursor_move(rterm_t *rterm) {} + +void before_key_press(rterm_t *rterm) {} - // if(rterm->key.c == 65 && rterm->key.escape){ - // rterm->key.c = 66; - //} -} void tick(rterm_t *rt) { - static char status_text[1024]; - status_text[0] = 0; + static char status_text[1024] = {0}; sprintf(status_text, "\rp:%d:%d | k:%c:%d | i:%ld ", rt->cursor.x + 1, rt->cursor.y + 1, rt->key.c == 0 ? '0' : rt->key.c, rt->key.c, rt->iterations); rt->status_text = status_text; diff --git a/rterminal.c b/rterminal.c index baa1d3e..2828fb4 100644 --- a/rterminal.c +++ b/rterminal.c @@ -1,3 +1,26 @@ +// Written by retoor@molodetz.nl + +// This program executes a progress bar test using functions from external libraries. + +// Includes external libraries "rterminal.h" and "rtest.h" + +// MIT License +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #include "rterminal.h" #include "rtest.h" diff --git a/rtime.c b/rtime.c index 9ad9f0b..968113a 100644 --- a/rtime.c +++ b/rtime.c @@ -1,3 +1,12 @@ +// Written by retoor@molodetz.nl + +// This program tests the functionality of converting milliseconds into a readable format using assertions to verify correct output, using +// an assumed custom testing and time conversion library. + +// The source code uses the custom includes "rtime.h" for time conversion and "rtest.h" for testing framework functionalities. + +// MIT License + #include "rtime.h" #include "rtest.h" #include <string.h> @@ -5,15 +14,15 @@ int main() { rtest_banner("time"); rtest_banner("Milliseconds tests"); - rtest_assert(!strcmp(msecs_str(0), "0Ms")); - rtest_assert(!strcmp(msecs_str(1), "1Ms")); - rtest_assert(!strcmp(msecs_str(12), "12Ms")); - rtest_assert(!strcmp(msecs_str(123), "123Ms")); - rtest_assert(!strcmp(msecs_str(999), "999Ms")); + rtest_assert(strcmp(msecs_str(0), "0Ms") == 0); + rtest_assert(strcmp(msecs_str(1), "1Ms") == 0); + rtest_assert(strcmp(msecs_str(12), "12Ms") == 0); + rtest_assert(strcmp(msecs_str(123), "123Ms") == 0); + rtest_assert(strcmp(msecs_str(999), "999Ms") == 0); rtest_banner("Second tests"); - rtest_assert(!strcmp(msecs_str(1000), "1s")); - rtest_assert(!strcmp(msecs_str(1100), "1.1s")); - rtest_assert(!strcmp(msecs_str(1234), "1.234s")); - rtest_assert(!strcmp(msecs_str(12345), "12.345s")); - return rtest_end("successs"); + rtest_assert(strcmp(msecs_str(1000), "1s") == 0); + rtest_assert(strcmp(msecs_str(1100), "1.1s") == 0); + rtest_assert(strcmp(msecs_str(1234), "1.234s") == 0); + rtest_assert(strcmp(msecs_str(12345), "12.345s") == 0); + return rtest_end("success"); } \ No newline at end of file diff --git a/rtree.c b/rtree.c index ac91229..08bd4e6 100644 --- a/rtree.c +++ b/rtree.c @@ -1,3 +1,12 @@ +// Written by retoor@molodetz.nl + +// This code provides functionality for testing and benchmarking `rtree` operations, including setting up, manipulating, and freeing nodes, +// as well as measuring the performance of these operations. + +// The code imports `rtree.h`, `rbench.h`, `rtest.h`, and the standard library `string.h`. + +// MIT License + #include "rtree.h" #include "rbench.h" #include "rtest.h" @@ -33,7 +42,6 @@ void rtree_bench(long item_count) { b->silent = true; b->add_function(b, "random_key", "rtree_rw", rtree_bench_f); b->execute(b, 1000000); - // rassert(rnsecs_to_msecs(b->execution_time) < 700); // faster than 700ms printf("r/w %ld items and deallocated in %s\n", item_count, format_time(b->execution_time)); rbench_free(b); } @@ -41,22 +49,18 @@ void rtree_bench(long item_count) { int main() { rtest_banner("rtree"); rtree_t *rtree = rtree_new(); - // Test new object default valuess rtest_banner("New object default values"); rtest_assert(rtree->c == 0); rtest_assert(rtree->next == NULL); rtest_assert(rtree->children == NULL); rtest_assert(rtree->data == NULL); - // Test set rtest_banner("Set"); rtree_set(rtree, "a", "data"); rtest_assert(rtree->c == 'a'); rtest_assert(!strcmp(rtree->data, "data")); - // Second element should be filled rtree_set(rtree, "b", "data2"); rtest_assert(rtree->next->c == 'b'); rtest_assert(!strcmp(rtree->next->data, "data2")); - // First child of second element should be filled rtree_set(rtree, "bc", "data3"); rtest_assert(rtree->next->children->c == 'c'); rtest_assert(!strcmp(rtree->next->children->data, "data3")); @@ -64,4 +68,4 @@ int main() { rtest_banner("Benchmark"); rtree_bench(1000000); return rtest_end(""); -} +} \ No newline at end of file diff --git a/rtypes.c b/rtypes.c index 3e2ecf4..ec0d826 100644 --- a/rtypes.c +++ b/rtypes.c @@ -1,14 +1,29 @@ +// Written by retoor@molodetz.nl + +// This program tests various types using the rtypes and rtest libraries, asserting the expected behaviors of the defined types such as +// unsigned integers, long integers, and byte values. + +// The source includes custom libraries "rtest.h" and "rtypes.h" which provide functionality for type definition and testing assertions. + +// The code is licensed under the MIT License. + #include "rtest.h" #include "rtypes.h" int main() { rtest_banner("rtypes"); + uint test1 = 1000; - rassert(test1 == 1000) ulong test2 = -1000; + rassert(test1 == 1000); + + ulong test2 = -1000; rassert(test2 == -1000); + byte test3 = 123; rassert(test3 == 123); + byte test4 = 'A'; rassert(test4 == 65); + return rtest_end(""); -} +} \ No newline at end of file