Skip to content

Commit 74c6e57

Browse files
committed
Updated tests
1 parent 03bbc67 commit 74c6e57

15 files changed

+765
-163
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ Original Authors "MobiRuby developers" are [https://github.com/mobiruby/mobiruby
2222
## License
2323

2424
See Copyright Notice in [mobiruby_common.h](https://github.com/mobiruby/mobiruby-common/blob/master/include/mobiruby_common.h).
25-

test/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
cfunc_test
1+
t/*.exe
2+
t/*.c

test/Makefile

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
# makefile discription.
2-
# basic build file for cocoa library
2+
# basic build file for cfunc library
33

44
# project-specific macros
55
# extension of the executable-file is modifiable(.exe .out ...)
66
BASEDIR = .
7-
TARGET := $(BASEDIR)/mobiruby-common-test
8-
MRBCSRC := $(patsubst %.rb,%.c,$(wildcard $(BASEDIR)/mrb/*.rb))
9-
MRBDATASRC := $(patsubst %.rb,%.c,$(wildcard $(BASEDIR)/mrb_data/*.rb))
10-
LIBR := $(BASEDIR)/../vendors/lib/libmruby-cfunc.a $(BASEDIR)/../lib/libmobiruby-common.a
11-
12-
EXCEPT1 := $(MRBCSRC)
7+
REQUIRES := ./t/require1.rb
8+
TESTSRCS := $(filter-out $(REQUIRES),$(wildcard $(BASEDIR)/t/*.rb))
9+
TESTOBJS := $(patsubst %.rb,%.o,$(TESTSRCS))
10+
TESTS := $(patsubst %.rb,%.exe,$(TESTSRCS))
11+
REQUIREOBJS := $(patsubst %.rb,%.o,$(REQUIRES))
1312

14-
OBJ1 := $(patsubst %.c,%.o,$(filter-out $(EXCEPT1),$(wildcard $(BASEDIR)/*.c)))
15-
OBJMRB := $(patsubst %.c,%.o,$(MRBCSRC))
16-
OBJMRBDATA := $(patsubst %.c,%.o,$(MRBDATASRC))
17-
OBJS := $(OBJ1) $(OBJ2) $(OBJ3)
1813

14+
LIBR := $(BASEDIR)/../vendors/lib/libmruby-cfunc.a $(BASEDIR)/../lib/libmobiruby-common.a
1915
MRBC := $(BASEDIR)/../vendors/bin/mrbc
2016

2117
# libffi
@@ -28,7 +24,7 @@ LIBS = -ldl -lm
2824
ifeq ($(shell uname -s),Darwin)
2925
LDFLAGS = -Wl,-allow_stack_execute
3026
else
31-
LDFLAGS =
27+
LDFLAGS = -Wl,--export-dynamic
3228
endif
3329

3430
ifeq ($(strip $(COMPILE_MODE)),)
@@ -47,7 +43,6 @@ endif
4743
ALL_CFLAGS = -Wall -Werror-implicit-function-declaration -std=c99 $(CFLAGS) $(MRUBY_CFLAGS) $(LIBFFI_CFLAGS)
4844

4945

50-
5146
##############################
5247
# internal variables
5348

@@ -60,35 +55,45 @@ export CAT := cat
6055
# generic build targets, rules
6156

6257
.PHONY : all
63-
all : $(TARGET)
58+
all : $(TESTS)
6459

6560
.PHONY : run
66-
run : $(TARGET)
67-
./$(TARGET)
61+
run : $(TESTS)
62+
@echo
63+
@echo "*** Start tests ***"
64+
@echo
65+
@for command in $(TESTS) ; do \
66+
$$command || exit 1 ; \
67+
echo ; \
68+
done
69+
6870

6971
# executable constructed using linker from object files
70-
$(TARGET) : $(OBJS) $(OBJMRB) $(LIBR) $(OBJMRBDATA)
71-
$(LL) -o $@ $(CFLAGS) $(LDFLAGS) $^ $(MRUBY_LIBS) $(LIBFFI_LIBS) $(LIBS)
72+
$(TESTS) : %.exe : %.o $(REQUIREOBJS)
73+
$(LL) $< $(REQUIREOBJS) -o $@ $(CFLAGS) $(LDFLAGS) $(LIBR) $(LIBFFI_LIBS) $(MRUBY_LIBS) $(LIBS)
74+
7275

73-
-include $(OBJS:.o=.d) $(OBJMRB:.o=.d) $(OBJMRBDATA:.o=.d)
76+
-include $(OBJS:.o=.d) $(OBJMRB:.o=.d)
7477

7578
# mrby complie
76-
$(OBJMRB) : %.o : %.rb
77-
$(MRBC) -Cinit_$(*F) $<
79+
$(TESTOBJS) : %.o : %.rb
80+
cat $< | ruby -e "print STDIN.read.gsub(/#\s*BEGIN\s+C.*/m,'')" | $(MRBC) -o- -Btest_irep - > $(basename $<).c
81+
echo "const char* appname = \"$(basename $<)\";" >> $(basename $<).c
82+
cat main.c >> $(basename $<).c
83+
cat $< | ruby -e 's=STDIN.read;print s.gsub(/.*#\s*BEGIN\s+C(.*)/m,"\\1") if s.match(/#\s*BEGIN\s+C/)' >> $(basename $<).c
7884
$(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(basename $<).c -o $@
7985

80-
$(OBJMRBDATA) : %.o : %.rb
81-
$(MRBC) -Bmruby_data_$(*F) $<
86+
$(REQUIREOBJS) : %.o : %.rb
87+
echo $<
88+
echo $(basename $<)
89+
$(MRBC) -Bmruby_data_`basename $(basename $<)` $<
8290
$(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(basename $<).c -o $@
8391

84-
# objects compiled from source
85-
$(OBJS) : %.o : %.c
86-
$(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $< -o $@
87-
8892
# clean up
8993
.PHONY : clean #cleandep
9094
clean :
9195
@echo "make: removing targets, objects and depend files of `pwd`"
92-
-$(RM_F) $(TARGET) $(OBJS) $(OBJMRB) $(OBJMRBDATA) $(MRB)
93-
-$(RM_F) $(OBJS:.o=.d) $(OBJY:.o=.d)
94-
-$(RM_F) $(patsubst %.c,%.o,$(EXCEPT1)) $(patsubst %.c,%.d,$(EXCEPT1))
96+
-$(RM_F) $(TESTS) $(TESTOBJS) $(TESTS)
97+
-$(RM_F) $(TESTOBJS:.o=.d) $(TESTOBJS:.o=.c)
98+
-$(RM_F) t/require/require1.c t/require/require1.o t/require/require1.d
99+

test/main.c

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,109 @@
11
#include "cfunc.h"
2+
#include "mobiruby_common.h"
23

34
#include "mruby.h"
45
#include "mruby/dump.h"
56
#include "mruby/proc.h"
67
#include "mruby/compile.h"
7-
#include "mobiruby_common.h"
88

99
struct mrb_state_ud {
1010
struct cfunc_state cfunc_state;
1111
};
1212

13-
14-
void
15-
init_unittest(mrb_state *mrb);
16-
17-
void
18-
init_mobi_common_test(mrb_state *mrb);
19-
13+
const char* assert_rb =
14+
"$ok_test = 0" "\n"
15+
"$ko_test = 0" "\n"
16+
"$kill_test = 0" "\n"
17+
"$asserts = []" "\n"
18+
""
19+
"def assert(test, failure_message = 'Assertion failed')" "\n"
20+
" assert_equal(true, !!test, failure_message)" "\n"
21+
"end" "\n"
22+
""
23+
"def assert_not_equal(expected, actual, failure_message = nil)" "\n"
24+
" assert_equal(false, expected === actual, \"#{actual} expected not '#{expected}' assertion failed\")" "\n"
25+
"end" "\n"
26+
""
27+
"def assert_equal(expected, actual, failure_message = nil)" "\n"
28+
" failure_message ||= \"#{actual} expected '#{expected}' assertion failed\"" "\n"
29+
" begin" "\n"
30+
" if expected === actual" "\n"
31+
" $ok_test += 1" "\n"
32+
" print('.')" "\n"
33+
" else" "\n"
34+
" $asserts.push(['Fail: ', failure_message])" "\n"
35+
" $ko_test += 1" "\n"
36+
" print('F')" "\n"
37+
" end" "\n"
38+
" rescue Exception => e" "\n"
39+
" $asserts.push(['Error: ', failure_message, e])" "\n"
40+
" $kill_test += 1" "\n"
41+
" print('X')" "\n"
42+
" end" "\n"
43+
"end" "\n"
44+
""
45+
// This method copy from mruby/test/assert.rb" "\n"
46+
// License: MITL mruby developers" "\n"
47+
// Report the test result and print all assertions" "\n"
48+
// which were reported broken." "\n"
49+
"def test_results()" "\n"
50+
" print \"\\n\"" "\n"
51+
" $asserts.each do |err, str, e|" "\n"
52+
" print(err);" "\n"
53+
" print(str);" "\n"
54+
" if e" "\n"
55+
" print(\" => \")" "\n"
56+
" print(e.message)" "\n"
57+
" end" "\n"
58+
" print(\"\\n\")" "\n"
59+
" end" "\n"
60+
""
61+
" $total_test = $ok_test.+($ko_test)" "\n"
62+
" print('Total: ')" "\n"
63+
" print($total_test)" "\n"
64+
" print(\"\\n\")" "\n"
65+
""
66+
" print(' OK: ')" "\n"
67+
" print($ok_test)" "\n"
68+
" print(\"\\n\")" "\n"
69+
" print(' KO: ')" "\n"
70+
" print($ko_test)" "\n"
71+
" print(\"\\n\")" "\n"
72+
" print('Crash: ')" "\n"
73+
" print($kill_test)" "\n"
74+
" print(\"\\n\")" "\n"
75+
""
76+
" ($ko_test + $kill_test) == 0" "\n"
77+
"end";
2078

2179
int main(int argc, char *argv[])
2280
{
81+
printf("%s: ", appname);
82+
2383
mrb_state *mrb = mrb_open();
2484
mrb->ud = malloc(sizeof(struct mrb_state_ud));
2585

2686
cfunc_state_offset = cfunc_offsetof(struct mrb_state_ud, cfunc_state);
2787
init_cfunc_module(mrb);
2888
init_mobiruby_common_module(mrb);
2989

30-
init_unittest(mrb);
90+
mrb_load_string(mrb, assert_rb);
3191
if (mrb->exc) {
3292
mrb_p(mrb, mrb_obj_value(mrb->exc));
93+
exit(1);
3394
}
3495

35-
init_mobi_common_test(mrb);
96+
int n = mrb_read_irep(mrb, test_irep);
97+
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
3698
if (mrb->exc) {
3799
mrb_p(mrb, mrb_obj_value(mrb->exc));
100+
mrb_load_string(mrb, "test_results();");
101+
exit(1);
102+
}
103+
if(mrb_test(mrb_load_string(mrb, "test_results()"))) {
104+
exit(0);
105+
}
106+
else {
107+
exit(1);
38108
}
39-
}
40-
41-
42-
struct STest {
43-
int8_t x;
44-
int16_t y;
45-
int32_t z;
46-
};
47-
48-
49-
struct STest2 {
50-
struct STest s;
51-
double xx;
52-
};
53-
54-
55-
struct STest cfunc_test_func1(struct STest val) {
56-
val.z = val.x + val.y;
57-
return val;
58-
};
59-
60-
61-
struct STest2 cfunc_test_func2(struct STest2 val) {
62-
val.xx = (double)(val.s.x + val.s.y) / val.s.z;
63-
return val;
64-
};
65-
66-
67-
int cfunc_test_func3(int (*func)(int, int)) {
68-
return func(10, 20);
69109
}

test/mobiruby-common-test

-384 KB
Binary file not shown.

test/mrb/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)