aboutsummaryrefslogtreecommitdiff
path: root/src/test/run-pass/syntax-extension-fmt.rs
blob: abe3a30acf253ba15164214b99480a8c9f09725d (plain) (blame)
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
// xfail-boot
// xfail-stage0
use std;
import std._str;

fn test(str actual, str expected) {
  log actual;
  log expected;
  check (_str.eq(actual, expected));
}

fn main() {
  test(#fmt("hello %d friends and %s things", 10, "formatted"),
    "hello 10 friends and formatted things");

  // Simple tests for types
  test(#fmt("%d", 1), "1");
  test(#fmt("%i", 2), "2");
  test(#fmt("%i", -1), "-1");
  test(#fmt("%u", 10u), "10");
  test(#fmt("%s", "test"), "test");
  test(#fmt("%b", true), "true");
  test(#fmt("%b", false), "false");
  test(#fmt("%c", 'A'), "A");
  test(#fmt("%x", 0xff_u), "ff");
  test(#fmt("%X", 0x12ab_u), "12AB");
  test(#fmt("%t", 0b11010101_u), "11010101");

  // 32-bit limits
  test(#fmt("%i", -2147483648), "-2147483648");
  test(#fmt("%i", 2147483647), "2147483647");
  test(#fmt("%u", 4294967295u), "4294967295");
  test(#fmt("%x", 0xffffffff_u), "ffffffff");
  test(#fmt("%t", 0xffffffff_u), "11111111111111111111111111111111");

  // Widths
  test(#fmt("%1d", 500), "500");
  test(#fmt("%10d", 500), "       500");
  test(#fmt("%10d", -500), "      -500");
  test(#fmt("%10u", 500u), "       500");
  test(#fmt("%10s", "test"), "      test");
  test(#fmt("%10b", true), "      true");
  test(#fmt("%10x", 0xff_u), "        ff");
  test(#fmt("%10X", 0xff_u), "        FF");
  test(#fmt("%10t", 0xff_u), "  11111111");

  // Left justify
  test(#fmt("%-10d", 500), "500       ");
  test(#fmt("%-10d", -500), "-500      ");
  test(#fmt("%-10u", 500u), "500       ");
  test(#fmt("%-10s", "test"), "test      ");
  test(#fmt("%-10b", true), "true      ");
  test(#fmt("%-10x", 0xff_u), "ff        ");
  test(#fmt("%-10X", 0xff_u), "FF        ");
  test(#fmt("%-10t", 0xff_u), "11111111  ");

  // Precision
//   test(#fmt("%.d", 0), "");
//   test(#fmt("%.u", 0u), "");
//   test(#fmt("%.x", 0u), "");
//   test(#fmt("%.d", 10), "10");
//   test(#fmt("%.d", -10), "-10");
//   test(#fmt("%.u", 10u), "10");
   test(#fmt("%.s", "test"), "");
//   test(#fmt("%.x", 127u), "7f");

//   test(#fmt("%.0d", 0), "");
//   test(#fmt("%.0u", 0u), "");
//   test(#fmt("%.0x", 0u), "");
//   test(#fmt("%.0d", 10), "10");
//   test(#fmt("%.0d", -10), "-10");
//   test(#fmt("%.0u", 10u), "10");
   test(#fmt("%.0s", "test"), "");
//   test(#fmt("%.0x", 127u), "7f");

//   test(#fmt("%.1d", 0), "0");
//   test(#fmt("%.1u", 0u), "0");
//   test(#fmt("%.1x", 0u), "0");
//   test(#fmt("%.1d", 10), "10");
//   test(#fmt("%.1d", -10), "-10");
//   test(#fmt("%.1u", 10u), "10");
   test(#fmt("%.1s", "test"), "t");
//   test(#fmt("%.1x", 127u), "7f");

//   test(#fmt("%.5d", 0), "00000");
//   test(#fmt("%.5u", 0u), "00000");
//   test(#fmt("%.5x", 0u), "00000");
//   test(#fmt("%.5d", 10), "00010");
//   test(#fmt("%.5d", -10), "-00010");
//   test(#fmt("%.5u", 10u), "00010");
   test(#fmt("%.5s", "test"), "test");
//   test(#fmt("%.5x", 127u), "0007f");
}