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