dr_light 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,377 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ shared_examples 'value normalized' do |order:, expctd_order: 1, devnc_fctr: 0|
6
+ let(:multiplier) { 0.1 * 10**order }
7
+ let(:expected_multiplier) { 10**expctd_order }
8
+ let(:deviance) do
9
+ Random.rand * multiplier * devnc_fctr
10
+ end
11
+
12
+ context 'when value is positive' do
13
+ let(:value) { Random.rand * multiplier }
14
+ let(:range) do
15
+ [0.1 * expected_multiplier, expected_multiplier]
16
+ end
17
+
18
+ it 'returns an order 1 positive number' do
19
+ expect(normalizer.value).to be_between(*range)
20
+ end
21
+ end
22
+
23
+ context 'when value is positive' do
24
+ let(:value) { -1 * Random.rand * multiplier }
25
+ let(:range) do
26
+ [-expected_multiplier, -0.1 * expected_multiplier]
27
+ end
28
+
29
+ it 'returns an order 1 positive number' do
30
+ expect(normalizer.value).to be_between(*range)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe DrLight::ScientificNumber::Normalizer do
36
+ subject(:normalizer) do
37
+ described_class.new(value: value, deviance: deviance)
38
+ end
39
+
40
+ describe '#value' do
41
+ context 'when there is no deviance' do
42
+ let(:deviance) { 0 }
43
+
44
+ context 'when value has order 2' do
45
+ it_behaves_like 'value normalized', order: 2
46
+ end
47
+
48
+ context 'when value has order 3' do
49
+ it_behaves_like 'value normalized', order: 3
50
+ end
51
+
52
+ context 'when value has order -2' do
53
+ it_behaves_like 'value normalized', order: -2
54
+ end
55
+ end
56
+
57
+ context 'when deviance has the same order as the value' do
58
+ context 'when number has 2 digits' do
59
+ it_behaves_like 'value normalized', order: 2, devnc_fctr: 1
60
+ end
61
+
62
+ context 'when number has 3 digits' do
63
+ it_behaves_like 'value normalized', order: 3, devnc_fctr: 1
64
+ end
65
+
66
+ context 'when negative number only has 2 digits and order 1' do
67
+ let(:value) { -1.2 }
68
+ let(:deviance) { 1.3 }
69
+
70
+ it 'shows 2 digits' do
71
+ expect(normalizer.value).to eq(-1.2)
72
+ end
73
+ end
74
+
75
+ context 'when negative number has 3 digits and order 1' do
76
+ let(:value) { -1.23 }
77
+ let(:deviance) { 1.34 }
78
+
79
+ it 'shows 2 digits' do
80
+ expect(normalizer.value).to eq(-1.23)
81
+ end
82
+ end
83
+
84
+ context 'when negative number only has 2 digits and order 10^-1' do
85
+ let(:value) { -0.12 }
86
+ let(:deviance) { 0.13 }
87
+
88
+ it 'shows 2 digits' do
89
+ expect(normalizer.value).to eq(-1.2)
90
+ end
91
+ end
92
+
93
+ context 'when negative number has 3 digits and order 10^-1' do
94
+ let(:value) { -0.123 }
95
+ let(:deviance) { 0.134 }
96
+
97
+ it 'shows 2 digits' do
98
+ expect(normalizer.value).to eq(-1.23)
99
+ end
100
+ end
101
+
102
+ context 'when positive number only has 2 digits and order 10' do
103
+ let(:value) { 12 }
104
+ let(:deviance) { 13 }
105
+
106
+ it 'shows 2 digits' do
107
+ expect(normalizer.value).to eq(1.2)
108
+ end
109
+ end
110
+
111
+ context 'when positive number has 3 digits and order 10^2' do
112
+ let(:value) { 123 }
113
+ let(:deviance) { 134 }
114
+
115
+ it 'shows 2 digits' do
116
+ expect(normalizer.value).to eq(1.23)
117
+ end
118
+ end
119
+
120
+ context 'when positive number only has 2 digits and order 1' do
121
+ let(:value) { 1.2 }
122
+ let(:deviance) { 1.3 }
123
+
124
+ it 'shows 2 digits' do
125
+ expect(normalizer.value).to eq(1.2)
126
+ end
127
+ end
128
+
129
+ context 'when positive number has 3 digits and order 1' do
130
+ let(:value) { 1.23 }
131
+ let(:deviance) { 1.34 }
132
+
133
+ it 'shows 2 digits' do
134
+ expect(normalizer.value).to eq(1.23)
135
+ end
136
+ end
137
+
138
+ context 'when positive number only has 2 digits and order 10^-1' do
139
+ let(:value) { 0.12 }
140
+ let(:deviance) { 0.13 }
141
+
142
+ it 'shows 2 digits' do
143
+ expect(normalizer.value).to eq(1.2)
144
+ end
145
+ end
146
+
147
+ context 'when positive number has 3 digits and order 10^-1' do
148
+ let(:value) { 0.123 }
149
+ let(:deviance) { 0.134 }
150
+
151
+ it 'shows 2 digits' do
152
+ expect(normalizer.value).to eq(1.23)
153
+ end
154
+ end
155
+ end
156
+
157
+ context 'when deviance has smaller order than the value' do
158
+ context 'when negative number only has 2 digits and order 10' do
159
+ let(:value) { -12 }
160
+ let(:deviance) { 0.13 }
161
+
162
+ it 'shows 2 deviance digits' do
163
+ expect(normalizer.value).to eq(-1.200)
164
+ end
165
+ end
166
+
167
+ context 'when negative number has 3 digits and order 10^2' do
168
+ let(:value) { -123 }
169
+ let(:deviance) { 1.34 }
170
+
171
+ it 'shows 2 deviance digits' do
172
+ expect(normalizer.value).to eq(-1.230)
173
+ end
174
+ end
175
+
176
+ context 'when negative number only has 2 digits and order 1' do
177
+ let(:value) { -1.2 }
178
+ let(:deviance) { 0.013 }
179
+
180
+ it 'shows 2 deviance digits' do
181
+ expect(normalizer.value).to eq(-1.200)
182
+ end
183
+ end
184
+
185
+ context 'when negative number has 3 digits and order 1' do
186
+ let(:value) { -1.23 }
187
+ let(:deviance) { 0.0134 }
188
+
189
+ it 'shows 2 deviance digits' do
190
+ expect(normalizer.value).to eq(-1.230)
191
+ end
192
+ end
193
+
194
+ context 'when negative number only has 2 digits and order 10^-1' do
195
+ let(:value) { -0.12 }
196
+ let(:deviance) { 0.0013 }
197
+
198
+ it 'shows 2 deviance digits' do
199
+ expect(normalizer.value).to eq(-1.200)
200
+ end
201
+ end
202
+
203
+ context 'when negative number has 3 digits and order 10^-1' do
204
+ let(:value) { -0.123 }
205
+ let(:deviance) { 0.00134 }
206
+
207
+ it 'shows 2 deviance digits' do
208
+ expect(normalizer.value).to eq(-1.230)
209
+ end
210
+ end
211
+
212
+ context 'when positive number only has 2 digits and order 10' do
213
+ let(:value) { 12 }
214
+ let(:deviance) { 0.13 }
215
+
216
+ it 'shows 2 deviance digits' do
217
+ expect(normalizer.value).to eq(1.200)
218
+ end
219
+ end
220
+
221
+ context 'when positive number has 3 digits and order 10^2' do
222
+ let(:value) { 123 }
223
+ let(:deviance) { 1.34 }
224
+
225
+ it 'shows 2 deviance digits' do
226
+ expect(normalizer.value).to eq(1.230)
227
+ end
228
+ end
229
+
230
+ context 'when positive number only has 2 digits and order 1' do
231
+ let(:value) { 1.2 }
232
+ let(:deviance) { 0.013 }
233
+
234
+ it 'shows 2 deviance digits' do
235
+ expect(normalizer.value).to eq(1.200)
236
+ end
237
+ end
238
+
239
+ context 'when positive number has 3 digits and order 1' do
240
+ let(:value) { 1.23 }
241
+ let(:deviance) { 0.0134 }
242
+
243
+ it 'shows 2 deviance digits' do
244
+ expect(normalizer.value).to eq(1.230)
245
+ end
246
+ end
247
+
248
+ context 'when positive number only has 2 digits and order 10^-1' do
249
+ let(:value) { 0.12 }
250
+ let(:deviance) { 0.0013 }
251
+
252
+ it 'shows 2 deviance digits' do
253
+ expect(normalizer.value).to eq(1.200)
254
+ end
255
+ end
256
+
257
+ context 'when positive number has 3 digits and order 10^-1' do
258
+ let(:value) { 0.123 }
259
+ let(:deviance) { 0.00134 }
260
+
261
+ it 'shows 2 deviance digits' do
262
+ expect(normalizer.value).to eq(1.230)
263
+ end
264
+ end
265
+ end
266
+
267
+ context 'when deviance has bigger order than the value' do
268
+ context 'when negative number only has 2 digits and order 10' do
269
+ let(:value) { -12 }
270
+ let(:deviance) { 130 }
271
+
272
+ it 'shows 2 deviance digits' do
273
+ expect(normalizer.value).to eq(-0.12)
274
+ end
275
+ end
276
+
277
+ context 'when negative number has 3 digits and order 10^2' do
278
+ let(:value) { -123 }
279
+ let(:deviance) { 1340 }
280
+
281
+ it 'shows 2 deviance digits' do
282
+ expect(normalizer.value).to eq(-0.123)
283
+ end
284
+ end
285
+
286
+ context 'when negative number only has 2 digits and order 1' do
287
+ let(:value) { -1.2 }
288
+ let(:deviance) { 13 }
289
+
290
+ it 'shows 2 deviance digits' do
291
+ expect(normalizer.value).to eq(-0.12)
292
+ end
293
+ end
294
+
295
+ context 'when negative number has 3 digits and order 1' do
296
+ let(:value) { -1.23 }
297
+ let(:deviance) { 13.4 }
298
+
299
+ it 'shows 2 deviance digits' do
300
+ expect(normalizer.value).to eq(-0.123)
301
+ end
302
+ end
303
+
304
+ context 'when negative number only has 2 digits and order 10^-1' do
305
+ let(:value) { -0.12 }
306
+ let(:deviance) { 1.3 }
307
+
308
+ it 'shows 2 deviance digits' do
309
+ expect(normalizer.value).to eq(-0.12)
310
+ end
311
+ end
312
+
313
+ context 'when negative number has 3 digits and order 10^-1' do
314
+ let(:value) { -0.123 }
315
+ let(:deviance) { 1.34 }
316
+
317
+ it 'shows 2 deviance digits' do
318
+ expect(normalizer.value).to eq(-0.123)
319
+ end
320
+ end
321
+
322
+ context 'when positive number only has 2 digits and order 10' do
323
+ let(:value) { 12 }
324
+ let(:deviance) { 0.13 }
325
+
326
+ it 'shows 2 deviance digits' do
327
+ expect(normalizer.value).to eq(1.200)
328
+ end
329
+ end
330
+
331
+ context 'when positive number has 3 digits and order 10^2' do
332
+ let(:value) { 123 }
333
+ let(:deviance) { 1340 }
334
+
335
+ it 'shows 2 deviance digits' do
336
+ expect(normalizer.value).to eq(0.123)
337
+ end
338
+ end
339
+
340
+ context 'when positive number only has 2 digits and order 1' do
341
+ let(:value) { 1.2 }
342
+ let(:deviance) { 13 }
343
+
344
+ it 'shows 2 deviance digits' do
345
+ expect(normalizer.value).to eq(0.12)
346
+ end
347
+ end
348
+
349
+ context 'when positive number has 3 digits and order 1' do
350
+ let(:value) { 1.23 }
351
+ let(:deviance) { 13.4 }
352
+
353
+ it 'shows 2 deviance digits' do
354
+ expect(normalizer.value).to eq(0.123)
355
+ end
356
+ end
357
+
358
+ context 'when positive number only has 2 digits and order 10^-1' do
359
+ let(:value) { 0.12 }
360
+ let(:deviance) { 1.3 }
361
+
362
+ it 'shows 2 deviance digits' do
363
+ expect(normalizer.value).to eq(0.12)
364
+ end
365
+ end
366
+
367
+ context 'when positive number has 3 digits and order 10^-1' do
368
+ let(:value) { 0.123 }
369
+ let(:deviance) { 1.34 }
370
+
371
+ it 'shows 2 deviance digits' do
372
+ expect(normalizer.value).to eq(0.123)
373
+ end
374
+ end
375
+ end
376
+ end
377
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe DrLight::Utils do
4
+ describe '.order' do
5
+ context 'when number is bigger than 1' do
6
+ it 'returns positive order' do
7
+ expect(described_class.order(100)).to eq(2)
8
+ end
9
+ end
10
+
11
+ context 'when number is smaller than 1 and positive' do
12
+ it 'returns negative order' do
13
+ expect(described_class.order(0.01)).to eq(-2)
14
+ end
15
+ end
16
+
17
+ context 'when number is 0' do
18
+ it do
19
+ expect(described_class.order(0)).to eq(0)
20
+ end
21
+ end
22
+
23
+ context 'when number is bigger than -1 and negative' do
24
+ it 'returns negative order' do
25
+ expect(described_class.order(-0.01)).to eq(-2)
26
+ end
27
+ end
28
+
29
+ context 'when number is smaller than -1' do
30
+ it 'returns positive order' do
31
+ expect(described_class.order(-100)).to eq(2)
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '.order_difference' do
37
+ subject(:difference) do
38
+ described_class.order_difference(first, second)
39
+ end
40
+
41
+ context 'when first is bigger and has greater order' do
42
+ let(:first) { 1000 }
43
+ let(:second) { 0.01 }
44
+
45
+ it 'returns positive' do
46
+ expect(difference).to be_positive
47
+ end
48
+ end
49
+
50
+ context 'when first is bigger but has lesser order' do
51
+ let(:first) { 100 }
52
+ let(:second) { -1000 }
53
+
54
+ it 'returns positive' do
55
+ expect(difference).to be_negative
56
+ end
57
+ end
58
+
59
+ context 'when first is bigger but has same order' do
60
+ let(:first) { 100 }
61
+ let(:second) { -100 }
62
+
63
+ it 'returns positive' do
64
+ expect(difference).to be_zero
65
+ end
66
+ end
67
+
68
+ context 'when first is smaller but has bigger order' do
69
+ let(:first) { -100 }
70
+ let(:second) { 10 }
71
+
72
+ it 'returns positive' do
73
+ expect(difference).to be_positive
74
+ end
75
+ end
76
+
77
+ context 'when first is smaller and has smaller order' do
78
+ let(:first) { -0.1 }
79
+ let(:second) { 10 }
80
+
81
+ it 'returns positive' do
82
+ expect(difference).to be_negative
83
+ end
84
+ end
85
+
86
+ context 'when first is smaller and has same order' do
87
+ let(:first) { -0.1 }
88
+ let(:second) { 0.1 }
89
+
90
+ it 'returns positive' do
91
+ expect(difference).to be_zero
92
+ end
93
+ end
94
+ end
95
+ end