Skip to content

Commit

Permalink
Add scary_dup.
Browse files Browse the repository at this point in the history
  • Loading branch information
tadd committed Sep 21, 2024
1 parent 01baa64 commit 6a484fa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions scary.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,12 @@ void scary_pop(void *p)
Scary *ary = get(p);
ary->length--; // do not shrink for speed
}

void *scary_dup(void *p)
{
Scary *ary = get(p);
Scary *dup = xmalloc(sizeof(Scary) + ary->capacity);
*dup = *ary;
memcpy(dup->space, ary->space, ary->capacity);
return opaque(dup);
}
1 change: 1 addition & 0 deletions scary.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ void scary_push_uint32(void *, uint32_t);
void scary_push_uint64(void *, uint64_t);
void scary_push_ptr(void *, const void *);
void scary_pop(void *ary);
void *scary_dup(void *ary);

#endif
13 changes: 13 additions & 0 deletions testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ Test(libscary, pop) {
cr_expect(eq(sz, 1, scary_length(a)));
scary_free(a);
}

Test(libscary, dup) {
int *a = scary_new(sizeof(int));
scary_push(&a, 42);
scary_push(&a, -42);
int *b = scary_dup(a);
cr_expect(ne(ptr, a, b));
cr_expect(eq(sz, scary_length(a), scary_length(b)));
cr_expect(eq(sz, 2, scary_length(b)));
cr_expect(eq(int[2], a, b));
scary_free(b);
scary_free(a);
}

0 comments on commit 6a484fa

Please sign in to comment.