1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
|
$TESTING = true
$0 = __FILE__ if $0 == "-e" # for autotest style execution
require 'inline'
require 'tempfile'
require 'tmpdir'
require 'test/unit'
File.umask(0)
module Foo
class Bar
inline do |builder|
builder.c <<-EOC
int arity6(int u, int v, int w, int x, int y, char * z) { return x + y + strlen(z); }
EOC
end
end
end
class InlineTestCase < Test::Unit::TestCase
def setup
super
@rootdir = File.join(Dir.tmpdir, "test_inline.#{$$}")
Dir.mkdir @rootdir, 0700 unless test ?d, @rootdir
ENV['INLINEDIR'] = @rootdir
end
def teardown
unless $DEBUG then
FileUtils.rm_rf @rootdir
ENV.delete 'INLINEDIR'
end
end
def test_stupid
#shuts test unit up
end
end
class TestDir < InlineTestCase
def setup
super
@count = 1
end
def util_assert_secure(perms, should_pass)
path = File.join(@rootdir, @count.to_s)
@count += 1
Dir.mkdir path, perms unless perms.nil?
if should_pass then
assert_nothing_raised do
Dir.assert_secure path
end
else
assert_raises(perms.nil? ? Errno::ENOENT : SecurityError) do
Dir.assert_secure path
end
end
end
def test_assert_secure
# existing/good
util_assert_secure 0700, true
# existing/bad
util_assert_secure 0707, false
util_assert_secure 0770, false
util_assert_secure 0777, false
# missing
util_assert_secure nil, false
end
end unless Inline::WINDOZE
class TestInline < InlineTestCase
def test_rootdir
assert_equal(@rootdir, Inline.rootdir)
end
def test_directory
inlinedir = File.join(@rootdir, ".ruby_inline")
assert_equal(inlinedir, Inline.directory)
end
end
class TestInline
class TestC < InlineTestCase
def setup
super
@builder = Inline::C.new(self.class)
end
# quick hack to make tests more readable,
# does nothing I wouldn't otherwise do...
def inline(lang=:C)
self.class.inline(lang, true) do |builder|
yield(builder)
end
end
def test_initialize
x = Inline::C.new(self.class)
assert_equal TestInline::TestC, x.mod
assert_equal [], x.src
assert_equal({}, x.sig)
assert_equal [], x.flags
assert_equal [], x.libs
end
def test_ruby2c
x = Inline::C.new(self.class)
assert_equal 'NUM2CHR', x.ruby2c("char")
assert_equal 'STR2CSTR', x.ruby2c("char *")
assert_equal 'FIX2INT', x.ruby2c("int")
assert_equal 'NUM2INT', x.ruby2c("long")
assert_equal 'NUM2UINT', x.ruby2c("unsigned int")
assert_equal 'NUM2UINT', x.ruby2c("unsigned long")
assert_equal 'NUM2UINT', x.ruby2c("unsigned")
assert_equal '', x.ruby2c("VALUE")
assert_raises ArgumentError do
x.ruby2c('blah')
end
end
def test_c2ruby
x = Inline::C.new(self.class)
assert_equal 'CHR2FIX', x.c2ruby("char")
assert_equal 'rb_str_new2', x.c2ruby("char *")
assert_equal 'INT2FIX', x.c2ruby("int")
assert_equal 'INT2NUM', x.c2ruby("long")
assert_equal 'UINT2NUM', x.c2ruby("unsigned int")
assert_equal 'UINT2NUM', x.c2ruby("unsigned long")
assert_equal 'UINT2NUM', x.c2ruby("unsigned")
assert_equal '', x.c2ruby("VALUE")
assert_raises ArgumentError do
x.c2ruby('blah')
end
end
def util_module_name(*signatures)
md5 = Digest::MD5.new
signatures.each do |signature|
@builder.sig[signature] = [nil, 0]
md5 << signature.to_s
end
assert_equal("Inline_TestInline__TestC_#{md5.to_s[0,4]}",
@builder.module_name)
end
def test_module_name_0_methods
util_module_name
end
def test_module_name_1_method
util_module_name :something1
end
def test_module_name_2_methods
util_module_name :something2, :something3
end
def test_module_name_2_other_methods
util_module_name :something4, :something5
end
def util_parse_signature(src, expected, t=nil, a=nil, b=nil)
result = nil
@builder.add_type_converter t, a, b unless t.nil?
result = @builder.parse_signature(src)
assert_equal(expected, result)
end
def test_parse_signature
src = "// stupid cpp comment
#include \"header.h\"
/* stupid c comment */
int
add(int x, int y) {
int result = x+y;
return result;
}
"
expected = {
'name' => 'add',
'return' => 'int',
'arity' => 2,
'args' => [
['x', 'int'],
['y', 'int']
]
}
util_parse_signature(src, expected)
end
def test_parse_signature_custom
src = "// stupid cpp comment
#include \"header.h\"
/* stupid c comment */
int
add(fooby x, int y) {
int result = x+y;
return result;
}
"
expected = {
'name' => 'add',
'return' => 'int',
'arity' => 2,
'args' => [
[ 'x', 'fooby' ],
['y', 'int']
]
}
util_parse_signature(src, expected,
"fooby", "r2c_fooby", "c2r_fooby")
end
def test_parse_signature_register
src = "// stupid cpp comment
#include \"header.h\"
/* stupid c comment */
int
add(register int x, int y) {
int result = x+y;
return result;
}
"
expected = {
'name' => 'add',
'return' => 'int',
'arity' => 2,
'args' => [
[ 'x', 'register int' ],
['y', 'int']
]
}
util_parse_signature(src, expected,
"register int", 'FIX2INT', 'INT2FIX')
end
def util_generate(src, expected, expand_types=true)
result = @builder.generate src, expand_types
result.gsub!(/\# line \d+/, '# line N')
expected = "# line N \"#{$0}\"\n" + expected
assert_equal(expected, result)
end
def util_generate_raw(src, expected)
util_generate(src, expected, false)
end
# Ruby Arity Rules, from the mouth of Matz:
# -2 = ruby array argv
# -1 = c array argv
# 0 = self
# 1 = self, value
# 2 = self, value, value
# ...
# 16 = self, value * 15
def test_generate_raw_arity_0
src = "VALUE y(VALUE self) {blah;}"
expected = "static VALUE y(VALUE self) {blah;}"
util_generate_raw(src, expected)
end
def test_generate_arity_0
src = "int y() { do_something; return 42; }"
expected = "static VALUE y(VALUE self) {\n do_something; return INT2FIX(42); }"
util_generate(src, expected)
end
def test_generate_arity_0_no_return
src = "void y() { do_something; }"
expected = "static VALUE y(VALUE self) {\n do_something;\nreturn Qnil;\n}"
util_generate(src, expected)
end
def test_generate_arity_0_void_return
src = "void y(void) {go_do_something_external;}"
expected = "static VALUE y(VALUE self) {
go_do_something_external;\nreturn Qnil;\n}"
util_generate(src, expected)
end
def test_generate_arity_0_int_return
src = "int x() {return 42}"
expected = "static VALUE x(VALUE self) {
return INT2FIX(42)}"
util_generate(src, expected)
end
def test_generate_raw_arity_1
src = "VALUE y(VALUE self, VALUE obj) {blah;}"
expected = "static VALUE y(VALUE self, VALUE obj) {blah;}"
util_generate_raw(src, expected)
end
def test_generate_arity_1
src = "int y(int x) {blah; return x+1;}"
expected = "static VALUE y(VALUE self, VALUE _x) {\n int x = FIX2INT(_x);\nblah; return INT2FIX(x+1);}"
util_generate(src, expected)
end
def test_generate_arity_1_no_return
src = "void y(int x) {blah;}"
expected = "static VALUE y(VALUE self, VALUE _x) {\n int x = FIX2INT(_x);\nblah;\nreturn Qnil;\n}"
util_generate(src, expected)
end
def test_generate_raw_arity_2
src = "VALUE func(VALUE self, VALUE obj1, VALUE obj2) {blah;}"
expected = "static VALUE func(VALUE self, VALUE obj1, VALUE obj2) {blah;}"
util_generate_raw(src, expected)
end
def test_generate_arity_2
src = "int func(int x, int y) {blah; return x+y;}"
expected = "static VALUE func(VALUE self, VALUE _x, VALUE _y) {\n int x = FIX2INT(_x);\n int y = FIX2INT(_y);\nblah; return INT2FIX(x+y);}"
util_generate(src, expected)
end
def test_generate_raw_arity_3
src = "VALUE func(VALUE self, VALUE obj1, VALUE obj2, VALUE obj3) {blah;}"
expected = "static VALUE func(VALUE self, VALUE obj1, VALUE obj2, VALUE obj3) {blah;}"
util_generate_raw(src, expected)
end
def test_generate_arity_too_many
src = "int too_many(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int aA, int aB, int aC, int aD, int aE, int aF, int ugh) {
int q = v + w;
return q+x+y+z;
}"
assert_raise(ArgumentError) do
@builder.generate src, true
end
end
def test_generate_comments
src = "// stupid cpp comment
/* stupid c comment */
int
add(int x, int y) { // add two numbers
return x+y;
}
"
expected = "static VALUE add(VALUE self, VALUE _x, VALUE _y) {
int x = FIX2INT(_x);
int y = FIX2INT(_y);
return INT2FIX(x+y);
}
"
util_generate(src, expected)
end
def test_generate_local_header
src = "// stupid cpp comment
#include \"header\"
/* stupid c comment */
int
add(int x, int y) { // add two numbers
return x+y;
}
"
# FIX: should be 2 spaces before the return. Can't find problem.
expected = "#include \"header\"
static VALUE add(VALUE self, VALUE _x, VALUE _y) {
int x = FIX2INT(_x);
int y = FIX2INT(_y);
return INT2FIX(x+y);
}
"
util_generate(src, expected)
end
def test_generate_system_header
src = "// stupid cpp comment
#include <header>
/* stupid c comment */
int
add(int x, int y) { // add two numbers
return x+y;
}
"
expected = "#include <header>
static VALUE add(VALUE self, VALUE _x, VALUE _y) {
int x = FIX2INT(_x);
int y = FIX2INT(_y);
return INT2FIX(x+y);
}
"
util_generate(src, expected)
end
def test_generate_wonky_return
src = "unsigned\nlong z(void) {return 42}"
expected = "static VALUE z(VALUE self) {
return UINT2NUM(42)}"
util_generate(src, expected)
end
def test_generate_compact
src = "int add(int x, int y) {return x+y}"
expected = "static VALUE add(VALUE self, VALUE _x, VALUE _y) {
int x = FIX2INT(_x);
int y = FIX2INT(_y);
return INT2FIX(x+y)}"
util_generate(src, expected)
end
def test_generate_char_star_normalize
src = "char\n\*\n blah( char*s) {puts(s); return s}"
expected = "static VALUE blah(VALUE self, VALUE _s) {
char * s = STR2CSTR(_s);
puts(s); return rb_str_new2(s)}"
util_generate(src, expected)
end
def test_c
result = @builder.c "int add(int a, int b) { return a + b; }"
expected = "# line N \"#{$0}\"\nstatic VALUE add(VALUE self, VALUE _a, VALUE _b) {\n int a = FIX2INT(_a);\n int b = FIX2INT(_b);\n return INT2FIX(a + b); }"
result.gsub!(/\# line \d+/, '# line N')
assert_equal expected, result
assert_equal [expected], @builder.src
end
def test_c_raw
src = "static VALUE answer_raw(int argc, VALUE *argv, VALUE self) { return INT2NUM(42); }"
result = @builder.c_raw src.dup
result.gsub!(/\# line \d+/, '# line N')
expected = "# line N \"#{$0}\"\n" + src
assert_equal expected, result
assert_equal [expected], @builder.src
end
# TODO: fix ?
def util_simple_code(klassname, c_src)
result = "
require 'inline'
class #{klassname}
inline do |builder|
builder.c <<-EOC
#{c_src}
EOC
end
end"
result
end
def util_test_build(src)
tempfile = Tempfile.new("util_test_build")
tempfile.write src
tempfile.close
rb_file = tempfile.path + ".rb"
File.rename tempfile.path, rb_file
begin
Kernel.module_eval { require rb_file }
yield if block_given?
ensure
File.unlink rb_file
end
end
def test_build_good
code = util_simple_code(:DumbTest1, "long dumbpi() { return 314; }")
util_test_build(code) do
result = DumbTest1.new.dumbpi
assert_equal(314, result)
end
end
def test_build_bad
code = util_simple_code(:DumbTest2, "void should_puke() { 1+1 2+2 }")
assert_raises(CompilationError) do
util_test_build(code) do
flunk
end
end
end
def util_strip_comments(input)
expect = 'line 1
#if 0
line 2
#endif
line 3
'
assert_equal expect, @builder.strip_comments(input)
end
def test_strip_comments_cpp
input = 'line 1
#if 0
line 2
#endif
// 1 comment
// 2 comment
line 3 // trailing comment
'
util_strip_comments(input)
end
def test_strip_comments_c
input = 'line 1
#if 0
line 2
#endif
/*
* 1 comment
* 2 comment
*/
line 3 /* trailing comment */
'
util_strip_comments(input)
end
def test_load
# totally tested by test_build
end
end # class TestC
end # class TestInline
$test_module_code = <<-EOR
module Foo
class Bar
inline do |builder|
builder.c <<-EOC
static int forty_two_instance() { return 42; }
EOC
builder.c_singleton <<-EOC
static int twenty_four_class() { return 24; }
EOC
end
end
end
EOR
$test_module_code2 = <<-EOR
require 'inline'
# Demonstrates native functions in nested classes and
# extending a class more than once from different ruby
# source files
module Foo
class Bar
inline do |builder|
builder.c <<-EOC
int twelve_instance() { return 12; }
EOC
builder.c_singleton <<-EOC
int twelve_class() { return 12; }
EOC
end
end
end
EOR
class TestModule < InlineTestCase
def test_nested
Object.class_eval $test_module_code
fb = Foo::Bar.new
assert_equal(42, fb.forty_two_instance)
assert_equal(24, Foo::Bar.twenty_four_class)
tempfile = Tempfile.new("test_inline_nested")
tempfile.write($test_module_code2)
tempfile.flush
tempfile.rewind
`cp #{tempfile.path} #{tempfile.path}.rb`
require "#{tempfile.path}.rb"
assert_equal(12, fb.twelve_instance)
assert_equal(12, Foo::Bar.twelve_class)
`rm "#{tempfile.path}.rb"`
end
def test_argument_check_good
fb = Foo::Bar.new
assert_equal 13, fb.arity6(1, 2, 3, 4, 5, "blah")
end
def test_argument_check_fewer
fb = Foo::Bar.new
assert_raise(ArgumentError) do
assert_equal 13, fb.arity6(1, 2, 3)
end
end
def test_argument_check_more
fb = Foo::Bar.new
assert_raise ArgumentError do
assert_equal 13, fb.arity6(1, 2, 3, 4, 5, "blah", :extra)
end
end
def test_inline
self.class.inline(:C) do |builder|
builder.c "int add(int a, int b) { return a + b; }"
end
assert(test(?d, Inline.directory),
"inline dir should have been created")
matches = Dir[File.join(Inline.directory, "Inline_TestModule_*.c")]
assert_equal(1, matches.length, "Source should have been created")
library_file = matches.first.gsub(/\.c$/) { "." + Config::CONFIG["DLEXT"] }
assert(test(?f, library_file),
"Library file should have been created")
end
end
class TestInlinePackager < InlineTestCase
def setup
super
@name = "Packager Test"
@version = "1.0.0"
@summary = "This is a Packager test gem"
@packager = Inline::Packager.new @name, @version, @summary
@package_dir = @rootdir + "_package"
Dir.mkdir @package_dir, 0700 unless test ?d, @package_dir
@orig_dir = Dir.pwd
Dir.chdir @package_dir
@ext = Config::CONFIG['DLEXT']
end
def teardown
super
Dir.chdir @orig_dir
FileUtils.rm_rf @package_dir unless $DEBUG
end
def util_generate_rakefile
summary = @summary
name = @name
version = @version
gem_libs = []
eval Inline::Packager::RAKEFILE_TEMPLATE
end
def test_initialize
assert_equal "lib/inline", @packager.inline_dir
assert_equal false, @packager.libs_copied
end
def test_package
assert_nothing_raised do
@packager.package
end
end
def test_copy_libs
assert_equal false, @packager.libs_copied
built_lib = "Inline_Test.#{@ext}"
dir = "#{@rootdir}/.ruby_inline"
Dir.mkdir dir, 0700 unless test ?d, dir
Dir.chdir dir do
FileUtils.touch built_lib
end
@packager.copy_libs
assert_equal true, File.directory?("#{@package_dir}/lib/inline")
assert_equal true, File.exists?("#{@package_dir}/lib/inline/#{built_lib}")
assert_equal true, @packager.libs_copied
end
def test_generate_rakefile_has_rakefile
FileUtils.rm 'Rakefile' if test ?f, 'Rakefile' and $DEBUG
FileUtils.touch 'Rakefile'
@packager.generate_rakefile
assert_equal "", File.read('Rakefile')
end
def test_generate_rakefile_no_rakefile
FileUtils.rm 'Rakefile' if test ?f, 'Rakefile' and $DEBUG
FileUtils.rm_r 'lib' if test ?d, 'lib' and $DEBUG
@packager.generate_rakefile
assert_equal util_generate_rakefile, File.read('Rakefile')
end
def test_build_gem
# test_copy_libs
pwd = Dir.pwd
File.open 'Rakefile', 'w' do |fp|
src = util_generate_rakefile
fp.puts src
end
@packager.build_gem
package_name = "pkg/#{@name}-#{@version}.gem"
assert File.exists?(package_name), "File #{package_name} must exist"
assert system("#{Inline::GEM} check #{package_name}"), "gem check must pass"
end
def test_gem_libs
system "rm lib/inline/*" if $DEBUG # hacky
@packager.libs_copied = true
expected = ["lib/blah.rb", "lib/inline/Inline_Test.#{@ext}"]
FileUtils.mkdir_p "lib/inline"
FileUtils.touch(expected)
assert_equal expected, @packager.gem_libs
end
end
|