ent 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ == 0.1.0 / 2011-03-21
2
+ * Official initial release
3
+
4
+ == 0.0.2 / 2011-03-10
5
+ * Initial version published.
6
+
data/Rakefile CHANGED
@@ -36,7 +36,7 @@ CLEAN.include("coverage/")
36
36
 
37
37
  require 'rspec/core'
38
38
  require 'rspec/core/rake_task'
39
- RSpec::Core::RakeTask.new(:spec) do |spec|
39
+ RSpec::Core::RakeTask.new(:spec => :compile ) do |spec|
40
40
  spec.pattern = FileList['spec/**/*_spec.rb']
41
41
  end
42
42
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.1.0
@@ -1,8 +1,8 @@
1
1
  #include "ruby.h"
2
2
 
3
- #include "randtest.h"
4
- #include "iso8859.h"
3
+ #include <stdio.h>
5
4
 
5
+ #include "randtest.h"
6
6
 
7
7
  #ifndef DBL2NUM
8
8
  #define DBL2NUM(dbl) rb_float_new(dbl)
@@ -78,21 +78,33 @@ VALUE rb_rt_read_string(VALUE self, VALUE rb_buf) {
78
78
  rb_raise(rb_eIOError, "data cannot be added after finalizing");
79
79
 
80
80
  rt_add(ctx, RSTRING_PTR(rb_buf), RSTRING_LEN(rb_buf));
81
+
81
82
  return LONG2NUM(RSTRING_LEN(rb_buf));
82
83
  }
83
84
 
84
85
 
85
86
  VALUE rb_rt_read_file(VALUE self, VALUE rb_filename) {
86
87
  rt_ctx *ctx;
88
+ FILE *fp;
89
+ int oc;
90
+ size_t fsz = 0;
87
91
 
88
92
  Check_Type(rb_filename, T_STRING);
89
-
90
93
  Data_Get_Struct(self, rt_ctx, ctx);
91
94
  if(ctx->ended)
92
95
  rb_raise(rb_eIOError, "data cannot be added after finalizing");
93
96
 
94
- // todo ...
95
- return Qnil;
97
+ if ((fp = fopen(RSTRING_PTR(rb_filename), "rb")) == NULL)
98
+ rb_sys_fail(0);
99
+
100
+ while((oc=fgetc(fp)) != EOF) {
101
+ unsigned char ocb = (unsigned char) oc;
102
+ rt_add(ctx, &ocb, 1);
103
+ fsz++;
104
+ }
105
+ fclose(fp);
106
+
107
+ return LONG2NUM(fsz);
96
108
  }
97
109
 
98
110
 
@@ -109,6 +121,7 @@ VALUE rb_rt_entropy(VALUE self) {
109
121
 
110
122
  VALUE rb_rt_mean(VALUE self) {
111
123
  rt_ctx *ctx;
124
+
112
125
  Data_Get_Struct(self, rt_ctx, ctx);
113
126
 
114
127
  if(ctx->ended)
@@ -128,15 +141,25 @@ VALUE rb_rt_chisquare(VALUE self) {
128
141
  return Qnil;
129
142
  }
130
143
 
131
-
132
- VALUE rb_rt_datasum(VALUE self) {
144
+ VALUE rb_rt_chisquare_probability(VALUE self) {
133
145
  rt_ctx *ctx;
146
+ double ret, chip;
147
+
134
148
  Data_Get_Struct(self, rt_ctx, ctx);
135
149
 
136
- if(ctx->ended)
137
- return DBL2NUM(ctx->r_datasum);
138
- else
150
+ if(! ctx->ended)
139
151
  return Qnil;
152
+
153
+ chip = pochisq(ctx->r_chisq, (ctx->binary ? 1 : 255));
154
+
155
+ if (chip < 0.0001)
156
+ ret = 0.009;
157
+ else if (chip > 0.9999)
158
+ ret = 99.991;
159
+ else
160
+ ret = chip * 100;
161
+
162
+ return DBL2NUM(ret);
140
163
  }
141
164
 
142
165
 
@@ -193,15 +216,25 @@ VALUE rb_rt_chisquare_force(VALUE self) {
193
216
  return DBL2NUM(ctx->r_chisq);
194
217
  }
195
218
 
196
-
197
- VALUE rb_rt_datasum_force(VALUE self) {
219
+ VALUE rb_rt_chisquare_probability_force(VALUE self) {
198
220
  rt_ctx *ctx;
221
+ double ret, chip;
222
+
199
223
  Data_Get_Struct(self, rt_ctx, ctx);
200
224
 
201
225
  if(! ctx->ended)
202
226
  rt_end(ctx);
203
227
 
204
- return DBL2NUM(ctx->r_datasum);
228
+ chip = pochisq(ctx->r_chisq, (ctx->binary ? 1 : 255));
229
+
230
+ if (chip < 0.0001)
231
+ ret = 0.009;
232
+ else if (chip > 0.9999)
233
+ ret = 99.991;
234
+ else
235
+ ret = chip * 100;
236
+
237
+ return DBL2NUM(ret);
205
238
  }
206
239
 
207
240
 
@@ -239,7 +272,6 @@ VALUE rb_rt_result(VALUE self) {
239
272
  rb_hash_aset(ret, ID2SYM(rb_intern("entropy")), DBL2NUM(ctx->r_ent));
240
273
  rb_hash_aset(ret, ID2SYM(rb_intern("mean")), DBL2NUM(ctx->r_mean));
241
274
  rb_hash_aset(ret, ID2SYM(rb_intern("chisquare")), DBL2NUM(ctx->r_chisq));
242
- rb_hash_aset(ret, ID2SYM(rb_intern("datasum")), DBL2NUM(ctx->r_datasum));
243
275
  rb_hash_aset(ret, ID2SYM(rb_intern("montepi")), DBL2NUM(ctx->r_montepicalc));
244
276
  rb_hash_aset(ret, ID2SYM(rb_intern("scc")), DBL2NUM(ctx->r_scc));
245
277
  return ret;
@@ -256,20 +288,20 @@ void Init_ent_native() {
256
288
  rb_define_method(c_random_test, "binary?", rb_rt_get_binmode, 0);
257
289
 
258
290
  rb_define_method(c_random_test, "read", rb_rt_read_string, 1);
259
- // rb_define_method(c_random_test, "read_string", rb_rt_read_string, 1);
260
- // rb_define_method(c_random_test, "read_file", rb_rt_read_file, 1);
291
+ rb_define_method(c_random_test, "read_string", rb_rt_read_string, 1);
292
+ rb_define_method(c_random_test, "read_file", rb_rt_read_file, 1);
261
293
  rb_define_method(c_random_test, "finalize", rb_rt_final, 0);
262
294
  rb_define_method(c_random_test, "result", rb_rt_result, 0);
263
295
  rb_define_method(c_random_test, "entropy", rb_rt_entropy, 0);
264
296
  rb_define_method(c_random_test, "mean", rb_rt_mean, 0);
265
297
  rb_define_method(c_random_test, "chisquare", rb_rt_chisquare, 0);
266
- rb_define_method(c_random_test, "datasum", rb_rt_datasum, 0);
298
+ rb_define_method(c_random_test, "chisquare_probability", rb_rt_chisquare_probability, 0);
267
299
  rb_define_method(c_random_test, "montepi", rb_rt_montepi, 0);
268
300
  rb_define_method(c_random_test, "scc", rb_rt_scc, 0);
269
301
  rb_define_method(c_random_test, "entropy!", rb_rt_entropy_force, 0);
270
302
  rb_define_method(c_random_test, "mean!", rb_rt_mean_force, 0);
271
303
  rb_define_method(c_random_test, "chisquare!", rb_rt_chisquare_force, 0);
272
- rb_define_method(c_random_test, "datasum!", rb_rt_datasum_force, 0);
304
+ rb_define_method(c_random_test, "chisquare_probability!", rb_rt_chisquare_probability_force, 0);
273
305
  rb_define_method(c_random_test, "montepi!", rb_rt_montepi_force, 0);
274
306
  rb_define_method(c_random_test, "scc!", rb_rt_scc_force, 0);
275
307
  }
@@ -1,7 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "Ent" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
3
+ describe Ent do
4
+
7
5
  end
@@ -0,0 +1,109 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Ent::RandomTest do
4
+ context "byte entropy" do
5
+ before :each do
6
+ @rt = Ent::RandomTest.new
7
+ end
8
+
9
+ it "should not have binary testing enabled" do
10
+ @rt.should_not be_binary
11
+ end
12
+
13
+ it "should initialize correctly" do
14
+ @rt.should be_kind_of Ent::RandomTest
15
+ @rt.entropy.should be_nil
16
+ @rt.mean.should be_nil
17
+ @rt.chisquare.should be_nil
18
+ @rt.scc.should be_nil
19
+ end
20
+
21
+ it "should default with no randomness" do
22
+ @rt.should be_kind_of Ent::RandomTest
23
+ @rt.entropy.should be_nil
24
+ @rt.mean.should be_nil
25
+ @rt.chisquare.should be_nil
26
+ @rt.chisquare_probability.should be_nil
27
+ @rt.scc.should be_nil
28
+
29
+ @rt.finalize
30
+
31
+ @rt.entropy.should == 0
32
+ @rt.chisquare_probability.should > 99.99
33
+ @rt.mean.should be_nan
34
+ @rt.chisquare.should be_nan
35
+ end
36
+
37
+ it "should read a string and calculate its entropy" do
38
+ @rt.read_string("foo").should == 3
39
+ @rt.finalize
40
+ (0.918295 .. 0.918297).should be_include(@rt.entropy)
41
+ (423.66 .. 423.67).should be_include(@rt.chisquare)
42
+ (108.0000 .. 108.0001).should be_include(@rt.mean)
43
+ @rt.chisquare_probability.should > 99.99
44
+ end
45
+
46
+ it "should read a file and calculate its entropy" do
47
+ @rt.read_file(sample_file("foo_test")).should == 3
48
+ @rt.finalize
49
+ (0.918295 .. 0.918297).should be_include(@rt.entropy)
50
+ (423.66 .. 423.67).should be_include(@rt.chisquare)
51
+ (108.0000 .. 108.0001).should be_include(@rt.mean)
52
+ @rt.chisquare_probability.should > 99.99
53
+ end
54
+
55
+ end
56
+
57
+ context "binary entropy" do
58
+ before :each do
59
+ @rt = Ent::RandomTest.new(true)
60
+ end
61
+
62
+
63
+ it "should have binary testing enabled" do
64
+ @rt.should be_binary
65
+ end
66
+
67
+ it "should initialize correctly" do
68
+ @rt.should be_kind_of Ent::RandomTest
69
+ @rt.entropy.should be_nil
70
+ @rt.mean.should be_nil
71
+ @rt.chisquare.should be_nil
72
+ @rt.scc.should be_nil
73
+ end
74
+
75
+ it "should default with no randomness" do
76
+ @rt.should be_kind_of Ent::RandomTest
77
+ @rt.entropy.should be_nil
78
+ @rt.mean.should be_nil
79
+ @rt.chisquare.should be_nil
80
+ @rt.scc.should be_nil
81
+
82
+ @rt.finalize
83
+
84
+ @rt.entropy.should == 0
85
+ @rt.chisquare_probability.should < 0.01
86
+ @rt.mean.should be_nan
87
+ @rt.chisquare.should be_nan
88
+ end
89
+
90
+ it "should read a string and calculate its entropy" do
91
+ @rt.read_string("foo").should == 3
92
+ @rt.finalize
93
+ (0.918295 .. 0.918297).should be_include(@rt.entropy)
94
+ (2.66 .. 2.67).should be_include(@rt.chisquare)
95
+ (0.6666 .. 0.6667).should be_include(@rt.mean)
96
+ @rt.chisquare_probability.should < 0.01
97
+ end
98
+
99
+ it "should read a file and calculate its entropy" do
100
+ @rt.read_file(sample_file("foo_test")).should == 3
101
+ @rt.finalize
102
+ (0.918295 .. 0.918297).should be_include(@rt.entropy)
103
+ (2.66 .. 2.67).should be_include(@rt.chisquare)
104
+ (0.6666 .. 0.6667).should be_include(@rt.mean)
105
+ @rt.chisquare_probability.should < 0.01
106
+ end
107
+ end
108
+ end
109
+
@@ -0,0 +1 @@
1
+ foo
@@ -1,6 +1,9 @@
1
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
1
+ require 'pathname'
3
2
  require 'rspec'
3
+ SPEC_DIR = Pathname.new(__FILE__).dirname
4
+ $LOAD_PATH.unshift(SPEC_DIR.join('..', 'lib').to_s)
5
+ $LOAD_PATH.unshift(SPEC_DIR.to_s)
6
+
4
7
  require 'ent'
5
8
 
6
9
  # Requires supporting files with custom matchers and macros, etc,
@@ -10,3 +13,12 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
13
  RSpec.configure do |config|
11
14
 
12
15
  end
16
+
17
+ def sample_path(fname)
18
+ Pathname.new(__FILE__).dirname.join("samples", fname)
19
+ end
20
+
21
+ def sample_file(fname)
22
+ sample_path(fname).to_s
23
+ end
24
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 1
7
8
  - 0
8
- - 2
9
- version: 0.0.2
9
+ version: 0.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Eric Monti
@@ -14,13 +14,13 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-10 00:00:00 -06:00
17
+ date: 2011-03-21 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
22
- prerelease: false
23
22
  requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
24
  requirements:
25
25
  - - ~>
26
26
  - !ruby/object:Gem::Version
@@ -30,11 +30,12 @@ dependencies:
30
30
  - 0
31
31
  version: 2.3.0
32
32
  type: :development
33
+ prerelease: false
33
34
  version_requirements: *id001
34
35
  - !ruby/object:Gem::Dependency
35
36
  name: bundler
36
- prerelease: false
37
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
38
39
  requirements:
39
40
  - - ~>
40
41
  - !ruby/object:Gem::Version
@@ -44,11 +45,12 @@ dependencies:
44
45
  - 0
45
46
  version: 1.0.0
46
47
  type: :development
48
+ prerelease: false
47
49
  version_requirements: *id002
48
50
  - !ruby/object:Gem::Dependency
49
51
  name: jeweler
50
- prerelease: false
51
52
  requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
52
54
  requirements:
53
55
  - - ~>
54
56
  - !ruby/object:Gem::Version
@@ -58,11 +60,12 @@ dependencies:
58
60
  - 2
59
61
  version: 1.5.2
60
62
  type: :development
63
+ prerelease: false
61
64
  version_requirements: *id003
62
65
  - !ruby/object:Gem::Dependency
63
66
  name: rcov
64
- prerelease: false
65
67
  requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
66
69
  requirements:
67
70
  - - ">="
68
71
  - !ruby/object:Gem::Version
@@ -70,11 +73,12 @@ dependencies:
70
73
  - 0
71
74
  version: "0"
72
75
  type: :development
76
+ prerelease: false
73
77
  version_requirements: *id004
74
78
  - !ruby/object:Gem::Dependency
75
79
  name: rake-compiler
76
- prerelease: false
77
80
  requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
78
82
  requirements:
79
83
  - - ">="
80
84
  - !ruby/object:Gem::Version
@@ -82,6 +86,7 @@ dependencies:
82
86
  - 0
83
87
  version: "0"
84
88
  type: :development
89
+ prerelease: false
85
90
  version_requirements: *id005
86
91
  description: Calculate the entropy of data
87
92
  email: esmonti@gmail.com
@@ -94,12 +99,12 @@ extra_rdoc_files:
94
99
  - README.rdoc
95
100
  - ext/ent_native/chisq.c
96
101
  - ext/ent_native/ent_native.c
97
- - ext/ent_native/iso8859.c
98
102
  - ext/ent_native/randtest.c
99
103
  files:
100
104
  - .document
101
105
  - .rspec
102
106
  - Gemfile
107
+ - History.txt
103
108
  - LICENSE.txt
104
109
  - README.rdoc
105
110
  - Rakefile
@@ -107,12 +112,12 @@ files:
107
112
  - ext/ent_native/chisq.c
108
113
  - ext/ent_native/ent_native.c
109
114
  - ext/ent_native/extconf.rb
110
- - ext/ent_native/iso8859.c
111
- - ext/ent_native/iso8859.h
112
115
  - ext/ent_native/randtest.c
113
116
  - ext/ent_native/randtest.h
114
117
  - lib/ent.rb
115
118
  - spec/ent_spec.rb
119
+ - spec/random_test_spec.rb
120
+ - spec/samples/foo_test
116
121
  - spec/spec_helper.rb
117
122
  has_rdoc: true
118
123
  homepage: http://github.com/emonti/ent
@@ -124,6 +129,7 @@ rdoc_options: []
124
129
  require_paths:
125
130
  - lib
126
131
  required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
127
133
  requirements:
128
134
  - - ">="
129
135
  - !ruby/object:Gem::Version
@@ -131,6 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
137
  - 0
132
138
  version: "0"
133
139
  required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
134
141
  requirements:
135
142
  - - ">="
136
143
  - !ruby/object:Gem::Version
@@ -140,10 +147,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
147
  requirements: []
141
148
 
142
149
  rubyforge_project:
143
- rubygems_version: 1.3.6
150
+ rubygems_version: 1.3.7
144
151
  signing_key:
145
152
  specification_version: 3
146
153
  summary: Calculate the entropy of data
147
154
  test_files:
148
155
  - spec/ent_spec.rb
156
+ - spec/random_test_spec.rb
149
157
  - spec/spec_helper.rb
@@ -1,19 +0,0 @@
1
-
2
- /* ISO 8859/1 Latin-1 alphabetic and upper and lower case bit vector tables. */
3
-
4
- /* LINTLIBRARY */
5
-
6
- unsigned char isoalpha[32] = {
7
- 0,0,0,0,0,0,0,0,127,255,255,224,127,255,255,224,0,0,0,0,0,0,0,0,255,255,
8
- 254,255,255,255,254,255
9
- };
10
-
11
- unsigned char isoupper[32] = {
12
- 0,0,0,0,0,0,0,0,127,255,255,224,0,0,0,0,0,0,0,0,0,0,0,0,255,255,254,254,
13
- 0,0,0,0
14
- };
15
-
16
- unsigned char isolower[32] = {
17
- 0,0,0,0,0,0,0,0,0,0,0,0,127,255,255,224,0,0,0,0,0,0,0,0,0,0,0,1,255,255,
18
- 254,255
19
- };
@@ -1,17 +0,0 @@
1
-
2
- /* ISO 8859/1 Latin-1 "ctype" macro replacements. */
3
-
4
- extern unsigned char isoalpha[32], isoupper[32], isolower[32];
5
-
6
- #define isISOspace(x) ((isascii(((unsigned char) (x))) && isspace(((unsigned char) (x)))) || ((x) == 0xA0))
7
- #define isISOalpha(x) ((isoalpha[(((unsigned char) (x))) / 8] & (0x80 >> ((((unsigned char) (x))) % 8))) != 0)
8
- #define isISOupper(x) ((isoupper[(((unsigned char) (x))) / 8] & (0x80 >> ((((unsigned char) (x))) % 8))) != 0)
9
- #define isISOlower(x) ((isolower[(((unsigned char) (x))) / 8] & (0x80 >> ((((unsigned char) (x))) % 8))) != 0)
10
- #define isISOprint(x) ((((x) >= ' ') && ((x) <= '~')) || ((x) >= 0xA0))
11
- #define toISOupper(x) (isISOlower(x) ? (isascii(((unsigned char) (x))) ? \
12
- toupper(x) : (((((unsigned char) (x)) != 0xDF) && \
13
- (((unsigned char) (x)) != 0xFF)) ? \
14
- (((unsigned char) (x)) - 0x20) : (x))) : (x))
15
- #define toISOlower(x) (isISOupper(x) ? (isascii(((unsigned char) (x))) ? \
16
- tolower(x) : (((unsigned char) (x)) + 0x20)) \
17
- : (x))