Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

gleam / compiler-core / src / javascript / tests / numbers.rs
6.8 kB 561 lines
1use crate::{assert_js, assert_js_module_error}; 2 3#[test] 4fn int_literals() { 5 assert_js!( 6 r#" 7pub fn go() { 8 1 9 2 10 -3 11 4001 12 0b00001111 13 0o17 14 0xF 15 1_000 16} 17"#, 18 ); 19} 20 21#[test] 22fn float_literals() { 23 assert_js!( 24 r#" 25pub fn go() { 26 1.5 27 2.0 28 -0.1 29 1. 30} 31"#, 32 ); 33} 34 35#[test] 36fn float_scientific_literals() { 37 assert_js!( 38 r#" 39pub fn go() { 40 0.01e-1 41 0.01e-0 42 -10.01e-1 43 -10.01e-0 44 -100.001e-523 45 -100.001e-123_456_789 46} 47"#, 48 ); 49} 50 51#[test] 52fn int_operators() { 53 assert_js!( 54 r#" 55pub fn go() { 56 1 + 1 // => 2 57 5 - 1 // => 4 58 5 / 2 // => 2 59 3 * 3 // => 9 60 5 % 2 // => 1 61 2 > 1 // => True 62 2 < 1 // => False 63 2 >= 1 // => True 64 2 <= 1 // => False 65} 66"#, 67 ); 68} 69 70#[test] 71fn int_divide_complex_expr() { 72 assert_js!( 73 r#" 74pub fn go() { 75 case 1 >= 0 { 76 True -> 2 77 False -> 4 78 } / 2 79} 80"#, 81 ); 82} 83 84#[test] 85fn int_mod_complex_expr() { 86 assert_js!( 87 r#" 88pub fn go() { 89 case 1 >= 0 { 90 True -> 2 91 False -> 4 92 } % 2 93} 94"#, 95 ); 96} 97 98#[test] 99fn float_operators() { 100 assert_js!( 101 r#" 102pub fn go() { 103 1.0 +. 1.4 // => 2.4 104 5.0 -. 1.5 // => 3.5 105 5.0 /. 2.0 // => 2.5 106 3.0 *. 3.1 // => 9.3 107 108 2.0 >. 1.0 // => True 109 2.0 <. 1.0 // => False 110 2.0 >=. 1.0 // => True 111 2.0 <=. 1.0 // => False 112} 113"#, 114 ); 115} 116 117#[test] 118fn float_divide_complex_expr() { 119 assert_js!( 120 r#" 121pub fn go() { 122 case 1.0 >=. 0.0 { 123 True -> 2.0 124 False -> 4.0 125 } /. 2.0 126} 127"#, 128 ); 129} 130 131#[test] 132fn wide_float_div() { 133 assert_js!( 134 r#" 135pub fn go() { 136 111111111111111111111111111111. /. 22222222222222222222222222222222222. 137} 138"#, 139 ); 140} 141 142#[test] 143fn int_patterns() { 144 assert_js!( 145 r#" 146pub fn go(x) { 147 let assert 4 = x 148} 149"#, 150 ); 151} 152 153#[test] 154fn int_equality() { 155 assert_js!( 156 r#" 157pub fn go() { 158 1 != 2 159 1 == 2 160} 161"#, 162 ); 163} 164 165#[test] 166fn int_equality1() { 167 assert_js!( 168 r#" 169pub fn go(y) { 170 let x = 1 171 x == y 172} 173"#, 174 ); 175} 176 177#[test] 178fn float_equality() { 179 assert_js!( 180 r#" 181pub fn go() { 182 1.0 != 2.0 183 1.0 == 2.0 184} 185"#, 186 ); 187} 188 189#[test] 190fn float_equality1() { 191 assert_js!( 192 r#" 193pub fn go(y) { 194 let x = 1.0 195 x == y 196} 197"#, 198 ); 199} 200 201#[test] 202fn operator_precedence() { 203 assert_js!( 204 r#" 205pub fn go() { 206 2.4 *. { 3.5 +. 6.0 } 207} 208"#, 209 ) 210} 211 212#[test] 213fn remainder() { 214 assert_js!( 215 r#" 216pub fn go() { 217 5 % 0 // => 0 218} 219"#, 220 ); 221} 222 223#[test] 224fn int_negation() { 225 assert_js!( 226 r#" 227pub fn go() { 228 let a = 3 229 let b = -a 230} 231"#, 232 ); 233} 234 235#[test] 236fn repeated_int_negation() { 237 assert_js!( 238 r#" 239pub fn go() { 240 let a = 3 241 let b = --a 242} 243"# 244 ); 245} 246 247// https://github.com/gleam-lang/gleam/issues/2412 248#[test] 249fn preceeding_zeros_int() { 250 assert_js!( 251 r#" 252pub fn main() { 253 09_179 254} 255"# 256 ); 257} 258 259// https://github.com/gleam-lang/gleam/issues/2412 260#[test] 261fn preceeding_zeros_float() { 262 assert_js!( 263 r#" 264pub fn main() { 265 09_179.1 266} 267"# 268 ); 269} 270 271// https://github.com/gleam-lang/gleam/issues/2412 272#[test] 273fn preceeding_zeros_int_const() { 274 assert_js!( 275 r#" 276pub const x = 09_179 277"# 278 ); 279} 280 281// https://github.com/gleam-lang/gleam/issues/2412 282#[test] 283fn preceeding_zeros_float_const() { 284 assert_js!( 285 r#" 286pub const x = 09_179.1 287"# 288 ); 289} 290 291// https://github.com/gleam-lang/gleam/issues/2412 292#[test] 293fn preceeding_zeros_int_pattern() { 294 assert_js!( 295 r#" 296pub fn main(x) { 297 let assert 09_179 = x 298} 299"# 300 ); 301} 302 303// https://github.com/gleam-lang/gleam/issues/2412 304#[test] 305fn preceeding_zeros_float_pattern() { 306 assert_js!( 307 r#" 308pub fn main(x) { 309 let assert 09_179.1 = x 310} 311"# 312 ); 313} 314 315// https://github.com/gleam-lang/gleam/issues/4459 316#[test] 317fn underscore_after_hexadecimal_prefix() { 318 assert_js!( 319 " 320pub fn main() { 321 0x_12_34 322} 323" 324 ); 325} 326 327// https://github.com/gleam-lang/gleam/issues/4459 328#[test] 329fn underscore_after_octal_prefix() { 330 assert_js!( 331 " 332pub fn main() { 333 0o_12_34 334} 335" 336 ); 337} 338 339// https://github.com/gleam-lang/gleam/issues/4459 340#[test] 341fn underscore_after_binary_prefix() { 342 assert_js!( 343 " 344pub fn main() { 345 0b_10_01 346} 347" 348 ); 349} 350 351// https://github.com/gleam-lang/gleam/issues/4481 352#[test] 353fn underscore_after_zero_after_hex_prefix() { 354 assert_js!( 355 " 356pub fn main() { 357 0x0_1_2_3 358} 359" 360 ); 361} 362 363// https://github.com/gleam-lang/gleam/issues/4481 364#[test] 365fn underscore_after_zero_after_octal_prefix() { 366 assert_js!( 367 " 368pub fn main() { 369 0o0_1_2_3 370} 371" 372 ); 373} 374 375// https://github.com/gleam-lang/gleam/issues/4481 376#[test] 377fn underscore_after_zero_after_binary_prefix() { 378 assert_js!( 379 " 380pub fn main() { 381 0b0_1_0_1 382} 383" 384 ); 385} 386 387// https://github.com/gleam-lang/gleam/issues/4481 388#[test] 389fn underscore_after_zero() { 390 assert_js!( 391 " 392pub fn main() { 393 0_1_2_3 394} 395" 396 ); 397} 398 399#[test] 400fn zero_after_underscore_after_hex_prefix() { 401 assert_js!( 402 " 403pub fn main() { 404 0x_0_1_2_3 405} 406" 407 ); 408} 409 410#[test] 411fn zero_after_underscore_after_octal_prefix() { 412 assert_js!( 413 " 414pub fn main() { 415 0o_0_1_2_3 416} 417" 418 ); 419} 420 421#[test] 422fn zero_after_underscore_after_binary_prefix() { 423 assert_js!( 424 " 425pub fn main() { 426 0b_0_1_0_1 427} 428" 429 ); 430} 431 432#[test] 433fn underscore_after_decimal_point() { 434 assert_js!( 435 " 436pub fn main() { 437 0._1 438} 439" 440 ); 441} 442 443#[test] 444fn underscore_after_decimal_point_case_statement() { 445 assert_js!( 446 " 447pub fn main(x) { 448 case x { 449 0._1 -> \"bar\" 450 _ -> \"foo\" 451 } 452} 453" 454 ); 455} 456 457#[test] 458fn inf_float_case_statement() { 459 assert_js_module_error!( 460 " 461pub fn main(x) { 462 case x { 463 100.001e123_456_789 -> \"bar\" 464 _ -> \"foo\" 465 } 466} 467" 468 ); 469} 470 471#[test] 472fn division_inf_by_inf_float() { 473 assert_js_module_error!( 474 " 475pub fn main(x) { 476 -100.001e123_456_789 /. 100.001e123_456_789 477} 478" 479 ); 480} 481 482#[test] 483fn division_by_zero_float() { 484 assert_js!( 485 "pub fn main() { 486 1.1 /. 0.0 487}" 488 ) 489} 490 491#[test] 492fn division_by_non_zero_float() { 493 assert_js!( 494 "pub fn main() { 495 1.1 /. 2.3 496}" 497 ) 498} 499 500#[test] 501fn complex_division_by_non_zero_float() { 502 assert_js!( 503 "pub fn main() { 504 { 1.1 +. 2.0 } /. 2.3 505}" 506 ) 507} 508 509#[test] 510fn division_by_zero_int() { 511 assert_js!( 512 "pub fn main() { 513 1 / 0 514}" 515 ) 516} 517 518#[test] 519fn division_by_non_zero_int() { 520 assert_js!( 521 "pub fn main() { 522 1 / 2 523}" 524 ) 525} 526 527#[test] 528fn complex_division_by_non_zero_int() { 529 assert_js!( 530 "pub fn main() { 531 { 1 + 2 } / 3 532}" 533 ) 534} 535 536#[test] 537fn remainder_by_zero_int() { 538 assert_js!( 539 "pub fn main() { 540 1 % 0 541}" 542 ) 543} 544 545#[test] 546fn remainder_by_non_zero_int() { 547 assert_js!( 548 "pub fn main() { 549 1 % 2 550}" 551 ) 552} 553 554#[test] 555fn complex_remainder_by_non_zero_int() { 556 assert_js!( 557 "pub fn main() { 558 { 1 + 2 } % 3 559}" 560 ) 561}