Skip to content

Commit ebec808

Browse files
committed
added test system
1 parent 2c7f870 commit ebec808

11 files changed

+264
-4
lines changed

Gemfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'guard'
4+
gem 'guard-rake'
5+
gem 'rb-fsevent'
6+
7+
gem 'ffi'

Gemfile.lock

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
coderay (1.0.9)
5+
ffi (1.8.1)
6+
formatador (0.2.4)
7+
guard (1.8.0)
8+
formatador (>= 0.2.4)
9+
listen (>= 1.0.0)
10+
lumberjack (>= 1.0.2)
11+
pry (>= 0.9.10)
12+
thor (>= 0.14.6)
13+
guard-rake (0.0.9)
14+
guard
15+
rake
16+
listen (1.1.3)
17+
rb-fsevent (>= 0.9.3)
18+
rb-inotify (>= 0.9)
19+
rb-kqueue (>= 0.2)
20+
lumberjack (1.0.3)
21+
method_source (0.8.1)
22+
pry (0.9.12.2)
23+
coderay (~> 1.0.5)
24+
method_source (~> 0.8)
25+
slop (~> 3.4)
26+
rake (10.0.4)
27+
rb-fsevent (0.9.3)
28+
rb-inotify (0.9.0)
29+
ffi (>= 0.5.0)
30+
rb-kqueue (0.2.0)
31+
ffi (>= 0.5.0)
32+
slop (3.4.5)
33+
thor (0.18.1)
34+
35+
PLATFORMS
36+
ruby
37+
38+
DEPENDENCIES
39+
ffi
40+
guard
41+
guard-rake
42+
rb-fsevent

Guardfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
guard 'rake', :task => 'mrbtest' do
3+
watch(%r{^mrblib/(.+)\.rb$})
4+
watch(%r{^specs/(.+)\.rb$})
5+
watch(%r{^specs/libtest/libtest.dylib})
6+
end
7+
8+
9+
guard 'rake', task: 'specs/libtest/libtest.dylib' do
10+
watch(%r{^specs/libtest/libtest.c})
11+
end

Rakefile

+14-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ task :mrbpack do
99
load(File.expand_path('../mrbgem.rake', __FILE__))
1010
target_files = $mrbfiles
1111

12-
test_file_path = File.expand_path('../mruby_test.rb', __FILE__)
13-
if File.exist?(test_file_path)
14-
target_files << test_file_path
12+
target_files.unshift File.expand_path('../specs/init.rb', __FILE__)
13+
14+
15+
%w(
16+
tool.rb
17+
basic_spec.rb
18+
enum_spec.rb
19+
).each do |path|
20+
target_files << File.expand_path("../specs/#{path}", __FILE__)
1521
end
1622

1723
FileUtils.mkdir_p('tmp')
@@ -30,6 +36,10 @@ end
3036

3137
task :mrbtest => :mrbpack do
3238
# replace ourself with mruby process
33-
Process.exec('mruby tmp/blob.rb')
39+
system('mruby tmp/blob.rb')
40+
end
41+
42+
file 'specs/libtest/libtest.dylib' => ["specs/libtest/libtest.c"] do
43+
sh "gcc -dynamiclib -o specs/libtest/libtest.dylib specs/libtest/libtest.c"
3444
end
3545

specs/basic_spec.rb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
3+
module FFITests
4+
module CLib
5+
extend FFI::Library
6+
7+
ffi_lib :c
8+
9+
attach_function :sleep, [:uint32], :uint32
10+
end
11+
12+
module TestLib
13+
extend FFI::Library
14+
15+
ffi_lib '/Users/schmurfy/Dev/personal/mrbgems/mruby-rubyffi-compat/specs/libtest/libtest.dylib'
16+
17+
attach_function :return_uint, [:uint32], :uint32
18+
attach_function :return_double, [:double], :double
19+
attach_function :return_uint_by_address, [:pointer], :void
20+
21+
22+
class S1Struct < FFI::Struct
23+
layout(
24+
:n1, :uint32,
25+
:n2, :uint32,
26+
:s1, :uint16,
27+
:d1, :double,
28+
)
29+
end
30+
31+
attach_function :fill_struct, [:pointer], :void
32+
end
33+
end
34+
35+
36+
37+
should 'return integer by address' do
38+
n = FFI::MemoryPointer.new(:uint32)
39+
FFITests::TestLib.return_uint_by_address(n)
40+
assert_equal(42, n.read_uint32())
41+
end
42+
43+
should 'return integer by value' do
44+
ret = FFITests::TestLib.return_uint(4)
45+
assert_equal(4, ret)
46+
end
47+
48+
should 'return double by value' do
49+
ret = FFITests::TestLib.return_double(4.32)
50+
assert_equal(4.32, ret)
51+
end
52+
53+
should 'fill structure' do
54+
s = FFITests::TestLib::S1Struct.new
55+
FFITests::TestLib.fill_struct(s.addr)
56+
57+
assert_equal(56, s[:n1])
58+
assert_equal(982, s[:n2])
59+
assert_equal(12, s[:s1])
60+
assert_equal(6.78, s[:d1])
61+
end
62+
63+
64+
should 'sleep 1s' do
65+
t = Time.now
66+
FFITests::CLib.sleep(1)
67+
assert_equal(1, (Time.now - t).to_i)
68+
end
69+

specs/enum_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module FFITests
2+
module TestLib
3+
enum :day, [:sunday, 1,
4+
:monday,
5+
:tuesday,
6+
:wednesday,
7+
:thursday,
8+
:friday,
9+
:saturday ]
10+
11+
attach_function :is_work_day, [ :day ], :int
12+
13+
end
14+
end
15+
16+
17+
should 'accept symbol as enum argument' do
18+
eq(0, FFITests::TestLib.is_work_day(:sunday))
19+
eq(0, FFITests::TestLib.is_work_day(:saturday))
20+
eq(1, FFITests::TestLib.is_work_day(:tuesday))
21+
end

specs/init.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module FFI
2+
def self.longsize
3+
8
4+
end
5+
end
6+

specs/libtest/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
all: libtest.so
2+
3+
libtest.so: libtest.c
4+
$(CC) -dynamiclib -o libtest.dylib libtest.c

specs/libtest/libtest.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
#include <stdio.h>
3+
#include <stdint.h>
4+
5+
6+
enum day {
7+
SUNDAY = 1,
8+
MONDAY,
9+
TUESDAY,
10+
WEDNESDAY,
11+
THURSDAY,
12+
FRIDAY,
13+
SATURDAY
14+
};
15+
16+
// A function that takes an argument of the Day enum type:
17+
int is_work_day( enum day day_of_week ){
18+
int ret = ((day_of_week > SUNDAY) && (day_of_week < SATURDAY)) ? 1 : 0;
19+
return ret;
20+
}
21+
22+
23+
unsigned int return_uint(unsigned int n){
24+
return n;
25+
}
26+
27+
double return_double(double d){
28+
return d;
29+
}
30+
31+
void return_uint_by_address(unsigned int *out){
32+
*out = 42;
33+
}
34+
35+
36+
struct s1
37+
{
38+
uint32_t n1;
39+
uint32_t n2;
40+
uint16_t shorty;
41+
double d1;
42+
};
43+
44+
void fill_struct(struct s1 *s)
45+
{
46+
s->n1 = 56;
47+
s->n2 = 982;
48+
s->shorty = 12;
49+
s->d1 = 6.78;
50+
}
51+
52+
void test(){
53+
printf("Yeah !\n");
54+
}

specs/mri_runner.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'rubygems'
2+
require 'bundler/setup'
3+
4+
require 'ffi'
5+
6+
class FFI::Pointer
7+
def addr
8+
self
9+
end
10+
end
11+
12+
class FFI::Struct
13+
def addr
14+
self
15+
end
16+
end
17+
18+
require File.expand_path('../tool', __FILE__)
19+
require File.expand_path('../basic_spec', __FILE__)
20+
require File.expand_path('../enum_spec', __FILE__)

specs/tool.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
def assert_equal(expected, given)
4+
if expected != given
5+
raise "Expected #{expected}, got #{given}"
6+
end
7+
end
8+
9+
alias :eq :assert_equal
10+
11+
def should(msg)
12+
print "Should #{msg}... #{' ' * (30 - msg.size)}"
13+
yield
14+
puts "OK"
15+
end
16+

0 commit comments

Comments
 (0)