Browse Source

change interface

Mistivia 3 months ago
parent
commit
43beeac38c
21 changed files with 322 additions and 335 deletions
  1. 26 29
      README.md
  2. 27 0
      src/basic_types.c
  3. 19 0
      src/basic_types.h
  4. 4 3
      src/dymc.h
  5. 0 91
      src/map.c
  6. 0 33
      src/map.h
  7. 11 12
      src/rb_tree.c
  8. 11 10
      src/rb_tree.h
  9. 22 27
      src/str.c
  10. 13 10
      src/str.h
  11. 50 0
      src/tree_map.c
  12. 21 0
      src/tree_map.h
  13. 0 15
      src/vec.h
  14. 16 23
      src/vector.c
  15. 19 0
      src/vector.h
  16. 0 33
      tests/test_map.c
  17. 0 23
      tests/test_str.c
  18. 25 0
      tests/test_string.c
  19. 34 0
      tests/test_tree_map.c
  20. 0 26
      tests/test_vec.c
  21. 24 0
      tests/test_vector.c

+ 26 - 29
README.md

@@ -1,6 +1,6 @@
 # DymC
 
-Utility library for very dynamic typed programming in C with [bdwgc](https://github.com/ivmai/bdwgc).
+Programming C in Java-style with [bdwgc](https://github.com/ivmai/bdwgc).
 
 ## Dependencies
 
@@ -24,51 +24,48 @@ Run tests:
 
 ### Vector
 
-    #include <dymc.h>
     #include <stdio.h>
 
-    int main() {
-        void *v = new_vec();
-
-        int *p;
+    #include "dymc.h"
 
-        p= malloc(sizeof(int));
-        *p = 100;
-        vec_push_back(v, p);
+    int main() {
+        Vector v = new_Vector();
+        
+        Integer elem = new_Integer(100);
+        Vector_pushBack(v, elem);
 
-        p = malloc(sizeof(int));
-        *p = 101;
-        vec_push_back(v, p);
+        elem = new_Integer(101);
+        Vector_pushBack(v, elem);
 
-        for (int i = 0; i < vec_size(v); i++) {
-            printf("%d\n", *(int*)vec_get(v, i));
+        for (int i = 0; i < Vector_size(v); i++) {
+            printf("%d\n", *(Integer)Vector_get(v, i));
         }
 
         return 0;
     }
 
-### Map/Dict
+### TreeMap
 
-    #include <dymc.h>
     #include <stdio.h>
+    #include <string.h>
 
-    int main() {
-        void *d = new_dict();
-        int *p;
+    #include "dymc.h"
 
-        p = malloc(sizeof(int));
-        *p = 1;
-        dict_set(d, "a", p);
+    int main() {
+        TreeMap dict = new_TreeMap((CompareFunc)strcmp);
 
-        p = malloc(sizeof(int));
-        *p = 2;
-        dict_set(d, "b", p);
+        TreeMap_set(dict, "a", new_Integer(1));
+        TreeMap_set(dict, "b", new_Integer(2));
 
-        printf("%d\n", *(int*)dict_get(d, "a"));
+        printf("%d\n", *(Integer)TreeMap_get(dict, "a"));
 
-        void *iter;
-        for (iter = dict_begin(d); iter != NULL; iter = dict_next(d, iter)) {
-            printf("key: %s, value: %d\n", dict_iter_key(iter), *(int*)dict_iter_value(iter));
+        TreeMapIter iter;
+        for (iter = TreeMap_begin(dict);
+                iter != NULL;
+                iter = TreeMap_next(dict, iter)) {
+            printf("key: %s, value: %d\n",
+                    TreeMapIter_getKey(iter),
+                    *(Integer)TreeMapIter_getValue(iter));
         }
 
         return 0;

+ 27 - 0
src/basic_types.c

@@ -0,0 +1,27 @@
+#include "basic_types.h"
+
+#include "gc_prelude.h"
+
+Integer new_Integer(int32_t val) {
+    Integer ret = malloc(sizeof(val));
+    *ret = val;
+    return ret;
+}
+
+Long new_Long(int64_t val) {
+    Long ret = malloc(sizeof(val));
+    *ret = val;
+    return ret;
+}
+
+Float new_Float(float val) {
+    Float ret = malloc(sizeof(val));
+    *ret = val;
+    return ret;
+}
+
+Double new_Double(double val) {
+    Double ret = malloc(sizeof(val));
+    *ret = val;
+    return ret;
+}

+ 19 - 0
src/basic_types.h

@@ -0,0 +1,19 @@
+#ifndef DYMC_BASIC_TYPE_H_
+#define DYMC_BASIC_TYPE_H_
+
+#include <stdint.h>
+
+typedef int32_t * Integer;
+typedef int64_t * Long;
+typedef float * Float;
+typedef double * Double;
+typedef const char* String;
+typedef char* ByteArray;
+typedef void* Object;
+
+Integer new_Integer(int32_t val);
+Long new_Long(int64_t val);
+Float new_Float(float val);
+Double new_Double(double val);
+
+#endif

+ 4 - 3
src/dymc.h

@@ -3,9 +3,10 @@
 
 #include <stdlib.h>
 #include <gc_prelude.h>
-#include <vec.h>
-#include <map.h>
-#include <str.h>
+
+#include "basic_types.h"
+#include "vector.h"
+#include "tree_map.h"
 
 #endif
 

+ 0 - 91
src/map.c

@@ -1,91 +0,0 @@
-#include "map.h"
-#include "rb_tree.h"
-
-#include <string.h>
-
-#include "gc_prelude.h"
-
-typedef int (*rb_cmp_t)(void *, void *);
-
-void* new_map(cmp_t compare) {
-    rb_tree_t *tree = malloc(sizeof(rb_tree_t));
-    *tree = (rb_tree_t){NULL, (rb_cmp_t)compare, NULL};
-    return tree;
-}
-
-void map_set(void* self, void* key, void* value) {
-    node_entry_t *iter = rb_tree_find(self, &key);
-    if (iter == NULL) {
-        iter = malloc(sizeof(*iter));
-        iter->key = key;
-        iter->value = value;
-        rb_tree_insert(self, iter);
-    } else {
-        iter->value = value;
-    }
-}
-
-void* map_get(void* self, void* key) {
-    node_entry_t *iter = rb_tree_find(self, &key);
-    if (iter == NULL) return NULL;
-    return iter->value;
-}
-
-void map_erase(void* self, void* key) {
-    rb_tree_remove(self, key);
-}
-
-void* map_begin(void *self) {
-    return rb_tree_min(self);
-}
-
-void* map_next(void *self, void *iter) {
-    return rb_tree_next(self, iter);
-}
-
-void* map_iter_key(void* iter_) {
-    node_entry_t *iter = iter_;
-    return iter->key;
-}
-
-void* map_iter_value(void* iter_) {
-    node_entry_t *iter = iter_;
-    return iter->value;
-}
-
-static int dict_cmp(void **a, void** b) {
-    return strcmp(*a, *b);
-}
-void* new_dict() {
-    return new_map(dict_cmp);
-}
-
-void dict_set(void* self, const char *key, void* value) {
-    map_set(self, (void*)key, value);
-}
-
-void* dict_get(void* self, const char* key) {
-    return map_get(self, (void*)key);
-}
-
-void dict_erase(void* self, const char* key) {
-    map_erase(self, (void*)key);
-}
-
-void* dict_begin(void *self) {
-    return map_begin(self);
-}
-
-void* dict_next(void *self, void *iter) {
-    return map_next(self, iter);
-}
-
-const char* dict_iter_key(void* iter) {
-    return map_iter_key(iter);
-}
-
-void* dict_iter_value(void* iter) {
-    return map_iter_value(iter);
-}
-
-

+ 0 - 33
src/map.h

@@ -1,33 +0,0 @@
-#ifndef DYMC_MAP_H_
-#define DYMC_MAP_H_
-
-#include "rb_tree.h"
-
-typedef struct {
-    rb_node_t node;
-    void *key;
-    void *value;
-} node_entry_t;
-typedef int (*cmp_t)(void** a, void** b);
-
-void* new_map(cmp_t compare);
-void map_set(void* self, void* key, void* value);
-void* map_get(void* self, void* key);
-void map_erase(void* self, void* key);
-
-void* map_begin(void *self);
-void* map_next(void *self, void *iter);
-void* map_iter_key(void* iter);
-void* map_iter_value(void* iter);
-
-void* new_dict();
-void dict_set(void* self, const char *key, void* value);
-void* dict_get(void* self, const char* key);
-void dict_erase(void* self, const char* key);
-
-void* dict_begin(void *self);
-void* dict_next(void *self, void *iter);
-const char* dict_iter_key(void* iter);
-void* dict_iter_value(void* iter);
-
-#endif

+ 11 - 12
src/rb_tree.c

@@ -29,20 +29,20 @@
 #define BLACK 0
 
 static rb_node_t *rb_tree_minmax(rb_tree_t *, int);
-void *rb_tree_min(rb_tree_t *head) { return rb_tree_minmax(head, -1); }
-void *rb_tree_max(rb_tree_t *head) { return rb_tree_minmax(head, 1); }
+rb_node_t *rb_tree_min(rb_tree_t *head) { return rb_tree_minmax(head, -1); }
+rb_node_t *rb_tree_max(rb_tree_t *head) { return rb_tree_minmax(head, 1); }
 
-void *rb_tree_left(void *node) {
+rb_node_t *rb_tree_left(rb_node_t *node) {
     rb_node_t *elm = node;
     if (node == NULL) return NULL;
     return elm->entry.rbe_left;
 }
-void *rb_tree_right(void *node) {
+rb_node_t *rb_tree_right(rb_node_t *node) {
     rb_node_t *elm = node;
     if (node == NULL) return NULL;
     return elm->entry.rbe_right;
 }
-void *rb_tree_parent(void *node) {
+rb_node_t *rb_tree_parent(rb_node_t *node) {
     rb_node_t *elm = node;
     if (node == NULL) return NULL;
     return elm->entry.rbe_parent;
@@ -222,7 +222,7 @@ static void rb_tree_remove_color(rb_tree_t *head, rb_node_t *parent,
     if (elm) elm->entry.rbe_color = BLACK;
 }
 
-void rb_tree_remove(rb_tree_t *head, void *elmv) {
+void rb_tree_remove(rb_tree_t *head, rb_node_t *elmv) {
     rb_node_t *elm = elmv;
     rb_node_t *child, *parent;
     int color;
@@ -289,7 +289,7 @@ color:
     if (color == 0) rb_tree_remove_color(head, parent, child);
 }
 
-void *rb_tree_insert(rb_tree_t *head, void *elmv) {
+rb_node_t *rb_tree_insert(rb_tree_t *head, rb_node_t *elmv) {
     rb_node_t *elm = elmv;
     rb_node_t *tmp;
     rb_node_t *parent = NULL;
@@ -297,7 +297,7 @@ void *rb_tree_insert(rb_tree_t *head, void *elmv) {
     tmp = head->rbh_root;
     while (tmp) {
         parent = tmp;
-        comp = head->cmp((void *)elm->content, (void *)parent->content);
+        comp = head->cmp(elm->key, parent->key);
         if (comp < 0)
             tmp = tmp->entry.rbe_left;
         else if (comp > 0)
@@ -325,11 +325,11 @@ void *rb_tree_insert(rb_tree_t *head, void *elmv) {
     return (NULL);
 }
 
-void *rb_tree_find(rb_tree_t *head, void *key) {
+rb_node_t *rb_tree_find(rb_tree_t *head, void *key) {
     rb_node_t *tmp = head->rbh_root;
     int comp;
     while (tmp) {
-        comp = head->cmp(key, (void *)tmp->content);
+        comp = head->cmp(key, tmp->key);
         if (comp < 0)
             tmp = tmp->entry.rbe_left;
         else if (comp > 0)
@@ -340,7 +340,7 @@ void *rb_tree_find(rb_tree_t *head, void *key) {
     return (NULL);
 }
 
-void *rb_tree_next(rb_tree_t *head, void *elmv) {
+rb_node_t *rb_tree_next(rb_tree_t *head, rb_node_t *elmv) {
     rb_node_t *elm = elmv;
     if (elm->entry.rbe_right) {
         elm = elm->entry.rbe_right;
@@ -372,4 +372,3 @@ static rb_node_t *rb_tree_minmax(rb_tree_t *head, int val) {
     }
     return parent;
 };
-

+ 11 - 10
src/rb_tree.h

@@ -35,7 +35,8 @@ struct rb_node {
         struct rb_node *rbe_parent;
         int rbe_color;
     } entry;
-    char content[0];
+    void *key;
+    void *value;
 };
 typedef struct rb_node rb_node_t;
 
@@ -46,16 +47,16 @@ struct rb_tree {
 };
 typedef struct rb_tree rb_tree_t;
 
-void rb_tree_remove(rb_tree_t *, void *iter);
+void rb_tree_remove(rb_tree_t *, rb_node_t *iter);
 
 // return a iterator
-void *rb_tree_insert(rb_tree_t *, void *treenode);
-void *rb_tree_find(rb_tree_t *, void *val);
-void *rb_tree_next(rb_tree_t *, void *iter);
-void *rb_tree_min(rb_tree_t *);
-void *rb_tree_max(rb_tree_t *);
-void *rb_tree_left(void *node);
-void *rb_tree_right(void *node);
-void *rb_tree_parent(void *node);
+rb_node_t* rb_tree_insert(rb_tree_t *, rb_node_t *treenode);
+rb_node_t* rb_tree_find(rb_tree_t *, void* key);
+rb_node_t* rb_tree_next(rb_tree_t *, rb_node_t *iter);
+rb_node_t* rb_tree_min(rb_tree_t *);
+rb_node_t* rb_tree_max(rb_tree_t *);
+rb_node_t* rb_tree_left(rb_node_t *node);
+rb_node_t* rb_tree_right(rb_node_t *node);
+rb_node_t* rb_tree_parent(rb_node_t *node);
 
 #endif

+ 22 - 27
src/str.c

@@ -9,10 +9,10 @@
 #include <string.h>
 
 #include "gc_prelude.h"
-#include "vec.h"
+#include "vector.h"
 
-void *str_split(const char *str, char delim) {
-    void* ret = new_vec();
+void* str_split(String str, char delim) {
+    void* ret = new_Vector();
 
     if (str == NULL) return NULL;
     if (*str == '\n') {
@@ -46,12 +46,12 @@ void *str_split(const char *str, char delim) {
         buf[size] = '\0';
         memcpy(buf, begin, size * sizeof(char));
         begin = p + 1;
-        vec_push_back(ret, buf);
+        Vector_pushBack(ret, buf);
     }
     return ret;
 }
 
-char *str_strip(const char *str) {
+String str_strip(String str) {
     if (str == NULL) return NULL;
     int len = strlen(str);
     const char *begin = str;
@@ -69,21 +69,20 @@ char *str_strip(const char *str) {
     return buf;
 }
 
-typedef struct {
+struct string_builder {
     size_t size;
     size_t cap;
     char *buf;
-} str_builder_t;
+};
 
-// string stream
-void* new_ss() {
-    str_builder_t *self = malloc(sizeof(str_builder_t));
-    *self = (str_builder_t){.size = 0, .cap = 16};
+StringBuilder new_StringBuilder() {
+    StringBuilder self = malloc(sizeof(struct string_builder));
+    *self = (struct string_builder){.size = 0, .cap = 16};
     self->buf = malloc(sizeof(char) * 17);
     return self;
 }
 
-static void ss_reserve(str_builder_t *self, int extra) {
+static void StringBuilder_reserve(StringBuilder self, int extra) {
     if (self->size + extra <= self->cap) {
         return;
     }
@@ -93,42 +92,38 @@ static void ss_reserve(str_builder_t *self, int extra) {
     self->cap = new_cap;
 }
 
-void ss_add(void *self_, char *format, ...) {
-    str_builder_t *self = self_;
+void StringBuilder_append(StringBuilder self, String format, ...) {
     va_list va1;
     va_list va2;
     va_start(va1, format);
     va_copy(va2, va1);
     int size = vsnprintf(NULL, 0, format, va1);
-    ss_reserve(self, size);
+    StringBuilder_reserve(self, size);
     vsnprintf(self->buf + self->size, self->cap - self->size + 1, format, va2);
     self->size += size;
 }
 
-void ss_addc(void *self_, char c) {
-    str_builder_t *self = self_;
-    ss_reserve(self, 1);
+void StringBuilder_appendChar(StringBuilder self, char c) {
+    StringBuilder_reserve(self, 1);
     self->buf[self->size] = c;
     self->size++;
 }
 
-char *ss_cstr(void *self_) {
-    str_builder_t *self = self_;
+String StringBuilder_getString(StringBuilder self) {
     return self->buf;    
 }
 
-size_t ss_size(void *self_) {
-    str_builder_t *self = self_;
+size_t StringBuilder_size(StringBuilder self) {
     return self->size;
 }
 
-char *fgetline(FILE *fp) {
-    void *ss = new_ss();
+String fgetline(FILE *fp) {
+    StringBuilder sb = new_StringBuilder();
     while (true) {
         int c = fgetc(fp);
-        if (c == EOF && ss_size(ss) == 0) return NULL;
-        if (c != EOF) ss_addc(ss, c);
-        if (c == EOF || c == '\n') return ss_cstr(ss);
+        if (c == EOF && StringBuilder_size(sb) == 0) return NULL;
+        if (c != EOF) StringBuilder_appendChar(sb, c);
+        if (c == EOF || c == '\n') return StringBuilder_getString(sb);
     }
     return NULL;
 }

+ 13 - 10
src/str.h

@@ -4,20 +4,23 @@
 #include <stdio.h>
 #include <stddef.h>
 
-char *str_strip(const char *str);
+#include "basic_types.h"
+// #include "list.h"
 
-// return: vec of char*
-void *str_split(const char *str, char delim);
+String String_strip(String s);
+// List String_split(String str, char delim);
 
+// StringBuilder
+typedef struct string_builder* StringBuilder;
+StringBuilder new_StringBuilder();
+void StringBuilder_append(StringBuilder self, String format, ...);
+void StringBuilder_appendChar(StringBuilder self, char c);
+String StringBuilder_getString(StringBuilder self);
+size_t StringBuilder_size(StringBuilder self);
 
-// string stream
-void* new_ss();
-void ss_add(void *self, char *format, ...);
-void ss_addc(void *self, char c);
-char *ss_cstr(void *self);
-size_t ss_size(void* self);
 
-char *fgetline(FILE *fp);
+// Utilities
+String fgetline(FILE *fp);
 int fpeek(FILE *fp);
 
 #endif

+ 50 - 0
src/tree_map.c

@@ -0,0 +1,50 @@
+#include "tree_map.h"
+#include "rb_tree.h"
+
+#include <string.h>
+
+#include "gc_prelude.h"
+
+TreeMap new_TreeMap(CompareFunc cmp_fn) {
+    TreeMap tree = malloc(sizeof(rb_tree_t));
+    *tree = (rb_tree_t){NULL, cmp_fn, NULL};
+    return tree;
+}
+
+void TreeMap_set(TreeMap self, Object key, Object value) {
+    TreeMapIter iter = rb_tree_find(self, &key);
+    if (iter == NULL) {
+        iter = malloc(sizeof(*iter));
+        iter->key = key;
+        iter->value = value;
+        rb_tree_insert(self, iter);
+    } else {
+        iter->value = value;
+    }
+}
+
+Object TreeMap_get(TreeMap self, Object key) {
+    TreeMapIter iter = rb_tree_find(self, key);
+    if (iter == NULL) return NULL;
+    return iter->value;
+}
+
+void TreeMap_erase(TreeMap self, Object key) {
+    rb_tree_remove(self, key);
+}
+
+TreeMapIter TreeMap_begin(TreeMap self) {
+    return rb_tree_min(self);
+}
+
+TreeMapIter TreeMap_next(TreeMap self, TreeMapIter iter) {
+    return rb_tree_next(self, iter);
+}
+
+Object TreeMapIter_getKey(TreeMapIter iter) {
+    return iter->key;
+}
+
+Object TreeMapIter_getValue(TreeMapIter iter) {
+    return iter->value;
+}

+ 21 - 0
src/tree_map.h

@@ -0,0 +1,21 @@
+#ifndef DYMC_MAP_H_
+#define DYMC_MAP_H_
+
+#include "basic_types.h"
+
+typedef struct rb_tree *TreeMap;
+typedef struct rb_node *TreeMapIter;
+typedef int (*CompareFunc)(Object a, Object b);
+
+TreeMap new_TreeMap(CompareFunc compare);
+void TreeMap_set(TreeMap self, Object key, Object value);
+Object TreeMap_get(TreeMap self, Object key);
+void TreeMap_erase(TreeMap self, Object key);
+
+TreeMapIter TreeMap_begin(TreeMap self);
+TreeMapIter TreeMap_next(TreeMap self, TreeMapIter iter);
+
+Object TreeMapIter_getKey(TreeMapIter self);
+Object TreeMapIter_getValue(TreeMapIter self);
+
+#endif

+ 0 - 15
src/vec.h

@@ -1,15 +0,0 @@
-#ifndef DYMC_VEC_H_ 
-#define DYMC_VEC_H_ 
-
-#include <stddef.h>
-
-void *new_vec();
-void vec_push_back(void *self, void* obj);
-void* vec_get(void *self, size_t n);
-size_t vec_length(void *self);
-void vec_erase(void *self, size_t n);
-size_t vec_size(void* self);
-void vec_reserve(void* self, size_t n);
-void vec_insert(void* self, size_t pos, void* obj);
-
-#endif

+ 16 - 23
src/vec.c → src/vector.c

@@ -1,59 +1,53 @@
-#include "vec.h"
+#include "vector.h"
 
 #include <string.h>
 
 #include "gc_prelude.h"
 
-
-struct vec {
+struct vector {
     size_t length;
     size_t capacity;
     void** buf;
 };
 
-static void vec_enlarge(struct vec* self) {
-    self->buf = realloc(self->buf, self->capacity * sizeof(void*) * 2);
+static void Vector_enlarge(Vector self) {
+    self->buf = realloc(self->buf, self->capacity * sizeof(Object) * 2);
     self->capacity = self->capacity * 2;
 }
 
-void *new_vec() {
-    struct vec* vec = malloc(sizeof(struct vec));
+Vector new_Vector() {
+    Vector vec = malloc(sizeof(struct vector));
     vec->length = 0;
     vec->capacity = 16;
-    vec->buf = malloc(16 * sizeof(void*));
+    vec->buf = malloc(16 * sizeof(Object));
     return vec;
 }
 
-void vec_push_back(void *self_, void* obj) {
-    struct vec *self = self_;
-    if (self->length == self->capacity) vec_enlarge(self);
+void Vector_pushBack(Vector self, Object obj) {
+    if (self->length == self->capacity) Vector_enlarge(self);
     self->buf[self->length] = obj;
     self->length++;
 }
 
-void* vec_get(void *self_, size_t n) {
-    struct vec *self = self_;
+Object Vector_get(Vector self, size_t n) {
     if (n < 0 || n >= self->length) return NULL;
     return self->buf[n];
 }
 
 
-void vec_erase(void *self_, size_t n) {
-    struct vec *self = self_;
+void Vector_erase(Vector self, size_t n) {
     if (self->length <= n) {
         return;
     }
-    memmove(self->buf + n, self->buf + n + 1, (self->length - n - 1) * sizeof(void*));
+    memmove(self->buf + n, self->buf + n + 1, (self->length - n - 1) * sizeof(Object));
     self->length--;
 }
 
-size_t vec_size(void *self_) {
-    struct vec *self = self_;
+size_t Vector_size(Vector self) {
     return self->length;
 }
 
-void vec_reserve(void *self_, size_t n) {
-    struct vec *self = self_;
+void Vector_reserve(Vector self, size_t n) {
     if (n <= self->capacity) {
         return;
     }
@@ -61,10 +55,9 @@ void vec_reserve(void *self_, size_t n) {
     self->capacity = n;
 }
 
-void vec_insert(void *self_, size_t pos, void *obj) {
-    struct vec *self = self_;
+void Vector_insert(Vector self, size_t pos, Object obj) {
     if (self->length == self->capacity) {
-        vec_enlarge(self);
+        Vector_enlarge(self);
     }
     if (pos > self->length || pos < 0) return;
     memmove(self->buf + pos + 1, self->buf + pos, sizeof(void*) * (self->length - pos));

+ 19 - 0
src/vector.h

@@ -0,0 +1,19 @@
+#ifndef DYMC_VEC_H_ 
+#define DYMC_VEC_H_ 
+
+#include <stddef.h>
+
+#include "basic_types.h"
+
+typedef struct vector *Vector;
+
+Vector new_Vector();
+void Vector_pushBack(Vector self, Object obj);
+void* Vector_get(Vector self, size_t n);
+size_t Vector_length(Vector self);
+void Vector_erase(Vector self, size_t n);
+size_t Vector_size(Vector self);
+void Vector_reserve(Vector self, size_t n);
+void Vector_insert(Vector self, size_t pos, Object obj);
+
+#endif

+ 0 - 33
tests/test_map.c

@@ -1,33 +0,0 @@
-#include "map.h"
-
-#include <assert.h>
-#include <stdio.h>
-
-#include "gc_prelude.h"
-
-int main() {
-    void *dict = new_dict();
-    int *i = malloc(sizeof(int));
-    *i = 1;
-    dict_set(dict, "1", i);
-    i = malloc(sizeof(int));
-    *i = 2;
-    dict_set(dict, "2", i);
-    i = malloc(sizeof(int));
-    *i = 3;
-    dict_set(dict, "3", i);
-    i = malloc(sizeof(int));
-    *i = 4;
-    dict_set(dict, "4", i);
-
-    assert(*(int*)dict_get(dict, "3") == 3);
-
-    void *iter = dict_begin(dict);
-    for (int i = 1; i <= 4; i++) {
-        assert(*(int*)dict_iter_value(iter) == i);
-        iter = dict_next(dict, iter);
-    }
-
-    printf("[PASSED] map\n");
-    return 0;
-}

+ 0 - 23
tests/test_str.c

@@ -1,23 +0,0 @@
-#include "dymc.h"
-
-#include <assert.h>
-#include <stdio.h>
-#include <string.h>
-
-int main() {
-    void *ss = new_ss();
-    ss_add(ss, "test %d", 1);
-    ss_add(ss, ", %d", 2);
-    assert(strcmp("test 1, 2", ss_cstr(ss)) == 0);
-    assert(ss_size(ss) == 9);
-
-    const char *s = "a,bc,def";
-    void *str_list = str_split(s, ',');
-    assert(vec_size(str_list) == 3);
-    assert(strcmp("a", vec_get(str_list, 0)) == 0);
-    assert(strcmp("bc", vec_get(str_list, 1)) == 0);
-    assert(strcmp("def", vec_get(str_list, 2)) == 0);
-
-    printf("[PASSED] str\n");
-    return 0;
-}

+ 25 - 0
tests/test_string.c

@@ -0,0 +1,25 @@
+#include "dymc.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "str.h"
+
+int main() {
+    StringBuilder builder = new_StringBuilder();
+    StringBuilder_append(builder, "test %d", 1);
+    StringBuilder_append(builder, ", %d", 2);
+    assert(strcmp("test 1, 2", StringBuilder_getString(builder)) == 0);
+    assert(StringBuilder_size(builder) == 9);
+
+    // String s = "a,bc,def";
+    // List str_list = str_split(s, ',');
+    // assert(vec_size(str_list) == 3);
+    // assert(strcmp("a", vec_get(str_list, 0)) == 0);
+    // assert(strcmp("bc", vec_get(str_list, 1)) == 0);
+    // assert(strcmp("def", vec_get(str_list, 2)) == 0);
+
+    printf("[PASSED] STRING\n");
+    return 0;
+}

+ 34 - 0
tests/test_tree_map.c

@@ -0,0 +1,34 @@
+#include "tree_map.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "gc_prelude.h"
+
+int main() {
+    TreeMap dict = new_TreeMap((CompareFunc)strcmp);
+    
+    Integer i = new_Integer(1);
+    TreeMap_set(dict, "1", i);
+    
+    i = new_Integer(2);
+    TreeMap_set(dict, "2", i);
+
+    i = new_Integer(3);
+    TreeMap_set(dict, "3", i);
+
+    i = new_Integer(4);
+    TreeMap_set(dict, "4", i);
+
+    assert(*(Integer)TreeMap_get(dict, "3") == 3);
+
+    TreeMapIter iter = TreeMap_begin(dict);
+    for (int i = 1; i <= 4; i++) {
+        assert(*(Integer)TreeMapIter_getValue(iter) == i);
+        iter = TreeMap_next(dict, iter);
+    }
+
+    printf("[PASSED] TreeMap\n");
+    return 0;
+}

+ 0 - 26
tests/test_vec.c

@@ -1,26 +0,0 @@
-#include <assert.h>
-#include <stdio.h>
-
-#include "vec.h"
-#include "gc_prelude.h"
-
-int main() {
-    void *v = new_vec();
-    double *n = malloc(sizeof(double));
-    *n = 42.42;
-    vec_push_back(v, n);
-    assert(*(double*)vec_get(v, 0) == 42.42);
-
-    v = new_vec();
-    for (int i = 0; i < 10000; i++) {
-        int *x = malloc(sizeof(int));
-        *x = i;
-        vec_push_back(v, x);
-        assert(vec_size(v) == i + 1);
-    }
-    for (int i = 0; i < 10000; i++) {
-        assert(*(int*)vec_get(v, i) == i);
-    }
-
-    printf("[PASSED] vec\n");
-}

+ 24 - 0
tests/test_vector.c

@@ -0,0 +1,24 @@
+#include <assert.h>
+#include <stdio.h>
+
+#include "vector.h"
+#include "gc_prelude.h"
+
+int main() {
+    Vector v = new_Vector();
+    Double n = new_Double(42.42);
+    Vector_pushBack(v, n);
+    assert(*(Double)Vector_get(v, 0) == 42.42);
+
+    v = new_Vector();
+    for (int i = 0; i < 10000; i++) {
+        Integer x = new_Integer(i);
+        Vector_pushBack(v, x);
+        assert(Vector_size(v) == i + 1);
+    }
+    for (int i = 0; i < 10000; i++) {
+        assert(*(Integer)Vector_get(v, i) == i);
+    }
+
+    printf("[PASSED] Vector\n");
+}