Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tadd committed Oct 28, 2024
1 parent e768734 commit ef3cd70
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}

Expand Down

0 comments on commit ef3cd70

Please sign in to comment.