From 00719b1933e803ca94772582bb3314c9d9a96214 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 9 Oct 2025 18:43:09 +0200 Subject: [PATCH] refactor code with new formatting rules Functions now break before their brace, mirroring the Linux kernel. The reason for this is that breaking the parameter list otherwise makes code unreadable. --- .clang-format | 6 +++--- src/dat/mcx.c | 27 ++++++++++++++++++--------- src/dat/nbt.c | 30 ++++++++++++++++++++---------- src/io/input.c | 3 ++- src/io/render.c | 12 ++++++++---- src/io/shader.c | 6 ++++-- src/io/window.c | 18 ++++++++++++------ src/main.c | 9 ++++++--- src/util/compat/io.h | 6 ++++-- src/util/conf.c | 37 ++++++++++++++++++++++++------------- src/util/error.c | 20 ++++++++++++-------- test/main.c | 3 ++- test/test.c | 3 ++- test/test_conf.c | 30 ++++++++++++++++++++---------- 14 files changed, 137 insertions(+), 73 deletions(-) diff --git a/.clang-format b/.clang-format index 9f59171..60fb2d9 100644 --- a/.clang-format +++ b/.clang-format @@ -34,9 +34,9 @@ AlignConsecutiveShortCaseStatements: # short constructs on a single line # --------------------------- AllowShortBlocksOnASingleLine: Always -AllowShortFunctionsOnASingleLine: All +AllowShortFunctionsOnASingleLine: None AllowShortLambdasOnASingleLine: All -AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortIfStatementsOnASingleLine: Never AllowShortCaseLabelsOnASingleLine: true AllowShortEnumsOnASingleLine: true AllowShortLoopsOnASingleLine: true @@ -61,7 +61,7 @@ BraceWrapping: AfterCaseLabel: false AfterClass: false AfterEnum: false - AfterFunction: false + AfterFunction: true AfterNamespace: false AfterObjCDeclaration: false AfterStruct: false diff --git a/src/dat/mcx.c b/src/dat/mcx.c index b6f348b..fcc3d04 100644 --- a/src/dat/mcx.c +++ b/src/dat/mcx.c @@ -26,7 +26,8 @@ enum mcx_compression { }; /* first 4 bytes is an i32 indicating remaining bytes, the following byte defines the compression scheme */ -static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int idx) { +static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int idx) +{ const u8 *chunk = buf + (be32toh(table[idx]) >> 8) * SECTOR; i32 len; @@ -64,7 +65,8 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int error("failed to decompress %i bytes of compression type %i", len, *chunk); return 1; } - if (size == 0) break; + if (size == 0) + break; // TODO: handle data } @@ -72,7 +74,8 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int } /* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */ -static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e) { +static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e) +{ assert(src > dst); uintptr len = src - dst; // acquire the amount of bytes that we shall move assert(!(len % SECTOR)); @@ -89,7 +92,8 @@ static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e /* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`. * `rmb` is an optional additional offset that can be applied, and signifies bytes already removed. * Returns the bytes removed by this function. */ -static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx, int eidx) { +static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx, int eidx) +{ // load the table data usize slen, bidx, blen; slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk @@ -110,7 +114,8 @@ static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx /* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well. * This is done instead of `delchunk` being globally linked, because * `delchunk` requests more specific parameters, which is confusing outside this module. */ -usize mcx_delchunk(u8 *restrict buf, int chunk) { +usize mcx_delchunk(u8 *restrict buf, int chunk) +{ u32 table[TABLE]; memcpy(table, buf, sizeof(table)); usize res = delchunk(buf, table, 0, chunk, CHUNKS); @@ -118,7 +123,8 @@ usize mcx_delchunk(u8 *restrict buf, int chunk) { return res; } -usize mcx_delchunk_range(u8 *restrict buf, int start, int end) { +usize mcx_delchunk_range(u8 *restrict buf, int start, int end) +{ assert(start < end && end < CHUNKS); u32 table[TABLE]; memcpy(table, buf, sizeof(table)); @@ -141,7 +147,8 @@ usize mcx_delchunk_range(u8 *restrict buf, int start, int end) { } /* comparer function for to be inputted into `qsort` to compare two */ -static int cmp_chunkids(const void *restrict x, const void *restrict y) { +static int cmp_chunkids(const void *restrict x, const void *restrict y) +{ u16 x2 = *(u16 *)x; u16 y2 = *(u16 *)y; return (x2 > y2) - (x2 < y2); @@ -149,7 +156,8 @@ static int cmp_chunkids(const void *restrict x, const void *restrict y) { /* Sorts the chunks marked for deletion from smallest to greatest index. * Then performs the deletion in this order. Making sure to only update the chunks up to the next. */ -usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) { +usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) +{ // ensure the chunks ids we're working on are sorted from least to greatest u16 chunkids[chunkc + 1]; memcpy(chunkids, chunks, chunkc); @@ -169,7 +177,8 @@ usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc /* Sum together the 4th byte in each location integer to compute the sector size of all chunks. * Multiplying by `SECTOR`, and adding the size of the table itself. */ -usize mcx_calcsize(const u8 *restrict buf) { +usize mcx_calcsize(const u8 *restrict buf) +{ usize size = 0; for (uint i = 0; i < CHUNKS; i++) size += *(buf + (i * 4) + 3); diff --git a/src/dat/nbt.c b/src/dat/nbt.c index 90c5c12..1ab8387 100644 --- a/src/dat/nbt.c +++ b/src/dat/nbt.c @@ -13,21 +13,24 @@ #define MAX_DEPTH 512 /* Extracts a big endian 16 bit integer from address `buf`, converts it to host byte size if needed and returns. */ -static inline u16 buftoh16(const void *restrict buf) { +static inline u16 buftoh16(const void *restrict buf) +{ u16 i; memcpy(&i, buf, sizeof(i)); return be16toh(i); } /* Extracts a big endian 32 bit integer from address `buf`, converts it to host byte size if needed and returns. */ -static inline u32 buftoh32(const void *restrict buf) { +static inline u32 buftoh32(const void *restrict buf) +{ u32 i; memcpy(&i, buf, sizeof(i)); return be32toh(i); } /* Extracts a big endian 64 bit integer from address `buf`, converts it to host byte size if needed and returns. */ -static inline u64 buftoh64(const void *restrict buf) { +static inline u64 buftoh64(const void *restrict buf) +{ u64 i; memcpy(&i, buf, sizeof(i)); return be64toh(i); @@ -36,7 +39,8 @@ static inline u64 buftoh64(const void *restrict buf) { /* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`. * The data shall be converted to little-endian on little-endian systems * Outputs the allocated data to `out`, returns where the next pointer would be. */ -static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) { +static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) +{ usize len = nmemb * size; *out = (struct nbt_array){ out->nmemb = nmemb, @@ -51,7 +55,8 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nb /* Only include this code for little-endian systems. Since only they require this logic. * Producing optimised code for other platforms. */ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - if (size == 1) return buf; + if (size == 1) + return buf; i32 i = 0; while (i < nmemb) { switch (size) { @@ -67,7 +72,8 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nb } /* calls `procarr` for the simple types available. */ -static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out) { +static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out) +{ uint size; switch (*(u8 *)buf) { @@ -88,7 +94,8 @@ static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out return procarr(buf, len, size, out); } -const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) { +const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) +{ const u8 *ptr, *tmp; ptr = buf + 3 + slen; @@ -123,7 +130,8 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) { * `ptr` is assumed to be the start of the `NBT_LIST` data, e.i. The list's ID, followed by the list's length. * If `ID` is `NBT_I8`, `NBT_I16`, `NBT_I32`, `NBT_I64`, `NBT_F32`, or `NBT_F64`, the entire list length is computed and returned. * For other types this won't be possible, and thus will add `1` to `dpt`, and write the list data to `lens` and `tags` at this new `dpt`. */ -static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) { +static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) +{ const u8 *tag = ptr; ptr++; switch (*tag) { @@ -151,7 +159,8 @@ static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt, * - `lens` shall contain `MAX_DEPTH` of items representing the list length, if the current item is non-zero we shall assume we're in a list. * Where the value is decremented until we reach `0`. * - `tags` shall contain `MAX_DEPTH` of items representing the list's stored type. */ -static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) { +static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *restrict const lens, u8 *restrict const tags) +{ u8 type; const u8 *ptr = tag; if (lens[*dpt]) { @@ -193,7 +202,8 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 * * - compound:list:int32 * - string */ -const u8 *nbt_nexttag(const u8 *restrict buf) { +const u8 *nbt_nexttag(const u8 *restrict buf) +{ const u8 *tag; u8 tags[MAX_DEPTH] = {0}; i32 lens[MAX_DEPTH] = {0}; diff --git a/src/io/input.c b/src/io/input.c index 7cf6355..f3a8de2 100644 --- a/src/io/input.c +++ b/src/io/input.c @@ -4,7 +4,8 @@ #include -void input_callback(GLFWwindow *win, int key, int scancode, int action, int mods) { +void input_callback(GLFWwindow *win, int key, int scancode, int action, int mods) +{ (void)win, (void)key, (void)scancode, (void)action, (void)mods; // make the compiler shut up as this is fine #ifndef NDEBUG if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) diff --git a/src/io/render.c b/src/io/render.c index 67b274c..0804f88 100644 --- a/src/io/render.c +++ b/src/io/render.c @@ -17,7 +17,8 @@ static GLuint vao; // vertex array object static GLuint screen_loc; // location to where OpenGL sends to the shaders of the screen dimensions static int win_w, win_h; -static void screen_resize(int w, int h) { +static void screen_resize(int w, int h) +{ i32 verts[VERTC][4] = { {0, 0, w, 20 }, {0, 20, w, h - 40}, @@ -31,7 +32,8 @@ static void screen_resize(int w, int h) { win_h = h; } -int render_init(void) { +int render_init(void) +{ pipe = glCreateProgram(); shader_init(pipe); // initialise and include the shaders glLinkProgram(pipe); // link the application @@ -63,7 +65,8 @@ int render_init(void) { return 0; } -void render_update(GLFWwindow *win) { +void render_update(GLFWwindow *win) +{ glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); @@ -80,7 +83,8 @@ void render_update(GLFWwindow *win) { glDrawArrays(GL_POINTS, 0, VERTC); } -void render_free(void) { +void render_free(void) +{ glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbo); glDeleteProgram(pipe); diff --git a/src/io/shader.c b/src/io/shader.c index 83b1f74..037f3a9 100644 --- a/src/io/shader.c +++ b/src/io/shader.c @@ -19,7 +19,8 @@ extern const uint sh_geom_glsl_len; /* Compiles a shader of `type` from `src` with `len` bytes. * Returns the integer for the shader. */ -static GLuint shader_compile(GLenum type, const char *src, usize len) { +static GLuint shader_compile(GLenum type, const char *src, usize len) +{ int ilen = len; GLuint shader = glCreateShader(type); glShaderSource(shader, 1, &src, &ilen); @@ -37,7 +38,8 @@ static GLuint shader_compile(GLenum type, const char *src, usize len) { return shader; } -int shader_init(GLuint pipe) { +int shader_init(GLuint pipe) +{ GLuint vs = shader_compile(GL_VERTEX_SHADER, sh_vert_glsl, sh_vert_glsl_len); GLuint fs = shader_compile(GL_FRAGMENT_SHADER, sh_frag_glsl, sh_frag_glsl_len); GLuint gs = shader_compile(GL_GEOMETRY_SHADER, sh_geom_glsl, sh_geom_glsl_len); diff --git a/src/io/window.c b/src/io/window.c index 54b2dc7..fbed2e3 100644 --- a/src/io/window.c +++ b/src/io/window.c @@ -15,7 +15,8 @@ static struct GLFWwindow *win = NULL; /* Initialises the GLFW window with some defaults, * then proceed to activate OpenGL on it. */ -int window_init(void) { +int window_init(void) +{ // initialise the window #ifndef NDEBUG glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); @@ -32,10 +33,12 @@ int window_init(void) { /* NOTE: on my system; x86_64, GTX 1650 580.82.09-2, X11, i3, this causes one direct, 2 indirect memory leaks. * This is not my fault, and can safely be ignored. */ win = glfwCreateWindow(640, 480, "MCA-Selector lite", NULL, NULL); - if (!win) return 1; + if (!win) + return 1; glfwMakeContextCurrent(win); - if (!gladLoadGL(glfwGetProcAddress)) return 1; + if (!gladLoadGL(glfwGetProcAddress)) + return 1; glfwSwapInterval(1); // wait 1 screen update for a redraw a.k.a. "vsync". (not really applicable in this case but eh) glfwSetKeyCallback(win, input_callback); @@ -53,7 +56,8 @@ int window_init(void) { return 0; } -void window_loop(void) { +void window_loop(void) +{ assert(win); render_init(); while (!glfwWindowShouldClose(win)) { @@ -65,12 +69,14 @@ void window_loop(void) { } } -void window_close(void) { +void window_close(void) +{ assert(win); glfwSetWindowShouldClose(win, 1); } -void window_free(void) { +void window_free(void) +{ if (!win) { debug("window has already been freed."); return; diff --git a/src/main.c b/src/main.c index c1d5cee..67f28ca 100644 --- a/src/main.c +++ b/src/main.c @@ -9,11 +9,13 @@ #include "util/error.h" /* reroutes GLFW errors to our logging system. */ -static void error_callback(int err, const char *const msg) { +static void error_callback(int err, const char *const msg) +{ error("glfw returned (%i); \"%s\"", err, msg); } -static void quit(void) { +static void quit(void) +{ window_free(); /* terminates GLFW; destroying any @@ -23,7 +25,8 @@ static void quit(void) { /* Entry-point of the application. */ -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ (void)argc, (void)argv; printf("debug: [DBG], info: [INF], warning: [WAR], error: [ERR], fatal: [FAT]\n"); atexit(quit); diff --git a/src/util/compat/io.h b/src/util/compat/io.h index 86080d7..0d52f2c 100644 --- a/src/util/compat/io.h +++ b/src/util/compat/io.h @@ -33,14 +33,16 @@ static inline int faccess(const char *restrict fname, int perms); #define PATH_SEP '/' // contains the path separator as a character. Yes it is extremely annoying that this has to exist. #define PATH_SEP_STR "/" // contains the path separator as a string, useful for concatenation. Yes it is extremely annoying that this has to exist. -int faccess(char const *restrict fname, int perms) { +int faccess(char const *restrict fname, int perms) +{ return access(fname, perms); } #elif defined(_WIN32) #define PATH_SEP '\\' // contains the path separator as a character. Yes it is extremely annoying that this has to exist. #define PATH_SEP_STR "\\" // contains the path separator as a string, useful for concatenation. Yes it is extremely annoying that this has to exist. -int faccess(char const *restrict fname, int perms) { +int faccess(char const *restrict fname, int perms) +{ return _access(fname, perms); } #else diff --git a/src/util/conf.c b/src/util/conf.c index 7dd5f9c..b8a2370 100644 --- a/src/util/conf.c +++ b/src/util/conf.c @@ -14,7 +14,8 @@ #include "../util/error.h" #include "atrb.h" -int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, usize len) { +int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, usize len) +{ bool feq = false; // whether we've found the equal sign // data traversal @@ -32,7 +33,8 @@ int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict v brk = true; break; } - if (brk) break; + if (brk) + break; // everything after `=` is interpreted as a value if (!feq && buf[i] == '=') { @@ -53,7 +55,8 @@ int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict v return (pos == kout) ? CONF_ENODAT : (!feq ? CONF_ESYNTAX : 0); } -struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc, const char *restrict key) { +struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc, const char *restrict key) +{ // find a match for the current key usize i = 0; for (; i < optc; i++) { @@ -63,7 +66,8 @@ struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc return NULL; } -int conf_procval(struct conf_entry const *opt, const char *restrict val) { +int conf_procval(struct conf_entry const *opt, const char *restrict val) +{ // parse the data errno = 0; char *end; @@ -128,26 +132,30 @@ int conf_procval(struct conf_entry const *opt, const char *restrict val) { /* utility function for conf_getpat to concatenate 3 strings, where we already know the size */ NONNULL((1, 3)) -static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2, const char *restrict s3, usize s1len, usize s2len, usize s3len) { +static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2, const char *restrict s3, usize s1len, usize s2len, usize s3len) +{ assert(s2 || (!s2 && !s2len)); // ensuring the programmer passes both s2 and s2len as 0, if they intend to char *buf, *ptr; // allocate enough data for all three to the buffer ptr = malloc(s1len + s2len + s3len + 1); - if (!ptr) return NULL; + if (!ptr) + return NULL; buf = ptr; // store the head pointer into buf // copy data to the buffer - ptr = memcpy(ptr, s1, s1len) + s1len; // copy s1 data to the buffer - if (s2len) ptr = memcpy(ptr, s2, s2len) + s2len; // copy s2 data to the buffer (excluding null-terminator) - (void)strcpy(ptr, s3); // copy s3 as a string, thus including null-terminator + ptr = memcpy(ptr, s1, s1len) + s1len; // copy s1 data to the buffer + if (s2len) + ptr = memcpy(ptr, s2, s2len) + s2len; // copy s2 data to the buffer (excluding null-terminator) + (void)strcpy(ptr, s3); // copy s3 as a string, thus including null-terminator // return the buffer return buf; } /* appends str to the config directory string we acquire from environment variables. */ -char *conf_getpat(const char *restrict str) { +char *conf_getpat(const char *restrict str) +{ char *buf = NULL; usize len; usize str_len = strlen(str); @@ -155,21 +163,24 @@ char *conf_getpat(const char *restrict str) { buf = getenv("XDG_CONFIG_HOME"); if (!buf) { buf = getenv("HOME"); - if (!buf) return NULL; + if (!buf) + return NULL; len = strlen(buf); return conf_getpat_concat(buf, "/.config", str, len, 8, str_len); } return conf_getpat_concat(buf, NULL, str, strlen(buf), 0, str_len); #elif defined(__APPLE__) buf = getenv("HOME"); - if (!buf) return NULL; + if (!buf) + return NULL; len = strlen(buf); return conf_getpat_concat(buf, "/Library/Application Support", str, len, 28, str_len); #elif defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__) buf = getenv("APPDATA"); if (!buf) { buf = getenv("USERPROFILE"); - if (!buf) return NULL; + if (!buf) + return NULL; len = strlen(buf); return conf_getpat_concat(buf, "\\AppData\\Roaming", str, len, 16, str_len); } diff --git a/src/util/error.c b/src/util/error.c index 2002133..fb9dd89 100644 --- a/src/util/error.c +++ b/src/util/error.c @@ -7,15 +7,15 @@ #include "../types.h" -static void error_log(FILE *restrict stream, const char *restrict pfx, uint ln, const char *restrict file, const char *restrict fmt, va_list ap) { +static void error_log(FILE *restrict stream, const char *restrict pfx, uint ln, const char *restrict file, const char *restrict fmt, va_list ap) +{ fprintf(stream, "(%s:%u) [%s] '", file, ln, pfx); - vfprintf(stream, fmt, ap); - fprintf(stream, "'\n"); } -void error_debug(uint ln, const char *restrict file, const char *restrict fmt, ...) { +void error_debug(uint ln, const char *restrict file, const char *restrict fmt, ...) +{ #ifndef NDEBUG #else char *env = getenv("DEBUG"); @@ -28,28 +28,32 @@ void error_debug(uint ln, const char *restrict file, const char *restrict fmt, . va_end(ap); } -void error_info(uint ln, const char *restrict file, const char *restrict fmt, ...) { +void error_info(uint ln, const char *restrict file, const char *restrict fmt, ...) +{ va_list ap; va_start(ap, fmt); error_log(stdout, "\033[94mINF\033[0m", ln, file, fmt, ap); va_end(ap); } -void error_warn(uint ln, const char *restrict file, const char *restrict fmt, ...) { +void error_warn(uint ln, const char *restrict file, const char *restrict fmt, ...) +{ va_list ap; va_start(ap, fmt); error_log(stdout, "\033[93mWAR\033[0m", ln, file, fmt, ap); va_end(ap); } -void error_error(uint ln, const char *restrict file, const char *restrict fmt, ...) { +void error_error(uint ln, const char *restrict file, const char *restrict fmt, ...) +{ va_list ap; va_start(ap, fmt); error_log(stdout, "\033[91mERR\033[0m", ln, file, fmt, ap); va_end(ap); } -void error_fatal(uint ln, const char *restrict file, const char *restrict fmt, ...) { +void error_fatal(uint ln, const char *restrict file, const char *restrict fmt, ...) +{ va_list ap; va_start(ap, fmt); error_log(stdout, "\033[101mFAT\033[0m", ln, file, fmt, ap); diff --git a/test/main.c b/test/main.c index 9ea162c..a8c4738 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,8 @@ #include "test.h" #include "test_conf.h" -int main(void) { +int main(void) +{ assert_true(-3 >> 5 == -1); // checking for arithmetic shift, rather than logical shift assert_true(sizeof(u8) == 1); assert_true(sizeof(u16) == 2); diff --git a/test/test.c b/test/test.c index 36ed5fa..4cf7e5b 100644 --- a/test/test.c +++ b/test/test.c @@ -7,7 +7,8 @@ uint test_okay = 0; uint test_fail = 0; -int test_process(int res, const char *restrict file, uint ln, const char *restrict function, const char *restrict expression) { +int test_process(int res, const char *restrict file, uint ln, const char *restrict function, const char *restrict expression) +{ const char *status = res ? "[\033[32;1m OK \033[0m]" : "[\033[31;1m FAIL \033[0m]"; diff --git a/test/test_conf.c b/test/test_conf.c index 0ba6935..48867ef 100644 --- a/test/test_conf.c +++ b/test/test_conf.c @@ -9,7 +9,8 @@ #include "../src/util/conf.h" #include "test.h" -void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key, const char *restrict expect_val, int expect_return) { +void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key, const char *restrict expect_val, int expect_return) +{ usize len = strlen(buf) + 1; char k[len], v[len]; *k = '\0', *v = '\0'; @@ -18,20 +19,23 @@ void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key assert_true(!strcmp(v, expect_val))); } -void test_conf_matchopt(struct conf_entry *opts, usize optc, const char *restrict key, int expect_index) { +void test_conf_matchopt(struct conf_entry *opts, usize optc, const char *restrict key, int expect_index) +{ usize idx = opts - conf_matchopt(opts, optc, key); idx = (ssize)idx < 0 ? -idx : idx; int i = idx < optc ? (int)idx : -1; assert_true(i == expect_index); } -void test_conf_procval_int(const char *val, u64 expect_value, int type) { +void test_conf_procval_int(const char *val, u64 expect_value, int type) +{ u8 out[sizeof(u64)] = {0}; assert_true(!conf_procval(&(struct conf_entry){NULL, out, type}, val)); assert_true(memcmp(out, &expect_value, sizeof(u64)) == 0); } -void test_conf_procval_f32(const char *val, f32 expect_value) { +void test_conf_procval_f32(const char *val, f32 expect_value) +{ u8 out[4]; f32 result; conf_procval(&(struct conf_entry){NULL, out, CONF_F32}, val); @@ -39,40 +43,46 @@ void test_conf_procval_f32(const char *val, f32 expect_value) { assert_true(fabsf(expect_value - result) < 1e-9f); } -void test_procval_str(void) { +void test_procval_str(void) +{ char *out = NULL; (void)(assert_true(!conf_procval(&(struct conf_entry){NULL, (void *)&out, CONF_STR}, "here comes the sowon")) && assert_false(strcmp("here comes the sowon", out))); free(out); } -void test_procval_str_predef(void) { +void test_procval_str_predef(void) +{ char *out = strdup("owo"); (void)(assert_true(!conf_procval(&(struct conf_entry){NULL, (void *)&out, CONF_STR}, "i leak if I don't free")) && assert_true(!strcmp("i leak if I don't free", out))); free(out); } -void test_procval_fstr(void) { +void test_procval_fstr(void) +{ char buf[16]; struct conf_fstr str = {sizeof(buf), buf}; (void)(assert_true(!conf_procval(&(struct conf_entry){NULL, &str, CONF_FSTR}, "hewwoo wowld")) && assert_true(!strcmp(str.out, "hewwoo wowld"))); } -void test_procval_fstr_trunc(void) { +void test_procval_fstr_trunc(void) +{ char buf[8]; struct conf_fstr str = {sizeof(buf), buf}; (void)(assert_true(!conf_procval(&(struct conf_entry){NULL, &str, CONF_FSTR}, "hewwooo wowld")) && assert_true(!strcmp(str.out, "hewwooo"))); } -void test_procval_eparse(void) { +void test_procval_eparse(void) +{ i32 out; assert_true(conf_procval(&(struct conf_entry){NULL, &out, CONF_I32}, "owo") == CONF_EPARSE); } -void test_conf_getpat(void) { +void test_conf_getpat(void) +{ char *path; #if defined(__linux__) /* test without setting environment variables. */