File: types.t

package info (click to toggle)
libdata-types-perl 0.17-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 108 kB
  • sloc: perl: 301; makefile: 2
file content (208 lines) | stat: -rw-r--r-- 5,291 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/perl -w

use strict;
use Test;
BEGIN { plan tests => 169 };
use Data::Types qw(:all);
ok(1); # If we made it this far, we're ok.

#########################

# Test is_whole.
ok( is_whole(10) );
ok( is_whole(22) );
ok( is_whole(1) );
ok( is_whole(700) );
ok( is_whole(0) );
ok( ! is_whole(.22) );
ok( ! is_whole(-33) );
ok( ! is_whole(-0.1) );

# Test to_whole.
ok( to_whole(10) == 10 );
ok( to_whole(1) == 1 );
ok( to_whole('foo33') ==  33);
ok( to_whole('ri+4') == 4 );
ok( to_whole('+45ts') == 45 );
ok( to_whole(1.23e99) == 1) ;  # This should probably be changed somehow.
ok( to_whole(0) eq '0' );
ok( ! defined to_whole('blech') );
ok( ! defined to_whole('') );
ok( ! defined to_whole(undef) );
ok( to_whole('foo00') eq '0' );
ok( to_whole(.44) eq '0' );
ok( ! defined to_whole('foo-33') );
ok( ! defined to_whole(-44) );
ok( ! defined to_whole(-0.33) );
ok( ! defined to_whole('sep-0.1') );

# Test is_count.
ok( is_count(10) );
ok( is_count(22) );
ok( is_count(1) );
ok( is_count(700) );
ok( ! is_count(0) );
ok( ! is_count(.22) );
ok( ! is_count(-33) );
ok( ! is_count(-0.1) );

# Test to_count.
ok( to_count(10) == 10 );
ok( to_count(1) == 1 );
ok( to_count('foo33') ==  33);
ok( to_count('ri+4') == 4 );
ok( to_count('+45ts') == 45 );
ok( to_count(1.23e99) == 1) ;  # This should probably be changed somehow.
ok( ! defined to_count(0) );
ok( ! defined to_count('blech') );
ok( ! defined to_count('') );
ok( ! defined to_count(undef) );
ok( ! defined to_count('foo00') );
ok( ! defined to_count(.44) );
ok( ! defined to_count('foo-33') );
ok( ! defined to_count(-44) );
ok( ! defined to_count(-0.33) );
ok( ! defined to_count('sep-0.1') );

# Test is_int.
ok( is_int(10) );
ok( is_int(0) );
ok( is_int(-33) );
ok( is_int(+23) );
ok( ! is_int('+') );
ok( ! is_int('-') );
ok( ! is_int(22.2) );
ok( ! is_int(0.44) );
ok( ! is_int('foo') );
ok( ! is_int('33foo') );
ok( ! is_int(-33.2) );
ok( ! is_int(undef));
ok( ! is_int(''));

# Test to_int.
ok( to_int(10) == 10 );
ok( to_int(10.22) == 10 );
ok( to_int(0.44) == 0 );
ok( to_int(0.54) == 1 );
ok( to_int(10.468473895043) == 10 );
ok( to_int(+10.51) == 11);
ok( to_int("10.44foo") == 10 );
ok( to_int(-22) == -22 );
ok( to_int(-22.6) == -23);
ok( to_int(1.23e99) == 1) ;  # This should probably be changed somehow.
ok( ! defined to_int(undef) );
ok( ! defined to_int('') );
ok( ! defined to_int('foo') );

# Test is_decimal.
ok( is_decimal(.22) );
ok( is_decimal(0.4) );
ok( is_decimal(22.44) );
ok( is_decimal(-0.44) );
ok( is_decimal(-100.45) );
ok( is_decimal(0) );
ok( is_decimal(22) );
ok( is_decimal(-33) );
ok( is_decimal(-33.0) );
ok( ! is_decimal('+') );
ok( ! is_decimal('-') );
ok( ! is_decimal(undef) );
ok( ! is_decimal('foo') );
ok( ! is_decimal('foo22') );
ok( ! is_decimal('22foo') );
ok( ! is_decimal(1.23e99) );

# Test to_decimal.
ok( to_decimal(0) == 0 );
ok( to_decimal(100) == 100 );
ok( to_decimal(0.22) == 0.22 );
ok( to_decimal(-4) == -4 );
ok( to_decimal(-3.4) == -3.4 );
ok( to_decimal('foo3.5') == 3.5 );
ok( to_decimal('-35foo') == -35 );
ok( to_decimal('foo-3') == -3 );
ok( to_decimal('40foo') == 40 );
ok( to_decimal(1.23e99) == 1.23 ); # This should probably be changed somehow.
ok( to_decimal(10.500009) == 10.50001 );
ok( to_decimal(10.500009, 10) == 10.500009 );
ok( ! defined to_decimal(undef) );
ok( ! defined to_decimal('') );
ok( ! defined to_decimal('foo'));

# Test is_real.
ok( is_real(0) );
ok( is_real(100) );
ok( is_real(0.22) );
ok( is_real(-4) );
ok( is_real(-4.9) );
ok( is_real(12043.3423) );
ok( ! is_real('foo') );
ok( ! is_real('+') );
ok( ! is_real('-') );
ok( ! is_real(undef) );
ok( ! is_real('foo34.33') );
ok( ! is_real(1.23e99) );

# Test to_real.
ok( to_real(0) == 0 );
ok( to_real(100) == 100 );
ok( to_real(0.22) == 0.22 );
ok( to_real(-4) == -4 );
ok( to_real(-3.4) == -3.4 );
ok( to_real('foo3.5') == 3.5 );
ok( to_real('-35foo') == -35 );
ok( to_real('foo-3') == -3 );
ok( to_real('40foo') == 40 );
ok( to_real(1.23e99) == 1.23 ); # This should probably be changed somehow.
ok( ! defined to_real(undef) );
ok( ! defined to_real('') );
ok( ! defined to_real('foo'));

# Test is_float.
ok( is_float(10) );
ok( is_float(11.2) );
ok( is_float(0.2) );
ok( is_float(345.96948383) );
ok( is_float(1.23e99) );
ok( is_float(-938.54) );
ok( is_float(+234.5) );
ok( !is_float('foo') );
ok( !is_float('22.34foo') );
ok( !is_float(undef) );
ok( !is_float('+') );
ok( !is_float('-') );
ok( !is_float('') );

# Test to_float.
ok( to_float(10) == 10 );
ok( to_float(11.2) == 11.2 );
ok( to_float(456.98765) == 456.98765 );
ok( to_float(0) == 0 );
ok( to_float('44.334foo') == 44.334 );
ok( to_float(-34.3) == -34.3 );
ok( to_float(+456.04) == 456.04 );
ok( to_float(1.23e99) == 1.23000e99 );
ok( to_float(1.23e99, 1) == 1.2e99 );
ok( to_float('foo1.23e99') == 1.23000e99 );
ok( ! defined to_float(undef) );
ok( ! defined to_float('') );
ok( ! defined to_float('foo'));

# Test is_string.
ok( is_string('foo') );
ok( is_string(4) );
my $var = [];
ok( is_string("$var") );
ok( ! is_string($var) );
ok( ! is_string(undef) );

# Test is_string.
ok( to_string(44) eq '44' );
ok( to_string('foo') eq 'foo' );
ok( to_string('') eq '' );
ok( to_string(0) eq '0' );
ok( to_string($var) eq "$var" );
ok( ! defined to_string(undef) );
ok( to_string('hello', 4) eq 'hell' );

__END__