Skip to content

Commit

Permalink
refactor: include std types such as int and string
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed Apr 14, 2024
1 parent 787f4ad commit 42218e3
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 79 deletions.
22 changes: 11 additions & 11 deletions tests/impl/int.h → include/wheel/std/int.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <stdlib.h>
#include <inttypes.h>

#define LIBWHEEL_TYPE uint64_t
#define LIBWHEEL_TYPE int
#define LIBWHEEL_ALIAS int

#define LIBWHEEL_TRAIT_COMPARE
int64_t int_trait_compare(const uint64_t a, const uint64_t b) {
int64_t int_trait_compare(const int a, const int b) {
if (a == b) {
return 0;
} else if (a > b) {
Expand All @@ -21,41 +21,41 @@ int64_t int_trait_compare(const uint64_t a, const uint64_t b) {
}

#define LIBWHEEL_TRAIT_SHALLOW_COPY
uint64_t int_trait_shallow_copy(const uint64_t t) {
int int_trait_shallow_copy(const int t) {
return t;
}

#define LIBWHEEL_TRAIT_DEEP_COPY
uint64_t int_trait_deep_copy(const uint64_t t) {
int int_trait_deep_copy(const int t) {
return t;
}

#define LIBWHEEL_TRAIT_HASHABLE
uint64_t int_trait_hash(const uint64_t t) {
uint64_t int_trait_hash(const int t) {
return t;
}

#define LIBWHEEL_TRAIT_STRINGIFY
uint64_t int_trait_stringify(char* target, const uint64_t t) {
const int size = snprintf(NULL, 0, "%" PRIu64, t);
uint64_t int_trait_stringify(char* target, const int t) {
const int size = snprintf(NULL, 0, "%d", t);

if (target == NULL) {
return size;
}

return snprintf(target, size + 1, "%" PRIu64, t);
return snprintf(target, size + 1, "%d", t);
}

#define LIBWHEEL_TRAIT_SERIALIZE_JSON
uint64_t int_trait_serialize_json(char* target, const uint64_t t) {
uint64_t int_trait_serialize_json(char* target, const int t) {
return int_trait_stringify(target, t);
}

#define LIBWHEEL_TRAIT_DESTROY
void int_trait_destroy(const uint64_t t) {
void int_trait_destroy(const int t) {
// Do nothing.
}

#include <wheel.h>

#endif // LIBWHEEL_INT_H
#endif // LIBWHEEL_INT_H
2 changes: 1 addition & 1 deletion tests/impl/string.h → include/wheel/std/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ void string_trait_destroy(char* value) {
free(value);
}

#include <wheel.h>
#include "wheel.h"

#endif // LIBWHEEL_STRING_H
3 changes: 1 addition & 2 deletions tests/bst/bst_int_insert.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <assert.h>

#include "../impl/int.h"
#include <wheel/std/int.h>

int main() {
bst_int* i = bst_int_init();
Expand Down
2 changes: 1 addition & 1 deletion tests/bst/bst_string_search.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../impl/string.h"
#include <assert.h>
#include <wheel/std/string.h>

int main() {
bst_string* s = bst_string_init();
Expand Down
39 changes: 0 additions & 39 deletions tests/eht/eht_int_search.c

This file was deleted.

2 changes: 1 addition & 1 deletion tests/impl/vec_int.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef LIBWHEEL_VEC_INT_H
#define LIBWHEEL_VEC_INT_H

#include "int.h"
#include <wheel/std/int.h>

#define LIBWHEEL_TYPE vec_int
#define LIBWHEEL_ALIAS vec_int
Expand Down
2 changes: 1 addition & 1 deletion tests/linked_list/ll_int_append.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../impl/int.h"
#include <wheel/std/int.h>

int main() {
ll_int l = ll_int_init();
Expand Down
2 changes: 1 addition & 1 deletion tests/linked_list/ll_int_prepend.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../impl/int.h"
#include <wheel/std/int.h>

int main() {
ll_int l = ll_int_init();
Expand Down
2 changes: 1 addition & 1 deletion tests/linked_list/ll_set.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../impl/int.h"
#include <wheel/std/int.h>

int main() {
ll_int l = ll_int_init();
Expand Down
2 changes: 1 addition & 1 deletion tests/rc/rc_string_copy.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "../impl/string.h"
#include <wheel/std/string.h>

int main() {
char* hello = strdup("Hello, world!");
Expand Down
2 changes: 1 addition & 1 deletion tests/stack/stack_int_push.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Copyright (c) 2023.
*/

#include "../impl/int.h"
#include <assert.h>
#include <wheel/std/int.h>

int main() {
stack_int s = stack_int_init();
Expand Down
10 changes: 5 additions & 5 deletions tests/trie/trie_int_search.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "../impl/int.h"
#include <assert.h>
#include <wheel/std/int.h>

int main() {
trie_int* trie = trie_int_init();

uint64_t a = 10000000000;
uint64_t b = 10000000001;
uint64_t c = 10000000002;
uint64_t d = 10000000003;
int a = 100000000;
int b = 100000001;
int c = 100000002;
int d = 100000003;

assert(!trie_int_add(trie, "10.0.0.0", a).present);
assert(!trie_int_add(trie, "10.0.0.1", b).present);
Expand Down
2 changes: 1 addition & 1 deletion tests/vec/vec_int.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../impl/int.h"
#include <assert.h>
#include <wheel/std/int.h>

int main(int argc, char** argv) {
vec_int v = vec_int_with_cap(5);
Expand Down
4 changes: 2 additions & 2 deletions tests/vec/vec_int_filter.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../impl/int.h"
#include <wheel/std/int.h>

bool is_even(uint64_t i) {
bool is_even(int i) {
return i % 2 == 0;
}

Expand Down
3 changes: 1 addition & 2 deletions tests/vec/vec_int_get.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "../impl/vec_int.h"

#include <assert.h>
#include <wheel/std/int.h>

int main() {
vec_int v = vec_int_with_cap(8);
Expand Down
12 changes: 6 additions & 6 deletions tests/vec/vec_int_grow.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
#include "../impl/int.h"
#include <wheel/std/int.h>

int main() {
vec_int v = vec_int_init();
uint64_t initial_size = v.size;

for (uint64_t i = 0; i < initial_size; ++i) {
for (int i = 0; i < initial_size; ++i) {
vec_int_set(&v, i, i);
}

assert(v.size == initial_size);

for (uint64_t i = initial_size; i < initial_size * 2; ++i) {
for (int i = (int) initial_size; i < initial_size * 2; ++i) {
vec_int_set(&v, i, i);
}

assert(v.size == initial_size * 2);

for (uint64_t i = initial_size * 2; i < initial_size * 3; ++i) {
for (int i = (int) initial_size * 2; i < initial_size * 3; ++i) {
vec_int_set(&v, i, i);
}

assert(v.size == initial_size * 4);

for (uint64_t i = 0; i < initial_size * 3; ++i) {
for (int i = 0; i < initial_size * 3; ++i) {
optional_int result = vec_int_get(&v, i);
assert(result.present);
assert(result.value == i);
}

for (uint64_t i = initial_size * 3; i < initial_size * 4; ++i) {
for (int i = (int) initial_size * 3; i < initial_size * 4; ++i) {
optional_int result = vec_int_get(&v, i);
assert(!result.present);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/vec/vec_int_map.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "../impl/int.h"
#include <wheel/std/int.h>

/**
* Rocket science.
*/
uint64_t mul2(uint64_t i) {
int mul2(int i) {
return 2 * i;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/vec/vec_int_serialize_json.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../impl/int.h"
#include <assert.h>
#include <wheel/std/int.h>

void test_simple() {
// Create vector [0, 1, 2, 3, 4].
Expand Down

0 comments on commit 42218e3

Please sign in to comment.