Permalink
Browse files

Add False()/Nil()/NotNil() for unit test object(Assert:XXX).

Fix hash map equals() bug.
  • Loading branch information...
1 parent b0c00db commit 90a671d8664224472d7894064eb1190a8036af4a @emptyland committed Jul 12, 2012
Showing with 205 additions and 138 deletions.
  1. +5 −1 Makefile
  2. +2 −0 hash_map.c
  3. +1 −128 libc.c
  4. +44 −7 libtest.c
  5. +98 −0 tostring.c
  6. +51 −0 tostring.h
  7. +4 −2 value.c
View
@@ -1,6 +1,7 @@
include config.mk
OBJS=state.o value.o memory.o dynamic_array.o hash_map.o skip_list.o closure.o \
- call.o libc.o libtest.o encode.o compiler.o lex.o disassembly.o print.o
+ call.o libc.o libtest.o encode.o compiler.o lex.o disassembly.o print.o \
+ tostring.o
OBJI=yut_rand.o yut.o main_test.o
OBJT=$(OBJS) $(OBJI)
INCS=state.h value.h memory.h
@@ -152,6 +153,9 @@ libtest.o: $(INCS) print.h libc.h libtest.h libtest.c
print.o: print.h print_posix.c
$(CC) $(CFLAGS) print_posix.c -c -o print.o
+tostring.o: $(INCS) tostring.c tostring.h
+ $(CC) $(CFLAGS) tostring.c -c -o tostring.o
+
encode.o: $(INCS) encode.c encode.h
$(CC) $(CFLAGS) encode.c -c -o encode.o
View
@@ -157,6 +157,8 @@ int hmap_equals(const struct hmap *map, const struct hmap *lhs) {
if (map == lhs)
return 1;
i = (1 << map->shift);
+ if (i != (1 << lhs->shift))
+ return 0;
while (i--) {
if (map->item[i].flag != KVI_FREE) {
const struct kvi *it = map->item + i;
View
129 libc.c
@@ -1,4 +1,5 @@
#include "3rd/regex/regex.h"
+#include "tostring.h"
#include "state.h"
#include "value.h"
#include "memory.h"
@@ -24,134 +25,6 @@ static const char *T_STREAM = "stream";
#define PRINT_SPLIT " "
-// Format context
-#define FMTX_STATIC_MAX 260
-struct fmtx {
- char kbuf[FMTX_STATIC_MAX];
- char *dy;
- int last;
- int max;
-};
-#define FMTX_INIT { {0}, NULL, 0, FMTX_STATIC_MAX }
-
-static inline void fmtx_final(struct fmtx *self) {
- if (self->dy) vm_free(self->dy);
- memset(self->kbuf, 0, sizeof(self->kbuf));
- self->dy = NULL;
- self->last = 0;
- self->max = FMTX_STATIC_MAX;
-}
-
-static inline char *fmtx_buf(struct fmtx *self) {
- return !self->dy ? self->kbuf : self->dy;
-}
-
-static inline int fmtx_remain(struct fmtx *self) {
- return self->max - self->last;
-}
-
-static inline char *fmtx_last(struct fmtx *self) {
- return fmtx_buf(self) + self->last;
-}
-
-static inline char *fmtx_add(struct fmtx *self) {
- self->last += strlen(fmtx_last(self));
- return fmtx_buf(self);
-}
-
-static void fmtx_need(struct fmtx *self, int n) {
- if (self->last + n <= (int)sizeof(self->kbuf))
- return;
- if (self->last + n <= self->max)
- return;
- self->max = (self->last + n) * 3 / 2 + FMTX_STATIC_MAX;
- if (!self->dy) {
- self->dy = vm_zalloc(self->max);
- memcpy(self->dy, self->kbuf, self->last);
- } else {
- self->dy = vm_realloc(self->dy, self->max);
- }
-}
-
-static const char *fmtx_append(struct fmtx *self, const char *src, int n) {
- fmtx_need(self, n);
- memcpy(fmtx_last(self), src, n);
- self->last += n;
- return fmtx_buf(self);
-}
-
-static const char *tostring(struct fmtx *ctx, const struct variable *var) {
- switch (var->type) {
- case T_NIL:
- return fmtx_append(ctx, "nil", 3);
- case T_INT:
- fmtx_need(ctx, 24);
- snprintf(fmtx_last(ctx), fmtx_remain(ctx), "%lld", var->value.i);
- return fmtx_add(ctx);
- case T_BOOL:
- return var->value.i ? fmtx_append(ctx, "true", 4)
- : fmtx_append(ctx, "false", 5);
- case T_EXT:
- fmtx_need(ctx, 24);
- snprintf(fmtx_last(ctx), fmtx_remain(ctx), "@%p", var->value.ext);
- return fmtx_add(ctx);
- case T_KSTR:
- return fmtx_append(ctx, kstr_k(var)->land, kstr_k(var)->len);
- case T_FUNC:
- return fmtx_append(ctx, func_k(var)->proto->land,
- func_k(var)->proto->len);
- case T_DYAY: {
- int i;
- fmtx_append(ctx, "[", 1);
- for (i = 0; i < dyay_k(var)->count; ++i) {
- if (i > 0) fmtx_append(ctx, ", ", 2);
- tostring(ctx, dyay_k(var)->elem + i);
- }
- } return fmtx_append(ctx, "]", 1);
- case T_HMAP: {
- struct kvi *initial = hmap_k(var)->item,
- *i = NULL,
- *k = initial + (1 << hmap_k(var)->shift);
- int f = 0;
- fmtx_append(ctx, "{", 1);
- for (i = initial; i != k; ++i) {
- if (!i->flag) continue;
- if (f++ > 0) fmtx_append(ctx, ", ", 2);
- tostring(ctx, &i->k);
- fmtx_append(ctx, " : ", 3);
- tostring(ctx, &i->v);
- }
- } return fmtx_append(ctx, "}", 1);
- case T_SKLS: {
- struct sknd *initial = skls_k(var)->head->fwd[0],
- *i = NULL;
- int f = 0;
- fmtx_append(ctx, "@{", 2);
- for (i = initial; i != NULL; i = i->fwd[0]) {
- if (f++ > 0) fmtx_append(ctx, ", ", 2);
- tostring(ctx, &i->k);
- fmtx_append(ctx, " : ", 3);
- tostring(ctx, &i->v);
- }
- } return fmtx_append(ctx, "}", 1);
- case T_MAND: {
- fmtx_append(ctx, "(", 1);
- if (mand_k(var)->tt)
- fmtx_append(ctx, mand_k(var)->tt, strlen(mand_k(var)->tt));
- else
- fmtx_append(ctx, "*", 1);
- fmtx_append(ctx, ")", 1);
- fmtx_need(ctx, 10 + 24 + 2);
- snprintf(fmtx_last(ctx), fmtx_remain(ctx), "[%d@%p]",
- mand_k(var)->len, mand_k(var)->land);
- } return fmtx_add(ctx);
- default:
- assert(0);
- break;
- }
- return NULL;
-}
-
static int libx_print(struct context *l) {
int i;
struct fmtx fx = FMTX_INIT;
View
@@ -1,3 +1,4 @@
+#include "tostring.h"
#include "state.h"
#include "memory.h"
#include "value.h"
@@ -37,21 +38,34 @@ static void yut_fail2(L, const struct variable *arg0,
const struct variable *arg1) {
struct call_info *up = l->info->chain;
struct func *fn = up->run;
- ymd_printf(yYELLOW
- "[ INFO ] :%d Assert fail, expected<>, unexpected<>;\n"
- yEND,
- fn->u.core->line[up->pc - 1]);
+ struct fmtx fx0 = FMTX_INIT, fx1 = FMTX_INIT;
+ ymd_printf(yYELLOW"[ INFO ] :%d Assert fail, expected"yEND
+ yPURPLE"<%s>"yEND
+ yYELLOW", unexpected"yEND
+ yPURPLE"<%s>"yEND
+ ";\n",
+ fn->u.core->line[up->pc - 1],
+ tostring(&fx0, arg0),
+ tostring(&fx1, arg1));
+ fmtx_final(&fx0);
+ fmtx_final(&fx1);
yut_raise(l);
}
static void yut_fail1(L, const char *expected,
const struct variable *arg0) {
struct call_info *up = l->info->chain;
struct func *fn = up->run;
+ struct fmtx fx = FMTX_INIT;
ymd_printf(yYELLOW"[ INFO ] :%d Assert fail, expected"yEND
yPURPLE"<%s>"yEND
- yYELLOW", unexpected<>;\n"yEND,
- fn->u.core->line[up->pc - 1], expected);
+ yYELLOW", unexpected"yEND
+ yPURPLE"<%s>"yEND
+ ";\n",
+ fn->u.core->line[up->pc - 1],
+ expected,
+ tostring(&fx, arg0));
+ fmtx_final(&fx);
yut_raise(l);
}
@@ -64,6 +78,27 @@ static int libx_True(L) {
return 0;
}
+static int libx_False(L) {
+ struct variable *arg0 = ymd_argv_get(l, 1);
+ if (!is_nil(arg0) && (arg0->type == T_BOOL && arg0->value.i))
+ yut_fail1(l, "false or nil", arg0);
+ return 0;
+}
+
+static int libx_Nil(L) {
+ struct variable *arg0 = ymd_argv_get(l, 1);
+ if (!is_nil(arg0))
+ yut_fail1(l, "nil", arg0);
+ return 0;
+}
+
+static int libx_NotNil(L) {
+ struct variable *arg0 = ymd_argv_get(l, 1);
+ if (is_nil(arg0))
+ yut_fail1(l, "not nil", arg0);
+ return 0;
+}
+
static int libx_EQ(L) {
struct variable *arg0 = ymd_argv_get(l, 1),
*arg1 = ymd_argv_get(l, 2);
@@ -111,7 +146,6 @@ static int yut_case(
}
static int yut_test(const char *clazz, struct variable *test) {
- struct variable v;
struct sknd *i;
struct func *setup, *teardown;
if (test->type != T_SKLS)
@@ -131,6 +165,9 @@ static int yut_test(const char *clazz, struct variable *test) {
LIBC_BEGIN(YutAssertMethod)
LIBC_ENTRY(True)
+ LIBC_ENTRY(False)
+ LIBC_ENTRY(Nil)
+ LIBC_ENTRY(NotNil)
LIBC_ENTRY(EQ)
LIBC_END
View
@@ -0,0 +1,98 @@
+#include "tostring.h"
+#include "state.h"
+#include "memory.h"
+#include "value.h"
+
+void fmtx_final(struct fmtx *self) {
+ if (self->dy) vm_free(self->dy);
+ memset(self->kbuf, 0, sizeof(self->kbuf));
+ self->dy = NULL;
+ self->last = 0;
+ self->max = FMTX_STATIC_MAX;
+}
+
+void fmtx_need(struct fmtx *self, int n) {
+ if (self->last + n <= (int)sizeof(self->kbuf))
+ return;
+ if (self->last + n <= self->max)
+ return;
+ self->max = (self->last + n) * 3 / 2 + FMTX_STATIC_MAX;
+ if (!self->dy) {
+ self->dy = vm_zalloc(self->max);
+ memcpy(self->dy, self->kbuf, self->last);
+ } else {
+ self->dy = vm_realloc(self->dy, self->max);
+ }
+}
+
+const char *tostring(struct fmtx *ctx, const struct variable *var) {
+ switch (var->type) {
+ case T_NIL:
+ return fmtx_append(ctx, "nil", 3);
+ case T_INT:
+ fmtx_need(ctx, 24);
+ snprintf(fmtx_last(ctx), fmtx_remain(ctx), "%lld", var->value.i);
+ return fmtx_add(ctx);
+ case T_BOOL:
+ return var->value.i ? fmtx_append(ctx, "true", 4)
+ : fmtx_append(ctx, "false", 5);
+ case T_EXT:
+ fmtx_need(ctx, 24);
+ snprintf(fmtx_last(ctx), fmtx_remain(ctx), "@%p", var->value.ext);
+ return fmtx_add(ctx);
+ case T_KSTR:
+ return fmtx_append(ctx, kstr_k(var)->land, kstr_k(var)->len);
+ case T_FUNC:
+ return fmtx_append(ctx, func_k(var)->proto->land,
+ func_k(var)->proto->len);
+ case T_DYAY: {
+ int i;
+ fmtx_append(ctx, "[", 1);
+ for (i = 0; i < dyay_k(var)->count; ++i) {
+ if (i > 0) fmtx_append(ctx, ", ", 2);
+ tostring(ctx, dyay_k(var)->elem + i);
+ }
+ } return fmtx_append(ctx, "]", 1);
+ case T_HMAP: {
+ struct kvi *initial = hmap_k(var)->item,
+ *i = NULL,
+ *k = initial + (1 << hmap_k(var)->shift);
+ int f = 0;
+ fmtx_append(ctx, "{", 1);
+ for (i = initial; i != k; ++i) {
+ if (!i->flag) continue;
+ if (f++ > 0) fmtx_append(ctx, ", ", 2);
+ tostring(ctx, &i->k);
+ fmtx_append(ctx, " : ", 3);
+ tostring(ctx, &i->v);
+ }
+ } return fmtx_append(ctx, "}", 1);
+ case T_SKLS: {
+ struct sknd *initial = skls_k(var)->head->fwd[0],
+ *i = NULL;
+ int f = 0;
+ fmtx_append(ctx, "@{", 2);
+ for (i = initial; i != NULL; i = i->fwd[0]) {
+ if (f++ > 0) fmtx_append(ctx, ", ", 2);
+ tostring(ctx, &i->k);
+ fmtx_append(ctx, " : ", 3);
+ tostring(ctx, &i->v);
+ }
+ } return fmtx_append(ctx, "}", 1);
+ case T_MAND: {
+ fmtx_append(ctx, "(", 1);
+ if (mand_k(var)->tt)
+ fmtx_append(ctx, mand_k(var)->tt, strlen(mand_k(var)->tt));
+ else
+ fmtx_append(ctx, "*", 1);
+ fmtx_append(ctx, ")", 1);
+ fmtx_need(ctx, 10 + 24 + 2);
+ snprintf(fmtx_last(ctx), fmtx_remain(ctx), "[%d@%p]",
+ mand_k(var)->len, mand_k(var)->land);
+ } return fmtx_add(ctx);
+ default:
+ assert(0);
+ break;
+ }
+ return NULL;
+}
Oops, something went wrong.

0 comments on commit 90a671d

Please sign in to comment.