cgen 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. data/.gitignore +5 -0
  2. data/History.txt +199 -0
  3. data/README.txt +34 -0
  4. data/examples/bench.rb +14 -0
  5. data/examples/complex.rb +63 -0
  6. data/examples/complex2.rb +48 -0
  7. data/examples/cshadow-example.rb +55 -0
  8. data/examples/cshadow-point.rb +58 -0
  9. data/examples/ctest.rb +34 -0
  10. data/examples/ctest2.rb +32 -0
  11. data/examples/ctest3.rb +179 -0
  12. data/examples/ctest4.rb +18 -0
  13. data/examples/ctest5.rb +27 -0
  14. data/examples/example-ruby-talk-30April2004.rb +65 -0
  15. data/examples/fixed-array.rb +221 -0
  16. data/examples/inherit-example.rb +26 -0
  17. data/examples/inherit-example.txt +80 -0
  18. data/examples/instance-eval.rb +66 -0
  19. data/examples/ivset.rb +55 -0
  20. data/examples/marshal-test.rb +19 -0
  21. data/examples/matrix.rb +91 -0
  22. data/examples/modular-def.rb +87 -0
  23. data/examples/objattr.rb +46 -0
  24. data/examples/opaque-struct-test.rb +36 -0
  25. data/examples/sample.rb +184 -0
  26. data/examples/struct.rb +103 -0
  27. data/examples/test.rb +24 -0
  28. data/examples/yaml.rb +56 -0
  29. data/install.rb +1015 -0
  30. data/lib/cgen/attribute.rb +414 -0
  31. data/lib/cgen/cgen.rb +2041 -0
  32. data/lib/cgen/cshadow.rb +1037 -0
  33. data/lib/cgen/inherit.rb +46 -0
  34. data/rakefile +42 -0
  35. data/tasks/ann.rake +80 -0
  36. data/tasks/bones.rake +20 -0
  37. data/tasks/gem.rake +201 -0
  38. data/tasks/git.rake +40 -0
  39. data/tasks/notes.rake +27 -0
  40. data/tasks/post_load.rake +34 -0
  41. data/tasks/rdoc.rake +51 -0
  42. data/tasks/rubyforge.rake +55 -0
  43. data/tasks/setup.rb +292 -0
  44. data/tasks/spec.rake +54 -0
  45. data/tasks/svn.rake +47 -0
  46. data/tasks/test.rake +40 -0
  47. data/tasks/zentest.rake +36 -0
  48. data/test/test-attribute.rb +430 -0
  49. data/test/test-cgen.rb +127 -0
  50. data/test/test-cshadow.rb +289 -0
  51. data/test/test.rb +17 -0
  52. metadata +123 -0
@@ -0,0 +1,127 @@
1
+ require 'test/unit'
2
+ require 'cgen/cgen'
3
+
4
+
5
+ class BasicTemplateTest < Test::Unit::TestCase
6
+
7
+ class BasicTemplate < CGenerator::Template
8
+ accumulator(:acc0, :acc1, :acc2)
9
+ def initialize(*args)
10
+ super
11
+ add 'acc 0 is ', acc0!, 'acc 1 is ', acc1!, 'acc 2 is ', acc2!
12
+ end
13
+ end
14
+
15
+ def setup
16
+ @template = BasicTemplate.new
17
+ @template.acc0 "zero", "zippo", "zed"
18
+ @template.acc1 "one", "unity", "uno"
19
+ @template.acc2 "two", "deuce", "brace"
20
+ end
21
+ alias set_up setup
22
+
23
+ def test_accumulator
24
+ result = ['acc 0 is ', "zero", "zippo", "zed",
25
+ 'acc 1 is ', "one", "unity", "uno",
26
+ 'acc 2 is ', "two", "deuce", "brace"].join "\n"
27
+ assert_equal(result, @template.to_s)
28
+ end
29
+
30
+ end
31
+
32
+
33
+ class CFragmentTest < Test::Unit::TestCase
34
+
35
+ class CodeTemplate < CGenerator::CFragment
36
+ accumulator(:decl) {StatementKeyAccumulator}
37
+ accumulator(:block) {BlockAccumulator}
38
+ accumulator(:inner, :outer) {StatementAccumulator}
39
+ def initialize(*args)
40
+ super
41
+ add decl!, block(inner!), outer!
42
+ end
43
+ end
44
+
45
+ def setup
46
+ @template = CodeTemplate.new
47
+ @template.decl :i => "int i"
48
+ @template.inner "i = 0"
49
+ @template.outer "f = 1.3"
50
+ @template.decl :f => "float f"
51
+ end
52
+ alias set_up setup
53
+
54
+ def test_accumulator
55
+ result = <<-END
56
+ int i;
57
+ float f;
58
+ {
59
+ i = 0;
60
+ }
61
+ f = 1.3;
62
+ END
63
+ result = result.chomp.tabto 0
64
+ assert_equal(result, @template.to_s)
65
+ end
66
+
67
+ end
68
+
69
+
70
+ class LibraryTest < Test::Unit::TestCase
71
+ class Sample
72
+ end
73
+
74
+ ##=== setup code to be done once ===##
75
+ @lib = CGenerator::Library.new "cgen_test_lib"
76
+
77
+ @lib.define_c_method(Sample, :add).instance_eval {
78
+ arguments "x", "y"
79
+ returns "rb_float_new(NUM2DBL(x) + NUM2DBL(y))"
80
+ }
81
+
82
+ @lib.define_c_singleton_method(Sample, :reverse).instance_eval {
83
+ rb_array_args
84
+ declare :result => "VALUE result"
85
+ reverse_c_name = declare_symbol :reverse
86
+ body "result = rb_funcall(args, #{reverse_c_name}, 0)"
87
+ returns "result"
88
+ }
89
+
90
+ # @lib.declare_symbol "just_to_be_unique_#{(Time.now.to_f * 1000).to_i}"
91
+
92
+ other_include_file, other_source_file = @lib.add_file "other_file"
93
+
94
+ other_source_file.define_c_method(Sample, :sub).instance_eval {
95
+ scope "extern"
96
+ arguments "x", "y"
97
+ returns "rb_float_new(NUM2DBL(x) - NUM2DBL(y))"
98
+ }
99
+
100
+ attr_accessor :ba
101
+
102
+ @lib.before_commit { @@ba = [1] }
103
+ @lib.before_commit { @@ba << 2 }
104
+ @lib.after_commit { @@ba << 3 }
105
+ @lib.after_commit { @@ba << 4 }
106
+
107
+ require 'fileutils'
108
+ dir = File.join("tmp", RUBY_VERSION)
109
+ FileUtils.mkpath dir
110
+ Dir.chdir dir do
111
+ @lib.commit
112
+ end
113
+ ##==================================##
114
+
115
+ def test_add
116
+ assert_equal(3, Sample.new.add(1, 2))
117
+ # assert_equal(-1, Sample.new.sub(1, 2))
118
+ end
119
+
120
+ def test_reverse
121
+ assert_equal([6, 5, 4], Sample.reverse(4, 5, 6))
122
+ end
123
+
124
+ def test_before_after
125
+ assert_equal([1,2,4,3], @@ba)
126
+ end
127
+ end
@@ -0,0 +1,289 @@
1
+ require 'test/unit'
2
+ require 'cgen/cshadow'
3
+
4
+ #
5
+ # Tests in this file focus on:
6
+ # - shadow objects in general, rather than particular attribute
7
+ # types, which are tested in attribute.rb
8
+ # - behavior accessible from Ruby. The examples (complex.rb,
9
+ # matrix.rb) test shadow objects from C.
10
+ # Features tested include inheritance, multiple attributes, omission
11
+ # of readers or writers, etc.
12
+ #
13
+
14
+ # EmptyBase hierarchy tests the following:
15
+ # - inheritance with "gaps"
16
+ # - using the same attr name in parallel branches
17
+
18
+ class EmptyBase
19
+ include CShadow
20
+ end
21
+
22
+ class EBSub_1 < EmptyBase
23
+ shadow_attr_accessor :x => "int x"
24
+ end
25
+
26
+ class EBSub_1_1 < EBSub_1
27
+ end
28
+
29
+ class EBSub_2 < EmptyBase
30
+ end
31
+
32
+ class EBSub_2_2 < EBSub_2
33
+ shadow_attr_accessor :x => "int x"
34
+ end
35
+
36
+
37
+ # Base hierarchy tests the following:
38
+ # - inheritance with multiple attributes
39
+ # - omission of readers and writers
40
+ # - accessors with different names than the variables
41
+ # - conflicting accessor names or C variable names
42
+ # - #each_shadow_attr and #shadow_attrs
43
+ # - protected and private attrs
44
+
45
+ class Base
46
+ include CShadow
47
+ shadow_attr_reader :x => 'int x'
48
+ shadow_attr_writer :y => 'int y'
49
+ shadow_attr_accessor :obj => Array
50
+ shadow_attr_accessor :nonpersistent, :np => Object
51
+ end
52
+
53
+ class Sub_1 < Base
54
+ shadow_attr :z => 'int zzz'
55
+ end
56
+
57
+ # test a class with no shadow_attrs
58
+ class Sub_2 < Base
59
+ attr_reader :ruby_reader
60
+ attr_writer :ruby_writer
61
+ end
62
+
63
+ module Mod_For_Sub_3
64
+ class Sub_3 < Base
65
+ # Make sure the nested class name bug isn't biting today.
66
+ end
67
+ end
68
+
69
+ class Sub_4 < Base
70
+ private :x
71
+ protected :y=
72
+ end
73
+
74
+ # OtherBase tests using shadow_library to specify another library
75
+ # to put definitions in. OtherFile tests using shadow_library_file
76
+ # to put definintions in another file within the same library.
77
+
78
+ class OtherBase
79
+ include CShadow
80
+ shadow_library Base
81
+ shadow_attr_accessor :str => "char *pchar"
82
+ end
83
+
84
+ class OtherFile < OtherBase
85
+ shadow_library_file "OtherFile"
86
+ shadow_attr_accessor :x => "double x"
87
+ end
88
+
89
+
90
+ # Compile-time tests (that is, pre-commit)
91
+
92
+ class CompileTimeTestCase < Test::Unit::TestCase
93
+ def test_conflict
94
+ assert_raises(NameError) {
95
+ Sub_2.class_eval {
96
+ shadow_attr "y" => 'char * yy'
97
+ }
98
+ }
99
+ assert_raises(NameError) {
100
+ Sub_2.class_eval {
101
+ shadow_attr :y => 'char * yy'
102
+ }
103
+ }
104
+ assert_raises(NameError) {
105
+ Sub_2.class_eval {
106
+ shadow_attr :yy => 'char * y'
107
+ }
108
+ }
109
+
110
+ # Checking overwrite by attr_*
111
+ assert_raises(NameError) {
112
+ Sub_2.class_eval {
113
+ attr_accessor :y
114
+ }
115
+ }
116
+ assert_raises(NameError) {
117
+ Sub_2.class_eval {
118
+ attr_reader :y
119
+ }
120
+ }
121
+ assert_raises(NameError) {
122
+ Sub_2.class_eval {
123
+ attr_writer :y
124
+ }
125
+ }
126
+
127
+ # Checking overwrite by shadow_attr_*
128
+ assert_raises(NameError) {
129
+ Sub_2.class_eval {
130
+ shadow_attr_reader :ruby_writer => Object
131
+ }
132
+ }
133
+ assert_raises(NameError) {
134
+ Sub_2.class_eval {
135
+ shadow_attr_writer :ruby_reader => Object
136
+ }
137
+ }
138
+ end
139
+ end
140
+
141
+ require 'fileutils'
142
+ dir = File.join("tmp", RUBY_VERSION)
143
+ FileUtils.mkpath dir
144
+ Dir.chdir dir do
145
+ EmptyBase.commit
146
+ Base.commit # do not commit OtherBase
147
+ end
148
+
149
+ METHOD_MISSING_ERROR =
150
+ RUBY_VERSION.to_f >= 1.7 ?
151
+ NoMethodError :
152
+ NameError
153
+
154
+ # Run-time tests (that is, post-commit)
155
+
156
+ class EmptyBaseTestCase < Test::Unit::TestCase
157
+
158
+ def test_empty_base
159
+ ebs1 = EBSub_1.new
160
+ ebs2 = EBSub_2.new
161
+ ebs11 = EBSub_1_1.new
162
+ ebs22 = EBSub_2_2.new
163
+
164
+ ebs1.x = 3
165
+ ebs11.x = 4
166
+ ebs22.x = 5
167
+
168
+ assert_raises(METHOD_MISSING_ERROR) {
169
+ ebs2.x = 6
170
+ }
171
+
172
+ assert_equal(3, ebs1.x)
173
+ assert_equal(4, ebs11.x)
174
+ assert_equal(5, ebs22.x)
175
+ end
176
+
177
+ end
178
+
179
+ class BaseTestCase < Test::Unit::TestCase
180
+
181
+ def test_limited_access
182
+ b = Sub_1.new
183
+
184
+ assert_raises(METHOD_MISSING_ERROR) {
185
+ b.x = 1
186
+ }
187
+ assert_equal(0, b.x)
188
+
189
+ b.y = 2
190
+ assert_raises(METHOD_MISSING_ERROR) {
191
+ b.y
192
+ }
193
+
194
+ assert_raises(METHOD_MISSING_ERROR) {
195
+ b.z = 3
196
+ }
197
+ assert_raises(METHOD_MISSING_ERROR) {
198
+ b.z
199
+ }
200
+ end
201
+
202
+ def test_inherit
203
+ b = Sub_1.new
204
+
205
+ # test inheritance of attr initializers
206
+ assert_equal(nil, b.obj)
207
+
208
+ # test inheritance of attr dump/load code
209
+ b.obj = [1,2,3]
210
+ assert_equal([1,2,3], Marshal.load(Marshal.dump(b)).obj)
211
+ end
212
+
213
+ def test_marshal
214
+ b = Base.new
215
+ b.obj = [1, {:foo => "foo"}, "bar"]
216
+ b.instance_eval {@z=3}
217
+ bb = Marshal.load(Marshal.dump(b))
218
+ assert_equal(bb.obj, b.obj)
219
+ assert_equal(bb.instance_eval{@z}, b.instance_eval{@z})
220
+ end
221
+
222
+ def test_nonpersistence
223
+ b = Base.new
224
+ assert_equal(nil, b.np)
225
+ b.np = [4,5,6]
226
+ assert_equal([4,5,6], b.np)
227
+ bb = Marshal.load(Marshal.dump(b))
228
+ assert_equal(nil, bb.np)
229
+ end
230
+
231
+ def test_protect
232
+ a = Base.new
233
+ b = Sub_4.new
234
+ assert_nothing_raised {a.x; a.y = 2}
235
+ assert_raises(METHOD_MISSING_ERROR) {
236
+ b.x
237
+ }
238
+ assert_raises(METHOD_MISSING_ERROR) {
239
+ b.y = 2
240
+ }
241
+ end
242
+
243
+ def test_reflection
244
+ names = Sub_1.shadow_attrs.collect { |attr| attr.var.to_s }.sort
245
+ assert_equal(['np', 'obj', 'x', 'y', 'z'], names)
246
+ end
247
+
248
+ end
249
+
250
+ class OtherBaseTestCase < Test::Unit::TestCase
251
+
252
+ def test_sharing_library
253
+ ob = OtherBase.new
254
+ ob.str = "fred"
255
+ assert_equal("fred", ob.str)
256
+
257
+ ob = OtherFile.new
258
+ ob.x = 1.2
259
+ assert_equal(1.2, ob.x)
260
+ end
261
+
262
+ end
263
+
264
+ begin
265
+ require 'yaml'
266
+ module Kernel
267
+ undef :y # else conflict with checking :y is undefined above
268
+ end
269
+ CShadow.allow_yaml
270
+ rescue Exception => e
271
+ $stderr.puts "Could not load yaml : #{e.inspect}"
272
+ else
273
+
274
+ class YamlTest < Test::Unit::TestCase
275
+
276
+ def test_yaml
277
+ base = Base.new
278
+ base.obj = [1,2,3]
279
+ base.np = "456"
280
+
281
+ base2 = YAML.load(YAML.dump(base))
282
+
283
+ assert_equal(base.obj, base2.obj)
284
+ assert_equal(nil, base2.np)
285
+ end
286
+
287
+ end
288
+
289
+ end
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ def run(x)
4
+ puts "-"*x.size
5
+ puts x
6
+ system(x)
7
+ # unless system(x)
8
+ # puts " ... failed: #{$?}"
9
+ # end
10
+ end
11
+
12
+ require 'rbconfig'
13
+ ruby = Config::CONFIG["RUBY_INSTALL_NAME"]
14
+
15
+ run "#{ruby} test-cgen.rb"
16
+ run "#{ruby} test-cshadow.rb"
17
+ run "#{ruby} test-attribute.rb"
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cgen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.16.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel VanderWerf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bones
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.5.1
24
+ version:
25
+ description: |
26
+ Framework for dynamically generating and loading C extensions from Ruby
27
+ programs. Includes CShadow module for defining classes in terms of C structs
28
+ with accessors and inheritance.
29
+
30
+ email: vjoel@users.sourceforge.net
31
+ executables: []
32
+
33
+ extensions: []
34
+
35
+ extra_rdoc_files:
36
+ - History.txt
37
+ - README.txt
38
+ - examples/inherit-example.txt
39
+ files:
40
+ - .gitignore
41
+ - History.txt
42
+ - README.txt
43
+ - examples/bench.rb
44
+ - examples/complex.rb
45
+ - examples/complex2.rb
46
+ - examples/cshadow-example.rb
47
+ - examples/cshadow-point.rb
48
+ - examples/ctest.rb
49
+ - examples/ctest2.rb
50
+ - examples/ctest3.rb
51
+ - examples/ctest4.rb
52
+ - examples/ctest5.rb
53
+ - examples/example-ruby-talk-30April2004.rb
54
+ - examples/fixed-array.rb
55
+ - examples/inherit-example.rb
56
+ - examples/inherit-example.txt
57
+ - examples/instance-eval.rb
58
+ - examples/ivset.rb
59
+ - examples/marshal-test.rb
60
+ - examples/matrix.rb
61
+ - examples/modular-def.rb
62
+ - examples/objattr.rb
63
+ - examples/opaque-struct-test.rb
64
+ - examples/sample.rb
65
+ - examples/struct.rb
66
+ - examples/test.rb
67
+ - examples/yaml.rb
68
+ - install.rb
69
+ - lib/cgen/attribute.rb
70
+ - lib/cgen/cgen.rb
71
+ - lib/cgen/cshadow.rb
72
+ - lib/cgen/inherit.rb
73
+ - rakefile
74
+ - tasks/ann.rake
75
+ - tasks/bones.rake
76
+ - tasks/gem.rake
77
+ - tasks/git.rake
78
+ - tasks/notes.rake
79
+ - tasks/post_load.rake
80
+ - tasks/rdoc.rake
81
+ - tasks/rubyforge.rake
82
+ - tasks/setup.rb
83
+ - tasks/spec.rake
84
+ - tasks/svn.rake
85
+ - tasks/test.rake
86
+ - tasks/zentest.rake
87
+ - test/test-attribute.rb
88
+ - test/test-cgen.rb
89
+ - test/test-cshadow.rb
90
+ - test/test.rb
91
+ has_rdoc: true
92
+ homepage: http://rubyforge.org/projects/cgen/
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options:
97
+ - --main
98
+ - README.txt
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: "0"
106
+ version:
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: "0"
112
+ version:
113
+ requirements: []
114
+
115
+ rubyforge_project: cgen
116
+ rubygems_version: 1.3.5
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Library for packed binary data stored in ruby Strings
120
+ test_files:
121
+ - test/test-attribute.rb
122
+ - test/test-cgen.rb
123
+ - test/test-cshadow.rb