bytesize 0.1.0 → 0.1.1

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.
@@ -1,30 +0,0 @@
1
- #!/usr/bin/env ruby -w
2
- #
3
- # ByteSize
4
- #
5
- # Spec
6
- #
7
- # © 2018 Adam Hunt
8
- # Created: 2018-04-07
9
- # Modified: 2018-04-07
10
- #
11
-
12
-
13
-
14
- require 'rspec'
15
- require 'test_enum'
16
- require 'shared_examples'
17
-
18
- require 'bytesize'
19
-
20
-
21
-
22
- describe ByteSize do
23
- TestEnum.new( base:1000 ).each do |value|
24
-
25
- it_behaves_like "a byte size", value
26
-
27
- #include_examples "compares to an instance of equal value correctly", new_class:IECByteSize
28
-
29
- end
30
- end
@@ -1,30 +0,0 @@
1
- #!/usr/bin/env ruby -w
2
- #
3
- # ByteSize
4
- #
5
- # Spec
6
- #
7
- # © 2018 Adam Hunt
8
- # Created: 2018-04-07
9
- # Modified: 2018-04-07
10
- #
11
-
12
-
13
-
14
- require 'rspec'
15
- require 'test_enum'
16
- require 'shared_examples'
17
-
18
- require 'bytesize'
19
-
20
-
21
-
22
- describe IECByteSize do
23
- TestEnum.new( base:1024 ).each do |value|
24
-
25
- include_examples "a byte size", value
26
-
27
- #include_examples "compares to an instance of equal value correctly", new_class:ByteSize
28
-
29
- end
30
- end
@@ -1,408 +0,0 @@
1
- #!/usr/bin/env ruby -w
2
- #
3
- # ByteSize
4
- #
5
- # Shared Examples
6
- #
7
- # © 2018 Adam Hunt
8
- # Created: 2018-04-07
9
- # Modified: 2018-04-07
10
- #
11
-
12
-
13
-
14
- require 'value_class_helper'
15
-
16
-
17
-
18
- shared_examples "a byte size" do |value|
19
- context "and" do
20
-
21
- let(:instance){ described_class.new(value) }
22
-
23
- let(:i){ instance.to_i }
24
-
25
-
26
- ###
27
- # Initialization
28
- ###
29
-
30
- context "initialized from an integer" do
31
- it "returns an instance of the correct class" do
32
- expect(instance.class).to be(described_class)
33
- end
34
- it "stores and returns the correct value" do
35
- expect(i).to eq(value)
36
- end
37
- end
38
-
39
-
40
- ###
41
- # Type Conversion
42
- ###
43
-
44
- describe "#to_i" do
45
- it "returns an integer" do
46
- expect(i.class).to be(Integer)
47
- end
48
- end
49
-
50
-
51
- ###
52
- # Unit Conversion
53
- ###
54
-
55
- describe "#bytes" do
56
- let(:v){ instance.bytes }
57
- it "returns the correct value in bytes" do
58
- expect(v).to eq(value)
59
- end
60
- it "returns an integer" do
61
- expect(v.class).to eq(Integer)
62
- end
63
- end
64
-
65
- describe "#to_bytes" do
66
- let(:v){ instance.to_bytes }
67
- it "returns the correct value in bytes" do
68
- expect(v).to eq(value)
69
- end
70
- it "returns an integer" do
71
- expect(v.class).to eq(Integer)
72
- end
73
- end
74
-
75
- describe "#to_kb" do
76
- let(:v){ instance.to_kb }
77
- it "returns the correct value in kilobytes" do
78
- expect(v).to be( value.to_f / 1000 )
79
- end
80
- it "returns an float" do
81
- expect(v.class).to eq(Float)
82
- end
83
- end
84
-
85
- describe "#to_kib" do
86
- let(:v){ instance.to_kib }
87
- it "returns the correct value in kibibyte" do
88
- expect(v).to be( value.to_f / 1024 )
89
- end
90
- it "returns an float" do
91
- expect(v.class).to eq(Float)
92
- end
93
- end
94
-
95
- describe "#to_mb" do
96
- let(:v){ instance.to_mb }
97
- it "returns the correct value in megabytes" do
98
- expect(v).to be( value.to_f / 1000000 )
99
- end
100
- it "returns an float" do
101
- expect(v.class).to eq(Float)
102
- end
103
- end
104
-
105
- describe "#to_mib" do
106
- let(:v){ instance.to_mib }
107
- it "returns the correct value in mebibytes" do
108
- expect(v).to be( value.to_f / 1048576 )
109
- end
110
- it "returns an float" do
111
- expect(v.class).to eq(Float)
112
- end
113
- end
114
-
115
- describe "#to_gb" do
116
- let(:v){ instance.to_gb }
117
- it "returns the correct value in gigabytes" do
118
- expect(v).to be( value.to_f / 1000000000 )
119
- end
120
- it "returns an float" do
121
- expect(v.class).to eq(Float)
122
- end
123
- end
124
-
125
- describe "#to_gib" do
126
- let(:v){ instance.to_gib }
127
- it "returns the correct value in gibibytes" do
128
- expect(v).to be( value.to_f / 1073741824 )
129
- end
130
- it "returns an float" do
131
- expect(v.class).to eq(Float)
132
- end
133
- end
134
-
135
- describe "#to_tb" do
136
- let(:v){ instance.to_tb }
137
- it "returns the correct value in terabytes" do
138
- expect(v).to be( value.to_f / 1000000000000 )
139
- end
140
- it "returns an float" do
141
- expect(v.class).to eq(Float)
142
- end
143
- end
144
-
145
- describe "#to_tib" do
146
- let(:v){ instance.to_tib }
147
- it "returns the correct value in tebibytes" do
148
- expect(v).to be( value.to_f / 1099511627776 )
149
- end
150
- it "returns an float" do
151
- expect(v.class).to eq(Float)
152
- end
153
- end
154
-
155
- describe "#to_pb" do
156
- let(:v){ instance.to_pb }
157
- it "returns the correct value in petabytes" do
158
- expect(v).to be( value.to_f / 1000000000000000 )
159
- end
160
- it "returns an float" do
161
- expect(v.class).to eq(Float)
162
- end
163
- end
164
-
165
- describe "#to_pib" do
166
- let(:v){ instance.to_pib }
167
- it "returns the correct value in pebibytes" do
168
- expect(v).to be( value.to_f / 1125899906842624 )
169
- end
170
- it "returns an float" do
171
- expect(v.class).to eq(Float)
172
- end
173
- end
174
-
175
- describe "#to_eb" do
176
- let(:v){ instance.to_eb }
177
- it "returns the correct value in exabytes" do
178
- expect(v).to be( value.to_f / 1000000000000000000 )
179
- end
180
- it "returns an float" do
181
- expect(v.class).to eq(Float)
182
- end
183
- end
184
-
185
- describe "#to_eib" do
186
- let(:v){ instance.to_eib }
187
- it "returns the correct value in exbibytes" do
188
- expect(v).to be( value.to_f / 1152921504606846976 )
189
- end
190
- it "returns an float" do
191
- expect(v.class).to eq(Float)
192
- end
193
- end
194
-
195
- describe "#to_zb" do
196
- let(:v){ instance.to_zb }
197
- it "returns the correct value in zettabytes" do
198
- expect(v).to be( value.to_f / 1000000000000000000000 )
199
- end
200
- it "returns an float" do
201
- expect(v.class).to eq(Float)
202
- end
203
- end
204
-
205
- describe "#to_zib" do
206
- let(:v){ instance.to_zib }
207
- it "returns the correct value in zebibytes" do
208
- expect(v).to be( value.to_f / 1180591620717411303424 )
209
- end
210
- it "returns an float" do
211
- expect(v.class).to eq(Float)
212
- end
213
- end
214
-
215
- describe "#to_yb" do
216
- let(:v){ instance.to_yb }
217
- it "returns the correct value in yottabytes" do
218
- expect(v).to be( value.to_f / 1000000000000000000000000 )
219
- end
220
- it "returns an float" do
221
- expect(v.class).to eq(Float)
222
- end
223
- end
224
-
225
- describe "#to_yib" do
226
- let(:v){ instance.to_yib }
227
- it "returns the correct value in yobibytes" do
228
- expect(v).to be( value.to_f / 1208925819614629174706176 )
229
- end
230
- it "returns an float" do
231
- expect(v.class).to eq(Float)
232
- end
233
- end
234
-
235
-
236
- ###
237
- # Comparison
238
- ###
239
-
240
- include_examples "compares to itself correctly"
241
-
242
- include_examples "compares to a duplicate instance correctly"
243
-
244
- include_examples "compares to an instance of equal value correctly"
245
-
246
- #include_examples "compares to an equal numeric correctly", value:value.to_i
247
-
248
- #include_examples "compares to an equal numeric correctly", value:value.to_r
249
-
250
- #include_examples "compares to an equal float correctly", value
251
-
252
- #larger_value = rand(value+1..1024**9)
253
-
254
- #include_examples "compares to a larger numeric correctly", value, larger_value.to_i
255
-
256
- #include_examples "compares to a larger numeric correctly", value, larger_value.to_r
257
-
258
- #include_examples "compares to a larger float correctly", value, larger_value
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
- # larger_value = rand(value+1..1024**9)
270
-
271
- # context "when compared to a larger instance (#{larger_value} bytes)" do
272
- # let(:a){ instance }
273
- # let(:b){ described_class.new(larger_value) }
274
- # include_examples "correct comparison", {
275
- # :== => false,
276
- # :=== => false,
277
- # :eql? => false,
278
- # :equal? => false,
279
- # :<=> => -1,
280
- # :> => false,
281
- # :< => true,
282
- # :>= => false,
283
- # :<= => true
284
- # }
285
- # end
286
-
287
- # context "when a larger instance (#{larger_value} bytes) is compared to it" do
288
- # let(:a){ described_class.new(larger_value) }
289
- # let(:b){ instance }
290
- # include_examples "correct comparison", {
291
- # :== => false,
292
- # :=== => false,
293
- # :eql? => false,
294
- # :equal? => false,
295
- # :<=> => 1,
296
- # :> => true,
297
- # :< => false,
298
- # :>= => true,
299
- # :<= => false
300
- # }
301
- # end
302
-
303
- # context "when compared to a larger integer (#{larger_value.to_i})" do
304
- # let(:a){ instance }
305
- # let(:b){ larger_value.to_i }
306
- # include_examples "correct comparison", {
307
- # :== => false,
308
- # :=== => false,
309
- # :eql? => false,
310
- # :equal? => false,
311
- # :<=> => -1,
312
- # :> => false,
313
- # :< => true,
314
- # :>= => false,
315
- # :<= => true
316
- # }
317
- # end
318
-
319
- # context "when a larger integer (#{larger_value.to_i}) is compared to it" do
320
- # let(:a){ larger_value.to_i }
321
- # let(:b){ instance }
322
- # include_examples "correct comparison", {
323
- # :== => false,
324
- # :=== => false,
325
- # :eql? => false,
326
- # :equal? => false,
327
- # :<=> => 1,
328
- # :> => true,
329
- # :< => false,
330
- # :>= => true,
331
- # :<= => false
332
- # }
333
- # end
334
-
335
- # f = value.to_f
336
- # context "when compared to a larger float (#{f})" do
337
- # rounding_error_direction = f <=> value.to_i
338
- # let(:a){ instance }
339
- # let(:b){ f }
340
- # include_examples "correct comparison", {
341
- # :== => rounding_error_direction.zero?,
342
- # :=== => rounding_error_direction.zero?,
343
- # :eql? => false,
344
- # :equal? => false,
345
- # :<=> => -rounding_error_direction,
346
- # :> => false,
347
- # :< => true,
348
- # :>= => ( rounding_error.zero? or rounding_error.positive? ),
349
- # :<= => ( rounding_error.zero? or rounding_error.negative? )
350
- # }
351
- # end
352
-
353
- # context "when an equal float is compared to it" do
354
- # f = value.to_f
355
- # rounding_error = value.to_i <=> value.to_f
356
- # let(:a){ f }
357
- # let(:b){ instance }
358
- # include_examples "correct comparison", {
359
- # :== => rounding_error.zero?,
360
- # :=== => rounding_error.zero?,
361
- # :eql? => false,
362
- # :equal? => false,
363
- # :<=> => rounding_error,
364
- # :> => rounding_error.positive?,
365
- # :< => rounding_error.negative?,
366
- # :>= => ( rounding_error.zero? or rounding_error.positive? ),
367
- # :<= => ( rounding_error.zero? or rounding_error.negative? )
368
- # }
369
- # end
370
-
371
- # context "when compared to an equal rational" do
372
- # let(:a){ instance }
373
- # let(:b){ value.to_r }
374
- # include_examples "correct comparison", {
375
- # :== => true,
376
- # :=== => true,
377
- # :eql? => false,
378
- # :equal? => false,
379
- # :<=> => 0,
380
- # :> => false,
381
- # :< => false,
382
- # :>= => true,
383
- # :<= => true
384
- # }
385
- # end
386
-
387
- # context "when an equal rational is compared to it" do
388
- # let(:a){ value.to_r }
389
- # let(:b){ instance }
390
- # include_examples "correct comparison", {
391
- # :== => true,
392
- # :=== => true,
393
- # :eql? => false,
394
- # :equal? => false,
395
- # :<=> => 0,
396
- # :> => false,
397
- # :< => false,
398
- # :>= => true,
399
- # :<= => true
400
- # }
401
- # end
402
-
403
-
404
-
405
- include_examples "correctly reports positive, negative, and zero", value
406
-
407
- end
408
- end