Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_stdio.d

raw
1 @system unittest
2 {
3 import std.stdio;
4
5 static import std.file;
6
7 auto testFile = std.file.deleteme();
8 std.file.write(testFile, "\r\n\n\r\n");
9 scope(exit) std.file.remove(testFile);
10
11 auto f = File(testFile, "r");
12 auto buf = f.rawRead(new char[5]);
13 f.close();
14 assert(buf == "\r\n\n\r\n");
15
16 }
17
18 @system unittest
19 {
20 import std.stdio;
21
22 static import std.file;
23
24 auto testFile = std.file.deleteme();
25 auto f = File(testFile, "w");
26 scope(exit) std.file.remove(testFile);
27
28 f.rawWrite("\r\n\n\r\n");
29 f.close();
30 assert(std.file.read(testFile) == "\r\n\n\r\n");
31
32 }
33
34 @system unittest
35 {
36 import std.stdio;
37
38 import std.conv : text;
39 static import std.file;
40
41 auto testFile = std.file.deleteme();
42 std.file.write(testFile, "abcdefghijklmnopqrstuvwqxyz");
43 scope(exit) { std.file.remove(testFile); }
44
45 auto f = File(testFile);
46 auto a = new ubyte[4];
47 f.rawRead(a);
48 assert(f.tell == 4, text(f.tell));
49
50 }
51
52 @system unittest
53 {
54 import std.stdio;
55
56 static import std.file;
57
58 auto deleteme = std.file.deleteme();
59 std.file.write(deleteme, "hello\nworld\ntrue\nfalse\n");
60 scope(exit) std.file.remove(deleteme);
61 string s;
62 auto f = File(deleteme);
63 f.readf!"%s\n"(s);
64 assert(s == "hello", "["~s~"]");
65 f.readf("%s\n", s);
66 assert(s == "world", "["~s~"]");
67
68 bool b1, b2;
69 f.readf("%s\n%s\n", b1, b2);
70 assert(b1 == true && b2 == false);
71
72 }
73
74 @system unittest
75 {
76 import std.stdio;
77
78 static import std.file;
79 import std.typecons : tuple;
80
81 // prepare test file
82 auto testFile = std.file.deleteme();
83 scope(failure) printf("Failed test at line %d\n", __LINE__);
84 std.file.write(testFile, "1 2\n4 1\n5 100");
85 scope(exit) std.file.remove(testFile);
86
87 File f = File(testFile);
88 scope(exit) f.close();
89
90 auto expected = [tuple(1, 2), tuple(4, 1), tuple(5, 100)];
91 uint i;
92 foreach (e; f.byRecord!(int, int)("%s %s"))
93 {
94 assert(e == expected[i++]);
95 }
96
97 }
98
99 @safe unittest
100 {
101 import std.stdio;
102
103 static assert(isFileHandle!(FILE*));
104 static assert(isFileHandle!(File));
105 }
106
107 @safe unittest
108 {
109 import std.stdio;
110
111 // Read stdin, sort lines, write to stdout
112 import std.algorithm.mutation : copy;
113 import std.algorithm.sorting : sort;
114 import std.array : array;
115 import std.typecons : Yes;
116
117 void main()
118 {
119 stdin // read from stdin
120 .byLineCopy(Yes.keepTerminator) // copying each line
121 .array() // convert to array of lines
122 .sort() // sort the lines
123 .copy( // copy output of .sort to an OutputRange
124 stdout.lockingTextWriter()); // the OutputRange
125 }
126 }
127
128 @safe unittest
129 {
130 import std.stdio;
131
132 void main()
133 {
134 stdout.writeln("Write a message to stdout.");
135 }
136 }
137
138 @safe unittest
139 {
140 import std.stdio;
141
142 void main()
143 {
144 import std.algorithm.iteration : filter, map, sum;
145 import std.format : format;
146 import std.range : iota, tee;
147
148 int len;
149 const r = 6.iota
150 .filter!(a => a % 2) // 1 3 5
151 .map!(a => a * 2) // 2 6 10
152 .tee!(_ => stdout.writefln("len: %d", len++))
153 .sum;
154
155 assert(r == 18);
156 }
157 }
158
159 @safe unittest
160 {
161 import std.stdio;
162
163 void main()
164 {
165 import std.algorithm.mutation : copy;
166 import std.algorithm.iteration : map;
167 import std.format : format;
168 import std.range : iota;
169
170 10.iota
171 .map!(e => "N: %d".format(e))
172 .copy(stdout.lockingTextWriter()); // the OutputRange
173 }
174 }
175
176 @safe unittest
177 {
178 import std.stdio;
179
180 void main()
181 {
182 stderr.writeln("Write a message to stderr.");
183 }
184 }
185