sizes 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,26 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .idea
23
+ *.o
24
+ *.bundle
25
+ *.out
26
+ Makefile
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Colin MacKenzie IV
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ = sizes
2
+
3
+ A very simple gem that exposes the C *sizeof* keyword to Ruby.
4
+
5
+ This does _not_ check the memory footprint of a given Ruby object. I may implement that sometime in the future if it's
6
+ really needed, but for now this gem is only useful for checking primitive C types such as float, short, and so on.
7
+
8
+ I wrote this because I couldn't find any cross-platform way to get these sizes from within Ruby. If it exists, then
9
+ I've just reinvented the wheel, but at least I'm documenting it. :)
10
+
11
+ == Installation
12
+
13
+ gem install sizes
14
+
15
+ == Usage
16
+
17
+ require 'sizes'
18
+ import Sizes
19
+ sizeof(:short)
20
+ #=> 2
21
+
22
+ The *sizeof* method can also be called as a module function:
23
+
24
+ require 'sizes'
25
+ Sizes.sizeof(:float)
26
+ #=> 4
27
+
28
+ == More info
29
+
30
+ Arguments to the #sizeof method can be either symbols or strings, and must be one of the following supported types:
31
+
32
+ :char
33
+ :unsigned_char
34
+ :signed_char
35
+ :signed_int
36
+ :int
37
+ :signed_short_int
38
+ :signed_long_int
39
+ :long_int
40
+ :long
41
+ :unsigned_int
42
+ :unsigned
43
+ :unsigned_short_int
44
+ :unsigned_short
45
+ :unsigned_long_int
46
+ :unsigned_long
47
+ :float
48
+ :double
49
+ :long_double
50
+
51
+ As you can see, some types are represented more than once with varying names. This is purely for convenience.
52
+
53
+ If the argument does not appear in the above list, an ArgumentError will be raised.
54
+
55
+ == Note on Patches/Pull Requests
56
+
57
+ * Fork the project.
58
+ * Make your feature addition or bug fix.
59
+ * Add tests for it. This is important so I don't break it in a
60
+ future version unintentionally.
61
+ * Commit, do not mess with rakefile, version, or history.
62
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
63
+ * Send me a pull request. Bonus points for topic branches.
64
+
65
+ == Copyright
66
+
67
+ Copyright (c) 2010 Colin MacKenzie IV. See LICENSE for details.
68
+ http://thoughtsincomputation.com
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sizes"
8
+ gem.summary = %Q{A very simple gem that exposes the C *sizeof* keyword to Ruby.}
9
+ gem.description = %Q{A very simple gem that exposes the C *sizeof* keyword to Ruby.}
10
+ gem.email = "sinisterchipmunk@gmail.com"
11
+ gem.homepage = "http://thoughtsincomputation.com"
12
+ gem.authors = ["Colin MacKenzie IV"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.files.concat FileList['ext/**/*.c']
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "sizes #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile("sizes")
@@ -0,0 +1,32 @@
1
+ #include "ruby.h"
2
+
3
+ static VALUE sizeof_char(VALUE self) { return INT2FIX(sizeof(char)); }
4
+ static VALUE sizeof_unsigned_char(VALUE self) { return INT2FIX(sizeof(unsigned char)); }
5
+ static VALUE sizeof_signed_char(VALUE self) { return INT2FIX(sizeof(signed char)); }
6
+ static VALUE sizeof_signed_int(VALUE self) { return INT2FIX(sizeof(signed int)); }
7
+ static VALUE sizeof_signed_short_int(VALUE self) { return INT2FIX(sizeof(signed short int)); }
8
+ static VALUE sizeof_long_int(VALUE self) { return INT2FIX(sizeof(long int)); }
9
+ static VALUE sizeof_unsigned_int(VALUE self) { return INT2FIX(sizeof(unsigned int)); }
10
+ static VALUE sizeof_unsigned_short_int(VALUE self) { return INT2FIX(sizeof(unsigned short int)); }
11
+ static VALUE sizeof_unsigned_long_int(VALUE self) { return INT2FIX(sizeof(unsigned long int)); }
12
+ static VALUE sizeof_float(VALUE self) { return INT2FIX(sizeof(float)); }
13
+ static VALUE sizeof_double(VALUE self) { return INT2FIX(sizeof(double)); }
14
+ static VALUE sizeof_long_double(VALUE self) { return INT2FIX(sizeof(long double)); }
15
+
16
+ void Init_sizes()
17
+ {
18
+ VALUE mSizes = rb_define_module("Sizes");
19
+
20
+ rb_define_module_function(mSizes, "sizeof_char", sizeof_char, 0);
21
+ rb_define_module_function(mSizes, "sizeof_unsigned_char", sizeof_unsigned_char, 0);
22
+ rb_define_module_function(mSizes, "sizeof_signed_char", sizeof_signed_char, 0);
23
+ rb_define_module_function(mSizes, "sizeof_signed_int", sizeof_signed_int, 0);
24
+ rb_define_module_function(mSizes, "sizeof_signed_short_int", sizeof_signed_short_int, 0);
25
+ rb_define_module_function(mSizes, "sizeof_long_int", sizeof_long_int, 0);
26
+ rb_define_module_function(mSizes, "sizeof_unsigned_int", sizeof_unsigned_int, 0);
27
+ rb_define_module_function(mSizes, "sizeof_unsigned_short_int", sizeof_unsigned_short_int, 0);
28
+ rb_define_module_function(mSizes, "sizeof_unsigned_long_int", sizeof_unsigned_long_int, 0);
29
+ rb_define_module_function(mSizes, "sizeof_float", sizeof_float, 0);
30
+ rb_define_module_function(mSizes, "sizeof_double", sizeof_double, 0);
31
+ rb_define_module_function(mSizes, "sizeof_long_double", sizeof_long_double, 0);
32
+ }
@@ -0,0 +1,50 @@
1
+ require File.join(File.dirname(__FILE__), "../ext/sizes/sizes")
2
+
3
+ module Sizes
4
+ module_function
5
+
6
+ STRING_COPIES = %w(char unsigned_char signed_char signed_int int signed_short_int signed_long_int
7
+ long_int long unsigned_int unsigned unsigned_short_int unsigned_short unsigned_long_int
8
+ unsigned_long float double long_double
9
+ ).inject({}) { |hash,key| hash[key] = key.to_sym; hash }.freeze
10
+
11
+ def sizeof(symbol_or_string)
12
+ # I could just use send("sizeof_#{symbol_or_string}") but that would be slower, and negate any advantage of using
13
+ # symbols. Not sure how much the speed difference really matters, but it couldn't hurt, right?
14
+
15
+ case symbol_or_string
16
+ when String
17
+ if sym = STRING_COPIES[symbol_or_string]
18
+ sizeof(sym)
19
+ else
20
+ raise ArgumentError, "Argument must be one of #{STRING_COPIES.values.inspect}, or a String version of the same"
21
+ end
22
+ when :char
23
+ sizeof_char
24
+ when :unsigned_char
25
+ sizeof_unsigned_char
26
+ when :signed_char
27
+ sizeof_signed_char
28
+ when :signed_int, :int
29
+ sizeof_signed_int
30
+ when :signed_short_int, :short_int, :short
31
+ sizeof_signed_short_int
32
+ when :signed_long_int, :long_int, :long
33
+ sizeof_long_int
34
+ when :unsigned_int, :unsigned
35
+ sizeof_unsigned_int
36
+ when :unsigned_short_int, :unsigned_short
37
+ sizeof_unsigned_short_int
38
+ when :unsigned_long_int, :unsigned_long
39
+ sizeof_unsigned_long_int
40
+ when :float
41
+ sizeof_float
42
+ when :double
43
+ sizeof_double
44
+ when :long_double
45
+ sizeof_long_double
46
+ else
47
+ raise ArgumentError, "Argument must be one of #{STRING_COPIES.values.inspect}, or a String version of the same"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sizes}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Colin MacKenzie IV"]
12
+ s.date = %q{2010-10-27}
13
+ s.description = %q{A very simple gem that exposes the C *sizeof* keyword to Ruby.}
14
+ s.email = %q{sinisterchipmunk@gmail.com}
15
+ s.extensions = ["ext/sizes/extconf.rb"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "ext/sizes/extconf.rb",
28
+ "ext/sizes/sizes.c",
29
+ "lib/sizes.rb",
30
+ "sizes.gemspec",
31
+ "spec/sizes_spec.rb",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb",
34
+ "spec/support/dump_sizes.c"
35
+ ]
36
+ s.homepage = %q{http://thoughtsincomputation.com}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.6}
40
+ s.summary = %q{A very simple gem that exposes the C *sizeof* keyword to Ruby.}
41
+ s.test_files = [
42
+ "spec/sizes_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ end
58
+ end
59
+
@@ -0,0 +1,86 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ # These tests describe correct values for my machine. YMMV.
4
+ # To check correct values for your machine, compile and run spec/support/dump_sizes.cpp.
5
+
6
+ describe "Sizes" do
7
+ include Sizes
8
+
9
+ it "should work as a module function" do
10
+ Sizes.sizeof(:char).should == 1
11
+ end
12
+
13
+ context "should report correct size:" do
14
+ it "char" do
15
+ sizeof(:char).should == 1
16
+ end
17
+
18
+ it "unsigned char" do
19
+ sizeof(:unsigned_char).should == 1
20
+ end
21
+
22
+ it "signed char" do
23
+ sizeof(:signed_char).should == 1
24
+ end
25
+
26
+ it "signed int" do
27
+ sizeof(:signed_int).should == 4
28
+ end
29
+
30
+ it "int" do
31
+ sizeof(:int).should == 4
32
+ end
33
+
34
+ it "signed short int" do
35
+ sizeof(:signed_short_int).should == 2
36
+ end
37
+
38
+ it "signed long int" do
39
+ sizeof(:signed_long_int).should == 8
40
+ end
41
+
42
+ it "long int" do
43
+ sizeof(:long_int).should == 8
44
+ end
45
+
46
+ it "long" do
47
+ sizeof(:long).should == 8
48
+ end
49
+
50
+ it "unsigned int" do
51
+ sizeof(:unsigned_int).should == 4
52
+ end
53
+
54
+ it "unsigned" do
55
+ sizeof(:unsigned).should == 4
56
+ end
57
+
58
+ it "unsigned short int" do
59
+ sizeof(:unsigned_short_int).should == 2
60
+ end
61
+
62
+ it "unsigned short" do
63
+ sizeof(:unsigned_short).should == 2
64
+ end
65
+
66
+ it "unsigned long int" do
67
+ sizeof(:unsigned_long_int).should == 8
68
+ end
69
+
70
+ it "unsigned long" do
71
+ sizeof(:unsigned_long).should == 8
72
+ end
73
+
74
+ it "float" do
75
+ sizeof(:float).should == 4
76
+ end
77
+
78
+ it "double" do
79
+ sizeof(:double).should == 8
80
+ end
81
+
82
+ it "long double" do
83
+ sizeof(:long_double).should == 16
84
+ end
85
+ end
86
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'sizes'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,27 @@
1
+ #include <stdio.h>
2
+
3
+ int main(int argc, char **argv)
4
+ {
5
+ printf("Correct values for sizeof(...) for this machine:\n\n");
6
+ printf("char : %ld\n", sizeof(char));
7
+ printf("unsigned char : %ld\n", sizeof(unsigned char));
8
+ printf("signed char : %ld\n", sizeof(signed char));
9
+ printf("signed int : %ld\n", sizeof(int));
10
+ printf(" / int\n");
11
+ printf("signed short int : %ld\n", sizeof(signed short int));
12
+ printf(" / short int\n");
13
+ printf(" / short\n");
14
+ printf("signed long int : %ld\n", sizeof(signed long int));
15
+ printf(" / long int\n");
16
+ printf(" / long\n");
17
+ printf("unsigned int : %ld\n", sizeof(unsigned int));
18
+ printf(" / unsigned\n");
19
+ printf("unsigned short int : %ld\n", sizeof(unsigned short int));
20
+ printf(" / unsigned short\n");
21
+ printf("unsigned long int : %ld\n", sizeof(unsigned long int));
22
+ printf(" / unsigned long\n");
23
+ printf("float : %ld\n", sizeof(float));
24
+ printf("double : %ld\n", sizeof(double));
25
+ printf("long double : %ld\n", sizeof(long double));
26
+ }
27
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sizes
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Colin MacKenzie IV
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-27 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: A very simple gem that exposes the C *sizeof* keyword to Ruby.
35
+ email: sinisterchipmunk@gmail.com
36
+ executables: []
37
+
38
+ extensions:
39
+ - ext/sizes/extconf.rb
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - ext/sizes/extconf.rb
51
+ - ext/sizes/sizes.c
52
+ - lib/sizes.rb
53
+ - sizes.gemspec
54
+ - spec/sizes_spec.rb
55
+ - spec/spec.opts
56
+ - spec/spec_helper.rb
57
+ - spec/support/dump_sizes.c
58
+ has_rdoc: true
59
+ homepage: http://thoughtsincomputation.com
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A very simple gem that exposes the C *sizeof* keyword to Ruby.
88
+ test_files:
89
+ - spec/sizes_spec.rb
90
+ - spec/spec_helper.rb