sizes 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,7 +47,37 @@ Arguments to the #sizeof method can be either symbols or strings, and must be on
47
47
  :float
48
48
  :double
49
49
  :long_double
50
-
50
+ :int8
51
+ :int16
52
+ :int32
53
+ :int64
54
+ :uint8
55
+ :uint16
56
+ :uint32
57
+ :uint64
58
+ :int_least8
59
+ :int_least16
60
+ :int_least32
61
+ :int_least64
62
+ :uint_least8
63
+ :uint_least16
64
+ :uint_least32
65
+ :uint_least64
66
+ :int_fast8
67
+ :int_fast16
68
+ :int_fast32
69
+ :int_fast64
70
+ :uint_fast8
71
+ :uint_fast16
72
+ :uint_fast32
73
+ :uint_fast64
74
+ :intptr
75
+ :uintptr
76
+ :intmax
77
+ :uintmax
78
+ :size_t, :size
79
+ :wchar_t, :wchar
80
+
51
81
  As you can see, some types are represented more than once with varying names. This is purely for convenience.
52
82
 
53
83
  If the argument does not appear in the above list, an ArgumentError will be raised.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -1,3 +1,7 @@
1
1
  require 'mkmf'
2
2
 
3
+ have_header("stddef.h")
4
+ have_header("stdlib.h")
5
+ have_header("stdint.h")
6
+
3
7
  create_makefile("sizes")
@@ -1,5 +1,7 @@
1
1
  #include "ruby.h"
2
2
 
3
+ /* I almost wonder if I should have just generated this file with Ruby. Ah, well... */
4
+
3
5
  static VALUE sizeof_char(VALUE self) { return INT2FIX(sizeof(char)); }
4
6
  static VALUE sizeof_unsigned_char(VALUE self) { return INT2FIX(sizeof(unsigned char)); }
5
7
  static VALUE sizeof_signed_char(VALUE self) { return INT2FIX(sizeof(signed char)); }
@@ -13,6 +15,46 @@ static VALUE sizeof_float(VALUE self) { return INT2FIX(sizeof(float
13
15
  static VALUE sizeof_double(VALUE self) { return INT2FIX(sizeof(double)); }
14
16
  static VALUE sizeof_long_double(VALUE self) { return INT2FIX(sizeof(long double)); }
15
17
 
18
+ /* C99 types */
19
+ #if HAVE_STDDEF_H || HAVE_STDLIB_H
20
+ static VALUE sizeof_size(VALUE self) { return INT2FIX(sizeof(size_t)); }
21
+ static VALUE sizeof_wchar(VALUE self) { return INT2FIX(sizeof(wchar_t)); }
22
+ #endif
23
+
24
+ #if HAVE_INTTYPES_H
25
+ static VALUE sizeof_int8(VALUE self) { return INT2FIX(sizeof(int8_t)); }
26
+ static VALUE sizeof_int16(VALUE self) { return INT2FIX(sizeof(int16_t)); }
27
+ static VALUE sizeof_int32(VALUE self) { return INT2FIX(sizeof(int32_t)); }
28
+ static VALUE sizeof_int64(VALUE self) { return INT2FIX(sizeof(int64_t)); }
29
+ static VALUE sizeof_uint8(VALUE self) { return INT2FIX(sizeof(uint8_t)); }
30
+ static VALUE sizeof_uint16(VALUE self) { return INT2FIX(sizeof(uint16_t)); }
31
+ static VALUE sizeof_uint32(VALUE self) { return INT2FIX(sizeof(uint32_t)); }
32
+ static VALUE sizeof_uint64(VALUE self) { return INT2FIX(sizeof(uint64_t)); }
33
+
34
+ static VALUE sizeof_int_least8(VALUE self) { return INT2FIX(sizeof(int_least8_t)); }
35
+ static VALUE sizeof_int_least16(VALUE self) { return INT2FIX(sizeof(int_least16_t)); }
36
+ static VALUE sizeof_int_least32(VALUE self) { return INT2FIX(sizeof(int_least32_t)); }
37
+ static VALUE sizeof_int_least64(VALUE self) { return INT2FIX(sizeof(int_least64_t)); }
38
+ static VALUE sizeof_uint_least8(VALUE self) { return INT2FIX(sizeof(uint_least8_t)); }
39
+ static VALUE sizeof_uint_least16(VALUE self) { return INT2FIX(sizeof(uint_least16_t)); }
40
+ static VALUE sizeof_uint_least32(VALUE self) { return INT2FIX(sizeof(uint_least32_t)); }
41
+ static VALUE sizeof_uint_least64(VALUE self) { return INT2FIX(sizeof(uint_least64_t)); }
42
+
43
+ static VALUE sizeof_int_fast8(VALUE self) { return INT2FIX(sizeof(int_fast8_t)); }
44
+ static VALUE sizeof_int_fast16(VALUE self) { return INT2FIX(sizeof(int_fast16_t)); }
45
+ static VALUE sizeof_int_fast32(VALUE self) { return INT2FIX(sizeof(int_fast32_t)); }
46
+ static VALUE sizeof_int_fast64(VALUE self) { return INT2FIX(sizeof(int_fast64_t)); }
47
+ static VALUE sizeof_uint_fast8(VALUE self) { return INT2FIX(sizeof(uint_fast8_t)); }
48
+ static VALUE sizeof_uint_fast16(VALUE self) { return INT2FIX(sizeof(uint_fast16_t)); }
49
+ static VALUE sizeof_uint_fast32(VALUE self) { return INT2FIX(sizeof(uint_fast32_t)); }
50
+ static VALUE sizeof_uint_fast64(VALUE self) { return INT2FIX(sizeof(uint_fast64_t)); }
51
+
52
+ static VALUE sizeof_intptr(VALUE self) { return INT2FIX(sizeof(intptr_t)); }
53
+ static VALUE sizeof_uintptr(VALUE self) { return INT2FIX(sizeof(uintptr_t)); }
54
+ static VALUE sizeof_intmax(VALUE self) { return INT2FIX(sizeof(intmax_t)); }
55
+ static VALUE sizeof_uintmax(VALUE self) { return INT2FIX(sizeof(uintmax_t)); }
56
+ #endif
57
+
16
58
  void Init_sizes()
17
59
  {
18
60
  VALUE mSizes = rb_define_module("Sizes");
@@ -29,4 +71,44 @@ void Init_sizes()
29
71
  rb_define_module_function(mSizes, "sizeof_float", sizeof_float, 0);
30
72
  rb_define_module_function(mSizes, "sizeof_double", sizeof_double, 0);
31
73
  rb_define_module_function(mSizes, "sizeof_long_double", sizeof_long_double, 0);
74
+
75
+ /* C99 types */
76
+ #if HAVE_STDDEF_H || HAVE_STDLIB_H
77
+ rb_define_module_function(mSizes, "sizeof_size", sizeof_size, 0);
78
+ rb_define_module_function(mSizes, "sizeof_wchar", sizeof_wchar, 0);
79
+ #endif
80
+
81
+ #if HAVE_STDINT_H
82
+ rb_define_module_function(mSizes, "sizeof_int8", sizeof_int8, 0);
83
+ rb_define_module_function(mSizes, "sizeof_int16", sizeof_int16, 0);
84
+ rb_define_module_function(mSizes, "sizeof_int32", sizeof_int32, 0);
85
+ rb_define_module_function(mSizes, "sizeof_int64", sizeof_int64, 0);
86
+ rb_define_module_function(mSizes, "sizeof_uint8", sizeof_uint8, 0);
87
+ rb_define_module_function(mSizes, "sizeof_uint16", sizeof_uint16, 0);
88
+ rb_define_module_function(mSizes, "sizeof_uint32", sizeof_uint32, 0);
89
+ rb_define_module_function(mSizes, "sizeof_uint64", sizeof_uint64, 0);
90
+
91
+ rb_define_module_function(mSizes, "sizeof_int_least8", sizeof_int_least8, 0);
92
+ rb_define_module_function(mSizes, "sizeof_int_least16", sizeof_int_least16, 0);
93
+ rb_define_module_function(mSizes, "sizeof_int_least32", sizeof_int_least32, 0);
94
+ rb_define_module_function(mSizes, "sizeof_int_least64", sizeof_int_least64, 0);
95
+ rb_define_module_function(mSizes, "sizeof_uint_least8", sizeof_uint_least8, 0);
96
+ rb_define_module_function(mSizes, "sizeof_uint_least16",sizeof_uint_least16,0);
97
+ rb_define_module_function(mSizes, "sizeof_uint_least32",sizeof_uint_least32,0);
98
+ rb_define_module_function(mSizes, "sizeof_uint_least64",sizeof_uint_least64,0);
99
+
100
+ rb_define_module_function(mSizes, "sizeof_int_fast8", sizeof_int_fast8, 0);
101
+ rb_define_module_function(mSizes, "sizeof_int_fast16", sizeof_int_fast16, 0);
102
+ rb_define_module_function(mSizes, "sizeof_int_fast32", sizeof_int_fast32, 0);
103
+ rb_define_module_function(mSizes, "sizeof_int_fast64", sizeof_int_fast64, 0);
104
+ rb_define_module_function(mSizes, "sizeof_uint_fast8", sizeof_uint_fast8, 0);
105
+ rb_define_module_function(mSizes, "sizeof_uint_fast16", sizeof_uint_fast16, 0);
106
+ rb_define_module_function(mSizes, "sizeof_uint_fast32", sizeof_uint_fast32, 0);
107
+ rb_define_module_function(mSizes, "sizeof_uint_fast64", sizeof_uint_fast64, 0);
108
+
109
+ rb_define_module_function(mSizes, "sizeof_intptr", sizeof_intptr, 0);
110
+ rb_define_module_function(mSizes, "sizeof_uintptr", sizeof_uintptr, 0);
111
+ rb_define_module_function(mSizes, "sizeof_intmax", sizeof_intmax, 0);
112
+ rb_define_module_function(mSizes, "sizeof_uintmax", sizeof_uintmax, 0);
113
+ #endif
32
114
  }
@@ -2,10 +2,15 @@ require File.join(File.dirname(__FILE__), "../ext/sizes/sizes")
2
2
 
3
3
  module Sizes
4
4
  module_function
5
+ C99_TYPES = %w(int8 int16 int32 int64 uint8 uint16 uint32 uint64 int_least8 int_least16 int_least32
6
+ int_least64 uint_least8 uint_least16 uint_least32 uint_least64 int_fast8 int_fast16 int_fast32
7
+ int_fast64 uint_fast8 uint_fast16 uint_fast32 uint_fast64 intptr uintptr intmax uintmax size
8
+ wchar)
5
9
 
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
10
+ STRING_COPIES = (
11
+ %w(char unsigned_char signed_char signed_int int signed_short_int signed_long_int
12
+ long_int long unsigned_int unsigned unsigned_short_int unsigned_short unsigned_long_int
13
+ unsigned_long float double long_double) + C99_TYPES
9
14
  ).inject({}) { |hash,key| hash[key] = key.to_sym; hash }.freeze
10
15
 
11
16
  def sizeof(symbol_or_string)
@@ -19,30 +24,48 @@ module Sizes
19
24
  else
20
25
  raise ArgumentError, "Argument must be one of #{STRING_COPIES.values.inspect}, or a String version of the same"
21
26
  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
27
+ when :char then sizeof_char
28
+ when :unsigned_char then sizeof_unsigned_char
29
+ when :signed_char then sizeof_signed_char
30
+ when :signed_int, :int then sizeof_signed_int
31
+ when :signed_short_int, :short_int, :short then sizeof_signed_short_int
32
+ when :signed_long_int, :long_int, :long then sizeof_long_int
33
+ when :unsigned_int, :unsigned then sizeof_unsigned_int
34
+ when :unsigned_short_int, :unsigned_short then sizeof_unsigned_short_int
35
+ when :unsigned_long_int, :unsigned_long then sizeof_unsigned_long_int
36
+ when :float then sizeof_float
37
+ when :double then sizeof_double
38
+ when :long_double then sizeof_long_double
39
+ when :int8 then sizeof_int8
40
+ when :int16 then sizeof_int16
41
+ when :int32 then sizeof_int32
42
+ when :int64 then sizeof_int64
43
+ when :uint8 then sizeof_uint8
44
+ when :uint16 then sizeof_uint16
45
+ when :uint32 then sizeof_uint32
46
+ when :uint64 then sizeof_uint64
47
+ when :int_least8 then sizeof_int_least8
48
+ when :int_least16 then sizeof_int_least16
49
+ when :int_least32 then sizeof_int_least32
50
+ when :int_least64 then sizeof_int_least64
51
+ when :uint_least8 then sizeof_uint_least8
52
+ when :uint_least16 then sizeof_uint_least16
53
+ when :uint_least32 then sizeof_uint_least32
54
+ when :uint_least64 then sizeof_uint_least64
55
+ when :int_fast8 then sizeof_int_fast8
56
+ when :int_fast16 then sizeof_int_fast16
57
+ when :int_fast32 then sizeof_int_fast32
58
+ when :int_fast64 then sizeof_int_fast64
59
+ when :uint_fast8 then sizeof_uint_fast8
60
+ when :uint_fast16 then sizeof_uint_fast16
61
+ when :uint_fast32 then sizeof_uint_fast32
62
+ when :uint_fast64 then sizeof_uint_fast64
63
+ when :intptr then sizeof_intptr
64
+ when :uintptr then sizeof_uintptr
65
+ when :intmax then sizeof_intmax
66
+ when :uintmax then sizeof_uintmax
67
+ when :size_t, :size then sizeof_size
68
+ when :wchar_t, :wchar then sizeof_wchar
46
69
  else
47
70
  raise ArgumentError, "Argument must be one of #{STRING_COPIES.values.inspect}, or a String version of the same"
48
71
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sizes}
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Colin MacKenzie IV"]
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "ext/sizes/sizes.c",
29
29
  "lib/sizes.rb",
30
30
  "sizes.gemspec",
31
+ "spec/dynamic_sizes_spec.rb",
31
32
  "spec/sizes_spec.rb",
32
33
  "spec/spec.opts",
33
34
  "spec/spec_helper.rb",
@@ -39,7 +40,8 @@ Gem::Specification.new do |s|
39
40
  s.rubygems_version = %q{1.3.6}
40
41
  s.summary = %q{A very simple gem that exposes the C *sizeof* keyword to Ruby.}
41
42
  s.test_files = [
42
- "spec/sizes_spec.rb",
43
+ "spec/dynamic_sizes_spec.rb",
44
+ "spec/sizes_spec.rb",
43
45
  "spec/spec_helper.rb"
44
46
  ]
45
47
 
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ # These tests iterate through all known types (see Sizes#STRING_COPIES) and compile a small C program
4
+ # for each; the C program prints out the size of the type for this machine, and the test passes
5
+ # if the sizes match.
6
+
7
+ describe "Sizes" do
8
+ # inline hashes the method names so that they aren't double-compiled -- but this sucks if
9
+ # we want to reuse the same method name, so instead we'll tack a global counter onto
10
+ # the end of the method name to ensure they're all unique.
11
+ $inline_counter = 0
12
+
13
+ begin
14
+ require "inline"
15
+ include Sizes
16
+
17
+ def real_sizeof(type_name)
18
+ sizeof_test = Class.new do
19
+ inline :C do |builder|
20
+ builder.include "<stdint.h>"
21
+ builder.c <<-end_c_code
22
+ int size#{$inline_counter += 1}()
23
+ {
24
+ int size = sizeof(#{type_name});
25
+ return size;
26
+ }
27
+ end_c_code
28
+ end
29
+ end
30
+
31
+ sizeof_test.new.send(:"size#{$inline_counter}")
32
+ end
33
+
34
+ (Sizes::STRING_COPIES.keys - Sizes::C99_TYPES).each do |type_name|
35
+ it "should generate correct size for :#{type_name}" do
36
+ sizeof(type_name).should == real_sizeof(type_name.gsub(/_/, ' '))
37
+ end
38
+ end
39
+
40
+ Sizes::C99_TYPES.each do |type_name|
41
+ it "should generate correct size for C99 type :#{type_name}" do
42
+ sizeof(type_name).should == real_sizeof("#{type_name}_t")
43
+ end
44
+ end
45
+ rescue LoadError
46
+ it "doesn't have RubyInline" do
47
+ fail "Can't run dynamic test suite without the RubyInline gem"
48
+ end
49
+ end
50
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
+ - 1
7
8
  - 0
8
- - 0
9
- version: 1.0.0
9
+ version: 1.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Colin MacKenzie IV
@@ -51,6 +51,7 @@ files:
51
51
  - ext/sizes/sizes.c
52
52
  - lib/sizes.rb
53
53
  - sizes.gemspec
54
+ - spec/dynamic_sizes_spec.rb
54
55
  - spec/sizes_spec.rb
55
56
  - spec/spec.opts
56
57
  - spec/spec_helper.rb
@@ -86,5 +87,6 @@ signing_key:
86
87
  specification_version: 3
87
88
  summary: A very simple gem that exposes the C *sizeof* keyword to Ruby.
88
89
  test_files:
90
+ - spec/dynamic_sizes_spec.rb
89
91
  - spec/sizes_spec.rb
90
92
  - spec/spec_helper.rb