From ef3cd702350a2e29d5aa8c2dc82261dc240f1ab6 Mon Sep 17 00:00:00 2001 From: Tadashi Saito <tad.a.digger@gmail.com> Date: Mon, 28 Oct 2024 23:48:53 +0900 Subject: [PATCH] WIP --- README.md | 24 ++++++++++++++++++++---- testlib.c | 6 ++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 16698ff..831e9ad 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,29 @@ printf("new length: %zu\n", scary_length(a)); //=> 2 You can push elements with automatic memory extension, as much as you want. +Moreover, the `scary_push` function is _generic_ 😱, so this code + +```c +scary_push(&a, 0UL); // Pushing `unsigned long` into an array of `int`! +``` + +can produce a warning by default with modern compilers like GCC 12. + +<pre><code><b>warning:</b> passing argument 1 of 'scary_push_uint64' from incompatible pointer type [<b>-Wincompatible-pointer-types</b>] + scary_push(<b>&a</b>, 0UL); + <b>^~</b> + <b>|</b> + <b>int **</b> +</code></pre> + +You can of course opt-in an option `-Werror` to prevent such typing mistakes. + And you'll see **magic** here: ```c int i = a[1]; ``` -You can read/write them as an ordinary C array without any overhead 😱😱. +You can read/write them as ordinary C arrays with **zero**-overhead 😱😱. ```c printf("content: %d\n", i); @@ -38,9 +55,8 @@ Then it prints `42`. Happy ending. 🤔🤔 We use [Criterion](https://github.com/Snaipe/Criterion) for tests so you'll need to install that before `make test`. -Dear Debian/Ubuntu users: You can install [its -package](https://packages.debian.org/bookworm/libcriterion-dev) via -`apt install libcriterion-dev`. +Dear Debian/Ubuntu users: You can install its package via +<code>apt install [libcriterion-dev](https://packages.debian.org/stable/libcriterion-dev)</code>. ## License diff --git a/testlib.c b/testlib.c index b135455..fd9a909 100644 --- a/testlib.c +++ b/testlib.c @@ -22,7 +22,9 @@ Test(libscary, push_and_length) { Test(libscary, push) { int *a = scary_new(sizeof(int)); scary_push(&a, 42); + scary_push(&a, 0UL); cr_expect(eq(sz, 1, scary_length(a))); + scary_free(a); char *b = scary_new(sizeof(char)); @@ -32,6 +34,10 @@ Test(libscary, push) { cr_expect(eq(sz, 2, scary_length(b))); cr_expect(eq(chr, 'a', b[0])); cr_expect(eq(chr, 'b', b[1])); + + + + scary_free(b); }