• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moonbitlang / x / 301

10 Dec 2024 06:19AM UTC coverage: 85.204% (-2.6%) from 87.841%
301

Pull #78

github

web-flow
Merge b830031f4 into 91f0fdf48
Pull Request #78: feat: new package encoding

105 of 161 new or added lines in 3 files covered. (65.22%)

124 existing lines in 29 files now uncovered.

1169 of 1372 relevant lines covered (85.2%)

434.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.96
/time/util.mbt
1
// Copyright 2024 International Digital Economy Academy
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
///|
16
fn add_prefix_zero(s : String, len : Int) -> String {
17
  let buf = StringBuilder::new(size_hint=len)
1,302✔
18
  for i = s.length(); i < len; i = i + 1 {
972✔
19
    buf.write_char('0')
972✔
20
  }
21
  buf.write_string(s)
22
  buf.to_string()
23
}
24

25
///|
26
fn remove_prefix_zero(s : String) -> String {
27
  if s.length() == 0 {
4✔
28
    return s
1✔
29
  }
30
  let mut i = 0
31
  while i < s.length() && s[i] == '0' {
8✔
32
    i += 1
5✔
33
  }
34
  if i < s.length() {
35
    s.substring(start=i)
2✔
36
  } else {
37
    s.substring(start=s.length() - 1)
1✔
38
  }
39
}
40

41
test "add_prefix_zero" {
42
  inspect!(add_prefix_zero("", 2), content="00")
43
  inspect!(add_prefix_zero("1", 0), content="1")
44
  inspect!(add_prefix_zero("1", 1), content="1")
45
  inspect!(add_prefix_zero("1", 2), content="01")
46
  inspect!(add_prefix_zero("1", 3), content="001")
47
}
48

49
test "remove_prefix_zero" {
50
  inspect!(remove_prefix_zero(""), content="")
51
  inspect!(remove_prefix_zero("01"), content="1")
52
  inspect!(remove_prefix_zero("09"), content="9")
53
  inspect!(remove_prefix_zero("000"), content="0")
54
}
55

56
///|
57
fn add_suffix_zero(s : String, len : Int) -> String {
58
  let buf = StringBuilder::new(size_hint=len)
11✔
59
  buf.write_string(s)
60
  for i = s.length(); i < len; i = i + 1 {
31✔
61
    buf.write_char('0')
31✔
62
  }
63
  buf.to_string()
64
}
65

66
///|
67
fn remove_suffix_zero(s : String) -> String {
68
  if s.length() == 0 {
67✔
69
    return s
1✔
70
  }
71
  let mut i = s.length() - 1
72
  while s[i] == '0' && i > 0 {
205✔
73
    i -= 1
139✔
74
  }
75
  s.substring(end=i + 1)
76
}
77

78
test "add_suffix_zero" {
79
  inspect!(add_suffix_zero("1", 0), content="1")
80
  inspect!(add_suffix_zero("1", 1), content="1")
81
  inspect!(add_suffix_zero("1", 2), content="10")
82
  inspect!(add_suffix_zero("1", 3), content="100")
83
}
84

85
test "remove_suffix_zero" {
86
  inspect!(remove_suffix_zero(""), content="")
87
  inspect!(remove_suffix_zero("1000100"), content="10001")
88
  inspect!(remove_suffix_zero("000"), content="0")
89
}
90

91
///| FIXME: use split method of String
92
fn split(s : String, delimiter : Char) -> Array[String] {
93
  let spl = []
36✔
94
  if s.length() == 0 {
95
    return spl
1✔
96
  }
97
  let buf = StringBuilder::new(size_hint=0)
98
  for i = 0; i < s.length(); i = i + 1 {
347✔
99
    if s[i] == delimiter {
347✔
100
      spl.push(buf.to_string())
45✔
101
      buf.reset()
102
    } else {
103
      buf.write_char(s[i])
302✔
104
    }
105
  }
106
  spl.push(buf.to_string())
107
  spl
108
}
109

110
test "split" {
111
  inspect!(
112
    split("2000-01-01", '-'),
113
    content=
114
      #|["2000", "01", "01"]
115
    ,
116
  )
117
  inspect!(
118
    split("-01-01", '-'),
119
    content=
120
      #|["", "01", "01"]
121
    ,
122
  )
123
  inspect!(
124
    split("12-31", '-'),
125
    content=
126
      #|["12", "31"]
127
    ,
128
  )
129
  inspect!(
130
    split("-01", '-'),
131
    content=
132
      #|["", "01"]
133
    ,
134
  )
135
  inspect!(
136
    split("-", '-'),
137
    content=
138
      #|["", ""]
139
    ,
140
  )
141
  inspect!(split("", '-'), content="[]")
142
  inspect!(
143
    split("0", '-'),
144
    content=
145
      #|["0"]
146
    ,
147
  )
148
}
149

150
///|
151
fn int_overflow_err[T]() -> T!Error {
152
  raise Failure("Int overflow")
11✔
153
}
154

155
///|
156
fn int64_overflow_err[T]() -> T!Error {
157
  raise Failure("Int64 overflow")
26✔
158
}
159

160
///| FIXME: use checked_add method of Int
161
fn checked_add_int(x : Int, y : Int) -> Int!Error {
162
  let r = x.to_int64() + y.to_int64()
35✔
163
  if r < @int.min_value.to_int64() || r > @int.max_value.to_int64() {
UNCOV
164
    int_overflow_err!()
×
165
  } else {
166
    r.to_int()
29✔
167
  }
168
}
169

170
///| FIXME: use checked_mul method of Int
171
fn checked_mul_int(x : Int, y : Int) -> Int!Error {
172
  let r = x.to_int64() * y.to_int64()
51✔
173
  if r < @int.min_value.to_int64() || r > @int.max_value.to_int64() {
UNCOV
174
    int_overflow_err!()
×
175
  } else {
176
    r.to_int()
46✔
177
  }
178
}
179

180
///| FIXME: use checked_add method of Int64
181
fn checked_add_int64(x : Int64, y : Int64) -> Int64!Error {
182
  let r = x + y
234✔
183
  // Overflow iff both arguments have the opposite sign of the result
184
  if ((x ^ r) & (y ^ r)) < 0L {
UNCOV
185
    int64_overflow_err!()
×
186
  } else {
187
    r
220✔
188
  }
189
}
190

191
///| FIXME: use checked_mul method of Int64
192
fn checked_mul_int64(x : Int64, y : Int64) -> Int64!Error {
193
  let r = x * y
134✔
194
  let abs_x = x.abs()
195
  let abs_y = y.abs()
196
  if ((abs_x ^ abs_y).reinterpret_as_uint64() >> 31).reinterpret_as_int64() !=
197
    0L {
198
    // bits that greater than 2^31 might cause overflow
199
    if (y != 0L && r / y != x) || (x == @int64.min_value && y == -1L) {
16✔
UNCOV
200
      int64_overflow_err!()
×
201
    }
202
  }
203
  r
204
}
205

206
test "checked_add_int" {
207
  inspect!(checked_add_int?(1, 1), content="Ok(2)")
208
  inspect!(checked_add_int?(1, -1), content="Ok(0)")
209
  inspect!(checked_add_int?(-1, -1), content="Ok(-2)")
210
  inspect!(
211
    checked_add_int?(@int.max_value, 1),
212
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
213
  )
214
  inspect!(
215
    checked_add_int?(@int.min_value, -1),
216
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
217
  )
218
}
219

220
test "checked_mul_int" {
221
  inspect!(checked_mul_int?(2, 3), content="Ok(6)")
222
  inspect!(checked_mul_int?(2, -3), content="Ok(-6)")
223
  inspect!(checked_mul_int?(-2, -3), content="Ok(6)")
224
  inspect!(
225
    checked_mul_int?(@int.max_value, 2),
226
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
227
  )
228
  inspect!(
229
    checked_mul_int?(@int.min_value, 2),
230
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
231
  )
232
}
233

234
test "checked_add_int64" {
235
  inspect!(checked_add_int64?(1L, 1L), content="Ok(2)")
236
  inspect!(checked_add_int64?(1L, -1L), content="Ok(0)")
237
  inspect!(checked_add_int64?(-1L, -1L), content="Ok(-2)")
238
  inspect!(
239
    checked_add_int64?(1L, @int64.max_value),
240
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
241
  )
242
  inspect!(
243
    checked_add_int64?(-1L, @int64.min_value),
244
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
245
  )
246
}
247

248
test "checked_mul_int64" {
249
  inspect!(checked_mul_int64?(2L, 3L), content="Ok(6)")
250
  inspect!(checked_mul_int64?(2L, -3L), content="Ok(-6)")
251
  inspect!(checked_mul_int64?(-2L, -3L), content="Ok(6)")
252
  inspect!(
253
    checked_mul_int64?(@int64.max_value, 2L),
254
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
255
  )
256
  inspect!(
257
    checked_mul_int64?(@int64.min_value, 2L),
258
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
259
  )
260
}
261

262
///|
263
fn floor(x : Double) -> Double {
264
  if x.is_pos_inf() || x.is_neg_inf() || x.is_nan() {
88✔
UNCOV
265
    return x
×
266
  }
267
  let n = x.to_int64()
268
  let d = n.to_double()
269
  if x >= 0.0 || x == d {
270
    d
80✔
271
  } else {
272
    d - 1.0
8✔
273
  }
274
}
275

276
///|
277
fn floor_div_int(x : Int, y : Int) -> Int!Error {
278
  if y == 0 {
12✔
279
    raise Failure("division by zero")
2✔
280
  }
281
  let r = x.to_double() / y.to_double()
282
  floor(r).to_int()
283
}
284

285
test "floor_div_int" {
286
  inspect!(floor_div_int?(1, 10), content="Ok(0)")
287
  inspect!(floor_div_int?(-1, 10), content="Ok(-1)")
288
  inspect!(floor_div_int?(9, 10), content="Ok(0)")
289
  inspect!(floor_div_int?(-9, 10), content="Ok(-1)")
290
  inspect!(floor_div_int?(0, 10), content="Ok(0)")
291
  inspect!(
292
    floor_div_int?(10, 0),
293
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
294
  )
295
}
296

297
///|
298
fn floor_div_int64(x : Int64, y : Int64) -> Int64!Error {
299
  if y == 0L {
80✔
UNCOV
300
    fail!("division by zero")
×
301
  }
302
  let r = x.to_double() / y.to_double()
303
  floor(r).to_int64()
304
}
305

306
test "floor_div_int64" {
307
  inspect!(floor_div_int64?(1L, 10L), content="Ok(0)")
308
  inspect!(floor_div_int64?(-1L, 10L), content="Ok(-1)")
309
  inspect!(floor_div_int64?(9L, 10L), content="Ok(0)")
310
  inspect!(floor_div_int64?(-9L, 10L), content="Ok(-1)")
311
  inspect!(floor_div_int64?(0L, 10L), content="Ok(0)")
312
  inspect!(
313
    floor_div_int64?(10L, 0L),
314
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
315
  )
316
}
317

318
///|
319
fn floor_mod_int(x : Int, y : Int) -> Int!Error {
320
  x - floor_div_int!(x, y) * y
5✔
321
}
322

323
test "floor_mod_int" {
324
  inspect!(floor_mod_int?(1, 10), content="Ok(1)")
325
  inspect!(floor_mod_int?(-1, 10), content="Ok(9)")
326
  inspect!(floor_mod_int?(11, 10), content="Ok(1)")
327
  inspect!(floor_mod_int?(-11, 10), content="Ok(9)")
328
  inspect!(floor_mod_int?(0, 10), content="Ok(0)")
329
  inspect!(
330
    floor_mod_int?(10, 0),
331
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
332
  )
333
}
334

335
///|
336
fn floor_mod_int64(x : Int64, y : Int64) -> Int64!Error {
337
  x - floor_div_int64!(x, y) * y
39✔
338
}
339

340
test "floor_mod_int64" {
341
  inspect!(floor_mod_int64?(1L, 10L), content="Ok(1)")
342
  inspect!(floor_mod_int64?(-1L, 10L), content="Ok(9)")
343
  inspect!(floor_mod_int64?(11L, 10L), content="Ok(1)")
344
  inspect!(floor_mod_int64?(-11L, 10L), content="Ok(9)")
345
  inspect!(floor_mod_int64?(0L, 10L), content="Ok(0)")
346
  inspect!(
347
    floor_mod_int64?(10L, 0L),
348
    content="Err(moonbitlang/core/builtin.Failure.Failure)",
349
  )
350
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc