mirror of
https://github.com/fmtlib/fmt.git
synced 2025-12-24 07:48:18 +01:00
Compare commits
973 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b097da31e | ||
|
|
f9e98c2007 | ||
|
|
2d80bcc1cb | ||
|
|
fa5b35e539 | ||
|
|
549b61e958 | ||
|
|
2d200e3caa | ||
|
|
3252eead94 | ||
|
|
d82b6c7e62 | ||
|
|
55ba812d12 | ||
|
|
5463a62d43 | ||
|
|
9350328086 | ||
|
|
9d1fc0ecd5 | ||
|
|
9c006520bd | ||
|
|
8ad6f93428 | ||
|
|
72c97ab4ab | ||
|
|
bcc2fd1dbe | ||
|
|
a89f578c14 | ||
|
|
6005907db6 | ||
|
|
53dd7d1583 | ||
|
|
65a8a7f85e | ||
|
|
89e0684da6 | ||
|
|
a5c6975118 | ||
|
|
ec7b006fab | ||
|
|
c1f8274701 | ||
|
|
91d9497911 | ||
|
|
3453df6d65 | ||
|
|
648c730bd2 | ||
|
|
8abc15f85e | ||
|
|
4ff6ab396c | ||
|
|
14d1fb561f | ||
|
|
7ae6dfbcba | ||
|
|
bdada10c2c | ||
|
|
65cf5414c9 | ||
|
|
cb40e7c60b | ||
|
|
3a9b7ad8b6 | ||
|
|
38b5f573dd | ||
|
|
5ad6c6bd7f | ||
|
|
85ef48cb6d | ||
|
|
a6864a7264 | ||
|
|
0dd1f19281 | ||
|
|
7c8f915f0b | ||
|
|
e1b62a5d39 | ||
|
|
4d208ae86f | ||
|
|
29451ea8e2 | ||
|
|
e6113fb9f5 | ||
|
|
7c0a23329e | ||
|
|
f1e77e41a5 | ||
|
|
267d4542bb | ||
|
|
ee5443e920 | ||
|
|
103530d407 | ||
|
|
efc42fbd4e | ||
|
|
7c0d5756ff | ||
|
|
91a7ee6c20 | ||
|
|
74512aa282 | ||
|
|
beaf6f7436 | ||
|
|
63b4f0ae1a | ||
|
|
5bea8a649b | ||
|
|
2e3423b053 | ||
|
|
1244f3d58f | ||
|
|
656f0895ed | ||
|
|
5a51aac85e | ||
|
|
c3e845ab7e | ||
|
|
ce4d3e175c | ||
|
|
fb86e4f6fa | ||
|
|
da6ac7fb26 | ||
|
|
07754c047c | ||
|
|
67089022bf | ||
|
|
4df5d10e3c | ||
|
|
8a915b9b8d | ||
|
|
8f1e5df672 | ||
|
|
c09c42f9a7 | ||
|
|
86a7d856fa | ||
|
|
391d6430e5 | ||
|
|
a8a7534f78 | ||
|
|
110d4c0ab0 | ||
|
|
17b9e4db8c | ||
|
|
52f44bf114 | ||
|
|
fede517b22 | ||
|
|
c746122623 | ||
|
|
ed490145d8 | ||
|
|
f3fe82415d | ||
|
|
d5602d9fb6 | ||
|
|
7716157bdb | ||
|
|
e70af5b99e | ||
|
|
f014d32147 | ||
|
|
bf14b2c41b | ||
|
|
f6cffb5eeb | ||
|
|
c634a5bf2b | ||
|
|
f8fd34ac30 | ||
|
|
c2bcb31354 | ||
|
|
7215c3446b | ||
|
|
642b72cd91 | ||
|
|
89fed501d4 | ||
|
|
28fd7fb159 | ||
|
|
85833c90ee | ||
|
|
4da1fa391e | ||
|
|
de92f5d8a7 | ||
|
|
d06ff7a7e8 | ||
|
|
a81baf2904 | ||
|
|
810c734c3f | ||
|
|
524222f094 | ||
|
|
016adee06c | ||
|
|
2b35c9e8e7 | ||
|
|
cbc46d9e78 | ||
|
|
e87a24633b | ||
|
|
14eae59c6a | ||
|
|
2418a84f02 | ||
|
|
27e0bf72c0 | ||
|
|
939193be21 | ||
|
|
00e3ae508e | ||
|
|
bc33d17ce3 | ||
|
|
65cd4835cd | ||
|
|
53010624a0 | ||
|
|
a92205be9c | ||
|
|
a16ca84346 | ||
|
|
f40f790159 | ||
|
|
88835f8468 | ||
|
|
5b0a9bbe85 | ||
|
|
6a52fc164a | ||
|
|
b4df6cb2fa | ||
|
|
932eb9777e | ||
|
|
4a1974577f | ||
|
|
dfad5f1d39 | ||
|
|
3f574c1611 | ||
|
|
8d07db0e0e | ||
|
|
bd133382f0 | ||
|
|
040b8e7e06 | ||
|
|
d9ef0e0a44 | ||
|
|
6c656663f3 | ||
|
|
291e43abac | ||
|
|
a85d5e962b | ||
|
|
11fab458c4 | ||
|
|
829cd70cd2 | ||
|
|
1d49443405 | ||
|
|
9fd566412d | ||
|
|
9331533309 | ||
|
|
43b9fefbd2 | ||
|
|
0fc15d48f3 | ||
|
|
2eef573656 | ||
|
|
f77350f8fa | ||
|
|
5282bebbd7 | ||
|
|
e2583ab5f3 | ||
|
|
950e6b9753 | ||
|
|
c110f7b8d3 | ||
|
|
86fee707fb | ||
|
|
c643fdae53 | ||
|
|
609f33c94a | ||
|
|
2f05eaa07d | ||
|
|
2ced92529d | ||
|
|
dd2e8b98de | ||
|
|
f79c88c190 | ||
|
|
4f51033aa9 | ||
|
|
503e87f153 | ||
|
|
3822241d99 | ||
|
|
e392eea160 | ||
|
|
8273855067 | ||
|
|
8bd1953094 | ||
|
|
33f85efb55 | ||
|
|
7b77eb61b5 | ||
|
|
50151ff804 | ||
|
|
4a11f70408 | ||
|
|
01b00e6dfb | ||
|
|
a32f9148fc | ||
|
|
a95b3f7ee1 | ||
|
|
04a21bbb2e | ||
|
|
618dd9d6af | ||
|
|
a92c179a6d | ||
|
|
267382af39 | ||
|
|
cfd476bb8b | ||
|
|
e875e3145f | ||
|
|
9368b6a4ee | ||
|
|
7004d1edf6 | ||
|
|
d5ab6ba728 | ||
|
|
bc78ca42a9 | ||
|
|
4806f75469 | ||
|
|
5a9dc8f991 | ||
|
|
26c151c5e4 | ||
|
|
bce73e2bd8 | ||
|
|
51ceef3dee | ||
|
|
326ade7698 | ||
|
|
502d2ede8a | ||
|
|
81dc04dd48 | ||
|
|
fe2b5351be | ||
|
|
d7fea1ba0c | ||
|
|
37ad436068 | ||
|
|
45d70b71ec | ||
|
|
d930f6956f | ||
|
|
38a7406318 | ||
|
|
83f5085551 | ||
|
|
554abb12f1 | ||
|
|
f2c16957f7 | ||
|
|
48e6df117a | ||
|
|
59254410da | ||
|
|
58a300c82a | ||
|
|
710817befb | ||
|
|
ba17cca432 | ||
|
|
f82f4bd79b | ||
|
|
589b73f646 | ||
|
|
afba2422ae | ||
|
|
58683f5376 | ||
|
|
bc6fcd90cf | ||
|
|
402f5f3178 | ||
|
|
bdeffc3fdc | ||
|
|
b4eef59100 | ||
|
|
90bc8dd3ed | ||
|
|
237eaa53e1 | ||
|
|
531ee615af | ||
|
|
fae27d262c | ||
|
|
0bd32f3f29 | ||
|
|
43aebf51d6 | ||
|
|
757a523195 | ||
|
|
7f706171a9 | ||
|
|
cd828a8691 | ||
|
|
6080108802 | ||
|
|
e8febf7f4b | ||
|
|
130bccbcbf | ||
|
|
01414ac6ec | ||
|
|
3cb004d0d3 | ||
|
|
a7d7e0daec | ||
|
|
67e196eac4 | ||
|
|
fdecd5b40f | ||
|
|
a8b1769803 | ||
|
|
8d89e56d2c | ||
|
|
fb12cfd196 | ||
|
|
ecd2b80e8f | ||
|
|
c95908bd8c | ||
|
|
c630d1f236 | ||
|
|
0d1eb8b1ea | ||
|
|
dff2137405 | ||
|
|
2523f3b939 | ||
|
|
f9fc8fd09d | ||
|
|
b49a1b4a52 | ||
|
|
df580969f7 | ||
|
|
f9c3ff244d | ||
|
|
6e3d7d9a6a | ||
|
|
54f19ff7b2 | ||
|
|
f3bd9d4241 | ||
|
|
6f3c095ed6 | ||
|
|
0fea89ec8f | ||
|
|
7706e1054d | ||
|
|
a23fbbb9a3 | ||
|
|
838dc1e189 | ||
|
|
820c03a473 | ||
|
|
311251eb91 | ||
|
|
c2a6903e19 | ||
|
|
d9c605c61f | ||
|
|
334fc54b5d | ||
|
|
bd0067ee76 | ||
|
|
4c59aa8780 | ||
|
|
aca49379a5 | ||
|
|
615c1eef6b | ||
|
|
8ea9f068c7 | ||
|
|
97ea39cf79 | ||
|
|
43d3c0a110 | ||
|
|
f02e4100db | ||
|
|
e41ca7b951 | ||
|
|
6527551d43 | ||
|
|
9cbb25e43f | ||
|
|
10698cc44d | ||
|
|
aa78f6155b | ||
|
|
6bcfb377b2 | ||
|
|
bbd7681a33 | ||
|
|
8f93e84a1a | ||
|
|
ed92af29b3 | ||
|
|
14984acd27 | ||
|
|
daca48cb15 | ||
|
|
cabe5bddb4 | ||
|
|
5a5870d3ca | ||
|
|
5c4b6678e4 | ||
|
|
10d83aaa52 | ||
|
|
d5a6316d5c | ||
|
|
9501ca0e68 | ||
|
|
06ae9a6de0 | ||
|
|
c50e7781fc | ||
|
|
e3263a341e | ||
|
|
adca25913c | ||
|
|
99ff512b32 | ||
|
|
0019793786 | ||
|
|
e640e458c1 | ||
|
|
fb6ed7590b | ||
|
|
e9bf788bd5 | ||
|
|
4a818ac785 | ||
|
|
c90c6805a2 | ||
|
|
7d2c3ac0f2 | ||
|
|
32392597aa | ||
|
|
2b17d47372 | ||
|
|
ca94e4d27f | ||
|
|
c752b6d489 | ||
|
|
4d3126c567 | ||
|
|
755ecb0311 | ||
|
|
d707adce5c | ||
|
|
0d732cf5d3 | ||
|
|
5189274797 | ||
|
|
73ec6504c7 | ||
|
|
88f3f53870 | ||
|
|
709465197a | ||
|
|
76a2ea56ce | ||
|
|
a7520f164a | ||
|
|
bb1926753d | ||
|
|
f05e856017 | ||
|
|
4865950649 | ||
|
|
8cab5909e0 | ||
|
|
80f5cbb9e5 | ||
|
|
833fd47f83 | ||
|
|
8b76e97062 | ||
|
|
55c61d1abd | ||
|
|
b6afd93879 | ||
|
|
1e63fc7ca0 | ||
|
|
32c5dca733 | ||
|
|
43b389f358 | ||
|
|
fae3ae67b9 | ||
|
|
ae413ebf7a | ||
|
|
04d38a8b14 | ||
|
|
03f7f99f4c | ||
|
|
8b9a6e03f6 | ||
|
|
74dc571ba0 | ||
|
|
98a25ec7cb | ||
|
|
313b259891 | ||
|
|
d00f689c40 | ||
|
|
b79d06a5da | ||
|
|
2846a9eb38 | ||
|
|
810f42c0c7 | ||
|
|
55a16eaec7 | ||
|
|
a309c4bbed | ||
|
|
e1c046c984 | ||
|
|
8c4db5081b | ||
|
|
dd90129066 | ||
|
|
d1ded569ff | ||
|
|
5ca3d00e26 | ||
|
|
a9da3d3852 | ||
|
|
b2bd63d2d9 | ||
|
|
65cb43a484 | ||
|
|
c2f02df2f5 | ||
|
|
f43caef311 | ||
|
|
098a8fdef8 | ||
|
|
75a2ea03a7 | ||
|
|
163178eee1 | ||
|
|
ae8bc36e57 | ||
|
|
93b314d3ab | ||
|
|
ec1aea34e6 | ||
|
|
ab6e759291 | ||
|
|
d2973766ea | ||
|
|
beb00edf73 | ||
|
|
49222dc0c7 | ||
|
|
13bbb0db7a | ||
|
|
eac25ad1c7 | ||
|
|
1d4640415d | ||
|
|
14f2577569 | ||
|
|
b9a568b1dd | ||
|
|
70205edd6e | ||
|
|
a734f67978 | ||
|
|
6a98f42336 | ||
|
|
a4998accf6 | ||
|
|
c76b22405d | ||
|
|
2938a92d64 | ||
|
|
b54d6e7124 | ||
|
|
94daf27c9d | ||
|
|
886ad20a9d | ||
|
|
434e83a192 | ||
|
|
ddd95965e1 | ||
|
|
a3062f6647 | ||
|
|
352b6ae496 | ||
|
|
74169e4b5d | ||
|
|
1e9ca17b9d | ||
|
|
225b757b50 | ||
|
|
cb7caa540f | ||
|
|
ea9989b254 | ||
|
|
949c3c5df4 | ||
|
|
e34e9fa0c7 | ||
|
|
f175591923 | ||
|
|
6cd9be2c06 | ||
|
|
3269fce2dc | ||
|
|
d59fbdcd4a | ||
|
|
445663fade | ||
|
|
f2c9df8e9f | ||
|
|
b33d2aa825 | ||
|
|
88e0db8486 | ||
|
|
d4916d9271 | ||
|
|
fe9888a1e2 | ||
|
|
93bcad6d8e | ||
|
|
22f75d8b6d | ||
|
|
82d4d11c11 | ||
|
|
5debb2aa86 | ||
|
|
605d2600f8 | ||
|
|
271fa8c908 | ||
|
|
3947a7a98c | ||
|
|
ab35af53c5 | ||
|
|
e62354a190 | ||
|
|
eab0b577f8 | ||
|
|
e44619707e | ||
|
|
2dad1690c8 | ||
|
|
564da25932 | ||
|
|
108cd1d1a0 | ||
|
|
32344d9b14 | ||
|
|
c57d7a506f | ||
|
|
56fc525e98 | ||
|
|
279c7a6e6a | ||
|
|
526b7fc91d | ||
|
|
9646e38c3b | ||
|
|
da0f7c0a51 | ||
|
|
42de4f1f7d | ||
|
|
8cc0d21124 | ||
|
|
59a462c67d | ||
|
|
45397e5046 | ||
|
|
034aedb6d4 | ||
|
|
dd4323f31c | ||
|
|
f9561671cf | ||
|
|
da0293c4dd | ||
|
|
03cdf1235a | ||
|
|
f1ac0a19e9 | ||
|
|
be00d8b272 | ||
|
|
406c6123fb | ||
|
|
c4a4a05d12 | ||
|
|
f2be7851cc | ||
|
|
e0f92d675e | ||
|
|
39ac84f01e | ||
|
|
186734cf8b | ||
|
|
61857356ea | ||
|
|
2462c61d05 | ||
|
|
adce0245dc | ||
|
|
366e852f00 | ||
|
|
bb01633d19 | ||
|
|
c978d88183 | ||
|
|
fb32161fa4 | ||
|
|
c7cfa7d4e7 | ||
|
|
d4412a01f0 | ||
|
|
1a0c76a81a | ||
|
|
18f1e08685 | ||
|
|
e488a28601 | ||
|
|
ddd087d8d1 | ||
|
|
d3a7039e31 | ||
|
|
5614295aa1 | ||
|
|
7c14431211 | ||
|
|
adbf3f8b86 | ||
|
|
137153b9f0 | ||
|
|
910dec5ed3 | ||
|
|
028d12dacd | ||
|
|
316ae7e244 | ||
|
|
6a8becb5bf | ||
|
|
56fb75c2ec | ||
|
|
bdbacde659 | ||
|
|
3465d056e7 | ||
|
|
e22d657202 | ||
|
|
44e23c39f1 | ||
|
|
431b556951 | ||
|
|
6a37965710 | ||
|
|
566061d1f1 | ||
|
|
6cd1563eac | ||
|
|
e04b07921f | ||
|
|
8b41b09add | ||
|
|
86bbd3c238 | ||
|
|
b797d39dd1 | ||
|
|
fc10d10b6b | ||
|
|
58dfe5c9f7 | ||
|
|
3390a9510e | ||
|
|
5ccbc9bb53 | ||
|
|
a259c941e2 | ||
|
|
be9356b651 | ||
|
|
d81fafc295 | ||
|
|
3f444fe3e2 | ||
|
|
e2f0408b69 | ||
|
|
f4156b57f0 | ||
|
|
39b0930aee | ||
|
|
eeca22357b | ||
|
|
e4c4e4e944 | ||
|
|
be785a8a43 | ||
|
|
d346a4120d | ||
|
|
bf5b246717 | ||
|
|
138c3dccdc | ||
|
|
8f8fd769ee | ||
|
|
24d6baa60f | ||
|
|
481c313df5 | ||
|
|
d8b9f413fe | ||
|
|
ad9c830282 | ||
|
|
7a83dcd23e | ||
|
|
7e40ca6a9c | ||
|
|
4d049cf598 | ||
|
|
75b5eb4b9d | ||
|
|
5d4803a567 | ||
|
|
4c563de76f | ||
|
|
82652f4ee2 | ||
|
|
b9c40c6af5 | ||
|
|
7b44b0113b | ||
|
|
8e0bfb5714 | ||
|
|
2ebfa7b53d | ||
|
|
721d95adf2 | ||
|
|
af90b08b76 | ||
|
|
d658c835d4 | ||
|
|
a8cef1d987 | ||
|
|
1a6cdb53ac | ||
|
|
33cde15334 | ||
|
|
533b9cf24f | ||
|
|
46b6e4b52c | ||
|
|
d91a66e1c5 | ||
|
|
2a1c0c9969 | ||
|
|
f634ccb344 | ||
|
|
b498ba0bc5 | ||
|
|
37356c3f5e | ||
|
|
aa6aa4207e | ||
|
|
2a747b19e0 | ||
|
|
d142e3b55f | ||
|
|
d699c2a0d9 | ||
|
|
a997de90eb | ||
|
|
89468c2a23 | ||
|
|
ee24839cbd | ||
|
|
ca0dcce2a6 | ||
|
|
c1db293518 | ||
|
|
a7d94f0c61 | ||
|
|
177715d8f9 | ||
|
|
f1dfd59a41 | ||
|
|
f86c812e1a | ||
|
|
4edc88f609 | ||
|
|
9144a8fbbe | ||
|
|
e2a66c58e5 | ||
|
|
9d74f9542b | ||
|
|
512e2ced93 | ||
|
|
e3a2ac8725 | ||
|
|
8a66bb89b2 | ||
|
|
a7a18fac89 | ||
|
|
591ad0a64a | ||
|
|
979561cc00 | ||
|
|
4ecfdd50b6 | ||
|
|
c3a6e01f52 | ||
|
|
308ee8a7eb | ||
|
|
eb04e9abd3 | ||
|
|
c0d55e6308 | ||
|
|
47f2e7aa02 | ||
|
|
6eaee58679 | ||
|
|
b119b9ad71 | ||
|
|
59ed28274c | ||
|
|
f29a3fadbf | ||
|
|
8cfe4e2266 | ||
|
|
0a26914e42 | ||
|
|
472e6107bb | ||
|
|
307c207351 | ||
|
|
dfadea8345 | ||
|
|
b1c89e37bf | ||
|
|
dac1aa0d23 | ||
|
|
f7e0fb8a58 | ||
|
|
f739978fbb | ||
|
|
cac3656b78 | ||
|
|
18b4abff0b | ||
|
|
2fb88aaa08 | ||
|
|
bd8f8b5525 | ||
|
|
5e42eaca0d | ||
|
|
e839608dcf | ||
|
|
83c2b37673 | ||
|
|
16985f4764 | ||
|
|
d3a7a4c209 | ||
|
|
53a475ac94 | ||
|
|
897397760c | ||
|
|
9c47f3e057 | ||
|
|
0ffcec0090 | ||
|
|
8321d0ecf9 | ||
|
|
e3a44c11f6 | ||
|
|
ed421848b3 | ||
|
|
1d7bd5d073 | ||
|
|
b9a06bafd8 | ||
|
|
e825156add | ||
|
|
f711266244 | ||
|
|
144e1fbb7c | ||
|
|
270ed1cb92 | ||
|
|
4e260e8599 | ||
|
|
43a873f7ff | ||
|
|
9fbdc32adc | ||
|
|
6e5551e77a | ||
|
|
c494c980b6 | ||
|
|
b827a97fd6 | ||
|
|
5d15bdd5a5 | ||
|
|
530479bd07 | ||
|
|
b8174bec30 | ||
|
|
39166f9aa9 | ||
|
|
2dc108b31f | ||
|
|
ccf1dbb139 | ||
|
|
cff1c79469 | ||
|
|
537c6c4e18 | ||
|
|
8e451911c9 | ||
|
|
442c122c44 | ||
|
|
536fedfe0f | ||
|
|
35866bb2c2 | ||
|
|
cea08c7abc | ||
|
|
eb034a0589 | ||
|
|
f4208771a8 | ||
|
|
c6eb12d1d4 | ||
|
|
182d8178fe | ||
|
|
3c72e0c18b | ||
|
|
a318244a1c | ||
|
|
a2205739b1 | ||
|
|
b939860d6e | ||
|
|
14e7ed76df | ||
|
|
53201033f2 | ||
|
|
d29e505568 | ||
|
|
e63a0ff125 | ||
|
|
90e6faffa0 | ||
|
|
ba70b30d7e | ||
|
|
21111cc954 | ||
|
|
f6d481f120 | ||
|
|
afc760263e | ||
|
|
e882db900d | ||
|
|
c673c84905 | ||
|
|
5fe01fd096 | ||
|
|
ba65bc1ec6 | ||
|
|
7955dc479c | ||
|
|
9d5905707a | ||
|
|
a1264926a0 | ||
|
|
d5b8196749 | ||
|
|
856e129cc6 | ||
|
|
c649007ed8 | ||
|
|
0914c91284 | ||
|
|
421cb0ad18 | ||
|
|
2e03963e9e | ||
|
|
8dbfd723b6 | ||
|
|
ddbd50de4a | ||
|
|
4d5b1e8a13 | ||
|
|
876a5d1f43 | ||
|
|
e06e9f885c | ||
|
|
09f40191f4 | ||
|
|
c164cb6ecd | ||
|
|
75e078748a | ||
|
|
5edda531f6 | ||
|
|
428d114766 | ||
|
|
9c8db26457 | ||
|
|
3b3f76b809 | ||
|
|
4f4e6c965f | ||
|
|
5aa36285b1 | ||
|
|
a0e2f52247 | ||
|
|
ce09e7e348 | ||
|
|
e4c2475318 | ||
|
|
3b18ecb24a | ||
|
|
be7473b401 | ||
|
|
2f6dddd6fd | ||
|
|
360bfbdd50 | ||
|
|
bf25ef6455 | ||
|
|
08e2dffa86 | ||
|
|
ef34eb6ec2 | ||
|
|
2f752abe97 | ||
|
|
8198df84f0 | ||
|
|
55f6640cae | ||
|
|
a062f94ab0 | ||
|
|
302b8f4406 | ||
|
|
73fda2a8b1 | ||
|
|
0195f543d9 | ||
|
|
ea99bfb902 | ||
|
|
8d4535e76c | ||
|
|
3e53ac2451 | ||
|
|
d0587445cb | ||
|
|
4099a1269b | ||
|
|
91e6bc8b97 | ||
|
|
8a8be2267d | ||
|
|
1a75ed01cd | ||
|
|
0552f5b809 | ||
|
|
c538dd5033 | ||
|
|
9c995a6b58 | ||
|
|
5be9a8de3f | ||
|
|
5e6f57ada1 | ||
|
|
680d88de68 | ||
|
|
b1bbc90919 | ||
|
|
f8c058e99c | ||
|
|
6238f2daa1 | ||
|
|
f430516d3f | ||
|
|
ca31c2b322 | ||
|
|
512ab9336b | ||
|
|
c556926597 | ||
|
|
879838a539 | ||
|
|
cb743c0249 | ||
|
|
7d5da66db9 | ||
|
|
1275923a68 | ||
|
|
8666ea82f7 | ||
|
|
1f19b986a0 | ||
|
|
da9457da61 | ||
|
|
7d8f3c261a | ||
|
|
f4d47d7140 | ||
|
|
1aeac1b25a | ||
|
|
dc440dd19d | ||
|
|
08d4e5040f | ||
|
|
f0bcc0b1c0 | ||
|
|
6aace693db | ||
|
|
e2cbee77b8 | ||
|
|
11f76fea94 | ||
|
|
e1d4690566 | ||
|
|
07095e85b2 | ||
|
|
a2828412b6 | ||
|
|
03776dd988 | ||
|
|
546b62e74f | ||
|
|
ed19147441 | ||
|
|
e7b6896c43 | ||
|
|
5222f59cc0 | ||
|
|
eaa89e23d7 | ||
|
|
b692ab58b2 | ||
|
|
b4dc0301f6 | ||
|
|
f51fc08670 | ||
|
|
1b80148420 | ||
|
|
bf790d2819 | ||
|
|
80c99760fb | ||
|
|
1ed23b9127 | ||
|
|
acf94abffd | ||
|
|
5b054ae7b8 | ||
|
|
34b5030239 | ||
|
|
39f812f040 | ||
|
|
533c8214d4 | ||
|
|
8ed2243a3c | ||
|
|
9a33891e97 | ||
|
|
ed8ff58019 | ||
|
|
438eba1402 | ||
|
|
f16aff8033 | ||
|
|
b88d261282 | ||
|
|
d9f5089a18 | ||
|
|
18316cb25f | ||
|
|
9aba05b76a | ||
|
|
bf8b29fbe7 | ||
|
|
7042d14341 | ||
|
|
60cd838f1c | ||
|
|
23eb9b4f4c | ||
|
|
19343ce274 | ||
|
|
0a3806602a | ||
|
|
6be9a89eb6 | ||
|
|
8d372eb30d | ||
|
|
27c7216e01 | ||
|
|
7aa9eaea48 | ||
|
|
e3f11bab42 | ||
|
|
f561a7b0e8 | ||
|
|
3e4eded5b2 | ||
|
|
4fa1647c3a | ||
|
|
242ad5a120 | ||
|
|
0e32544855 | ||
|
|
e2670001bc | ||
|
|
10e0c15534 | ||
|
|
b65c19012a | ||
|
|
49fac7666c | ||
|
|
ba176c3cb3 | ||
|
|
3159170383 | ||
|
|
3c1bc74b04 | ||
|
|
cfa34cff2d | ||
|
|
c123fc612f | ||
|
|
f578bc3a4a | ||
|
|
952fd7a756 | ||
|
|
5bd9efebda | ||
|
|
df991a0f46 | ||
|
|
f21fa23186 | ||
|
|
e654f56210 | ||
|
|
13a5b2511b | ||
|
|
e79bcfe5ab | ||
|
|
1dd0e3046b | ||
|
|
bd205aea28 | ||
|
|
ed2caaf265 | ||
|
|
5bc2b533ff | ||
|
|
e22665e528 | ||
|
|
22240589fc | ||
|
|
17cc8b0f23 | ||
|
|
f1eab158c5 | ||
|
|
232e5c3a3b | ||
|
|
41895fdf42 | ||
|
|
fb17316d77 | ||
|
|
d758dbb33a | ||
|
|
e1ba41f0ce | ||
|
|
34648f4019 | ||
|
|
5415b2a43b | ||
|
|
c3b3c0fafe | ||
|
|
d2e7554f74 | ||
|
|
3303708e94 | ||
|
|
a59dc93764 | ||
|
|
bb93cf12a7 | ||
|
|
cb389f9a09 | ||
|
|
191c2011f2 | ||
|
|
83920084f7 | ||
|
|
891da2f474 | ||
|
|
089e54a09b | ||
|
|
660938fe4d | ||
|
|
b0728fed27 | ||
|
|
4b31c28aea | ||
|
|
39561f8a08 | ||
|
|
734bb02904 | ||
|
|
1eb30e4288 | ||
|
|
b9fbdadf75 | ||
|
|
6cb7f4638d | ||
|
|
76d9a103e1 | ||
|
|
0c13c4483b | ||
|
|
44afc0aed6 | ||
|
|
9c75b9eaa1 | ||
|
|
e283930566 | ||
|
|
72ad3781a4 | ||
|
|
45c50a9f1a | ||
|
|
44b57bdf8b | ||
|
|
aab84f67a4 | ||
|
|
6bd3880cf1 | ||
|
|
6a5f27bed2 | ||
|
|
eb09e58697 | ||
|
|
5cf3b6dc7d | ||
|
|
2e50361a29 | ||
|
|
b322a1f58b | ||
|
|
e1c86f0b5d | ||
|
|
fb4ccac951 | ||
|
|
d286efd3d2 | ||
|
|
494f53421e | ||
|
|
83e1658109 | ||
|
|
e44a2be8b9 | ||
|
|
cada26d226 | ||
|
|
2ecce39ca9 | ||
|
|
0aacd0cf6d | ||
|
|
098f8ac343 | ||
|
|
758283f7d9 | ||
|
|
03ca48f3e7 | ||
|
|
536e61386d | ||
|
|
368505ebe9 | ||
|
|
5434b0282c | ||
|
|
3555341ab4 | ||
|
|
a07350a0d4 | ||
|
|
6a4a9b773c | ||
|
|
02b185751f | ||
|
|
e4f8b069b5 | ||
|
|
590490f35f | ||
|
|
3e55391ea8 | ||
|
|
527c4b312a | ||
|
|
f4c5b37561 | ||
|
|
58b762f965 | ||
|
|
ba89c6399c | ||
|
|
798c0fae71 | ||
|
|
15f1f8510f | ||
|
|
c880e31d9f | ||
|
|
a2cf54fca2 | ||
|
|
d13946bfb7 | ||
|
|
1a6d365db5 | ||
|
|
823ce5fc3d | ||
|
|
c6f40e339f | ||
|
|
ee793b35cf | ||
|
|
c6d83b1a3b | ||
|
|
f516fb9cba | ||
|
|
7b2568a3a9 | ||
|
|
b808503bc1 | ||
|
|
9556d3714d | ||
|
|
c5c2cd63bc | ||
|
|
98b6e59272 | ||
|
|
f3e123b304 | ||
|
|
cc6a772b89 | ||
|
|
a297e272d1 | ||
|
|
90a3f0a620 | ||
|
|
e648d289bb | ||
|
|
8214a5ef5e | ||
|
|
1a2d7be3f5 | ||
|
|
dd0120c189 | ||
|
|
f9eb0b8ba5 | ||
|
|
f912eace9e | ||
|
|
62394e0ebd | ||
|
|
61d5ebc0af | ||
|
|
79226fe703 | ||
|
|
7bde2b3c25 | ||
|
|
9cf43ccdad | ||
|
|
5c639eb4c4 | ||
|
|
6ec1389de2 | ||
|
|
78209468a0 | ||
|
|
9830c52816 | ||
|
|
73d98d5d17 | ||
|
|
dcd039df81 | ||
|
|
42764e54ae | ||
|
|
58a2f4f425 | ||
|
|
53b4c31afb | ||
|
|
f3a86e83fe | ||
|
|
d35411817e | ||
|
|
d3b1783647 | ||
|
|
0bece94257 | ||
|
|
400812a905 | ||
|
|
c7eba007cc | ||
|
|
89a8aaf502 | ||
|
|
34712da2e7 | ||
|
|
f68d329d24 | ||
|
|
99e6112fda | ||
|
|
a1a84df7a4 | ||
|
|
c8e2b474e8 | ||
|
|
8f06746051 | ||
|
|
25ce9e7153 | ||
|
|
f79398699b | ||
|
|
2c6372d790 | ||
|
|
5019b6b0f4 | ||
|
|
ba571064e7 | ||
|
|
ed2bdbaeaf | ||
|
|
c16217bf9b | ||
|
|
089974eb92 | ||
|
|
da9aeab810 | ||
|
|
859a4975f6 | ||
|
|
4e33311597 | ||
|
|
063d0077b3 | ||
|
|
48be5c22a2 | ||
|
|
e00409c517 | ||
|
|
2b64133287 | ||
|
|
cfa7702409 | ||
|
|
3c3c846908 | ||
|
|
3fe942c284 | ||
|
|
c1f947f4fb | ||
|
|
6798c1704e | ||
|
|
bfc8615d28 | ||
|
|
98e4447b46 | ||
|
|
710725ea9d | ||
|
|
60c92a7431 | ||
|
|
7ad88eff13 | ||
|
|
11b3b5128e | ||
|
|
e3063a5902 | ||
|
|
9514da06e7 | ||
|
|
7e6dd376b1 | ||
|
|
46e26191f4 | ||
|
|
d9db89814f | ||
|
|
d2bf073334 | ||
|
|
83a7b79c2d | ||
|
|
5dee290482 | ||
|
|
8d28a2382b | ||
|
|
8641461c98 | ||
|
|
0b307248a6 | ||
|
|
f6baab5c27 | ||
|
|
f153f6f4b5 | ||
|
|
515fd5218a | ||
|
|
3bb9941e37 | ||
|
|
a9922a6df2 | ||
|
|
2266e93fce | ||
|
|
5746660183 | ||
|
|
38f285c477 | ||
|
|
9b2e5b0c75 | ||
|
|
26277c8ab3 | ||
|
|
5adc272ee7 | ||
|
|
08b0741fa7 | ||
|
|
259a307daf | ||
|
|
daaa314aac | ||
|
|
b61bb5d635 | ||
|
|
9882a7cf33 | ||
|
|
9df02e2b97 | ||
|
|
f28645f900 | ||
|
|
4c84eb18b6 | ||
|
|
71a5b7a126 | ||
|
|
bc2cab2efe | ||
|
|
b22493ce7e | ||
|
|
81217a2cc0 | ||
|
|
47cbcf8b3a | ||
|
|
f04cb531db | ||
|
|
cfeba45c35 | ||
|
|
33baa8f382 | ||
|
|
cb316b13b1 | ||
|
|
f2e0391800 | ||
|
|
9bde82689f | ||
|
|
269c3bab7e | ||
|
|
8c423d9747 | ||
|
|
cf2278d4c5 | ||
|
|
2ef85bf845 | ||
|
|
96bcc1688b | ||
|
|
83b23aeb44 | ||
|
|
1e31452544 | ||
|
|
6b07e0d28f | ||
|
|
ac62d74d20 | ||
|
|
0054010255 | ||
|
|
ca8b6f0b13 | ||
|
|
e905a2870a | ||
|
|
01ab792f60 | ||
|
|
e78904b9b9 | ||
|
|
1104e73242 | ||
|
|
656a8378d1 | ||
|
|
5b8126f84d | ||
|
|
028fb01f42 | ||
|
|
a27560ef8d | ||
|
|
656ca25177 | ||
|
|
03f68857e2 | ||
|
|
cdfbd128ec | ||
|
|
64005c04ac | ||
|
|
395f7a9615 | ||
|
|
aec95bcbba | ||
|
|
23dd27873d | ||
|
|
da2a35de79 | ||
|
|
f4c31ccd59 | ||
|
|
5e3e7fe7d3 | ||
|
|
31f7a7479d | ||
|
|
107a184a81 | ||
|
|
a07b51f7df | ||
|
|
7f622c118d | ||
|
|
c22c26ed3d | ||
|
|
5ef5195c8f | ||
|
|
265b40ec6e | ||
|
|
2e8c75269a |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,16 +1,15 @@
|
||||
/bin
|
||||
/_CPack_Packages
|
||||
/doc/conf.py
|
||||
/doc/doxyxml
|
||||
/doc/html
|
||||
/Testing
|
||||
/*.cmake
|
||||
/format-test
|
||||
/install_manifest.txt
|
||||
/tinyformat_speed_test
|
||||
*~
|
||||
*.a
|
||||
*.zip
|
||||
cmake_install.cmake
|
||||
CPack*Config.cmake
|
||||
CTestTestfile.cmake
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
Makefile
|
||||
|
||||
11
.gitmodules
vendored
11
.gitmodules
vendored
@@ -1,9 +1,6 @@
|
||||
[submodule "gtest"]
|
||||
path = gtest
|
||||
url = git://github.com/vitaut/gtest.git
|
||||
[submodule "format-benchmark"]
|
||||
path = format-benchmark
|
||||
url = git://github.com/vitaut/format-benchmark.git
|
||||
[submodule "breathe"]
|
||||
path = breathe
|
||||
url = git://github.com/vitaut/breathe.git
|
||||
url = https://github.com/michaeljones/breathe.git
|
||||
[submodule "doc/sphinx-bootstrap-theme"]
|
||||
path = doc/sphinx-bootstrap-theme
|
||||
url = https://github.com/cppformat/sphinx-bootstrap-theme.git
|
||||
|
||||
17
.travis.yml
17
.travis.yml
@@ -1,9 +1,20 @@
|
||||
language: cpp
|
||||
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
before_install:
|
||||
- git submodule update --init
|
||||
|
||||
env:
|
||||
- BUILD_TYPE=Debug
|
||||
- BUILD_TYPE=Release
|
||||
|
||||
script:
|
||||
- cmake .
|
||||
- make
|
||||
- make test
|
||||
- cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DFMT_EXTRA_TESTS=ON .
|
||||
- make -j4
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 make test
|
||||
|
||||
after_failure:
|
||||
- cat Testing/Temporary/LastTest.log
|
||||
|
||||
15
Android.mk
Normal file
15
Android.mk
Normal file
@@ -0,0 +1,15 @@
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_MODULE := cppformat_static
|
||||
LOCAL_MODULE_FILENAME := libcppformat
|
||||
|
||||
LOCAL_SRC_FILES := format.cc
|
||||
|
||||
LOCAL_C_INCLUDES := $(LOCAL_PATH)
|
||||
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
|
||||
|
||||
LOCAL_CFLAGS += -std=c++11 -fexceptions
|
||||
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
|
||||
234
CMakeLists.txt
234
CMakeLists.txt
@@ -1,3 +1,5 @@
|
||||
message(STATUS "CMake version: ${CMAKE_VERSION}")
|
||||
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
# Set the default CMAKE_BUILD_TYPE to Release.
|
||||
@@ -8,153 +10,165 @@ if (NOT CMAKE_BUILD_TYPE)
|
||||
"Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
|
||||
endif ()
|
||||
|
||||
option(FMT_EXTRA_TESTS "Enable extra tests." OFF)
|
||||
|
||||
project(FORMAT)
|
||||
|
||||
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
|
||||
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
include(CheckCXXCompilerFlag)
|
||||
check_cxx_compiler_flag(-std=c++11 HAVE_STD_CPP11_FLAG)
|
||||
if (HAVE_STD_CPP11_FLAG)
|
||||
set(CPP11_FLAG -std=c++11)
|
||||
else ()
|
||||
check_cxx_compiler_flag(-std=c++0x HAVE_STD_CPP0X_FLAG)
|
||||
if (HAVE_STD_CPP0X_FLAG)
|
||||
set(CPP11_FLAG -std=c++0x)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
add_library(format format.cc)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/support/cmake")
|
||||
|
||||
if (CMAKE_GENERATOR MATCHES "Visual Studio")
|
||||
# If Microsoft SDK is installed create script run-msbuild.bat that
|
||||
# calls SetEnv.cmd to to set up build environment and runs msbuild.
|
||||
# It is useful when building Visual Studio projects with the SDK
|
||||
# toolchain rather than Visual Studio.
|
||||
include(FindSetEnv)
|
||||
if (WINSDK_SETENV)
|
||||
set(MSBUILD_SETUP "call \"${WINSDK_SETENV}\"")
|
||||
endif ()
|
||||
# Set FrameworkPathOverride to get rid of MSB3644 warnings.
|
||||
set(netfxpath "C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.0")
|
||||
file(WRITE run-msbuild.bat "
|
||||
${MSBUILD_SETUP}
|
||||
${CMAKE_MAKE_PROGRAM} -p:FrameworkPathOverride=\"${netfxpath}\" %*")
|
||||
endif ()
|
||||
|
||||
set(FMT_SOURCES format.cc format.h)
|
||||
|
||||
include(CheckSymbolExists)
|
||||
if (WIN32)
|
||||
check_symbol_exists(open io.h HAVE_OPEN)
|
||||
else ()
|
||||
check_symbol_exists(open fcntl.h HAVE_OPEN)
|
||||
endif ()
|
||||
if (HAVE_OPEN)
|
||||
add_definitions(-DFMT_USE_FILE_DESCRIPTORS=1)
|
||||
set(FMT_SOURCES ${FMT_SOURCES} posix.cc posix.h)
|
||||
endif ()
|
||||
|
||||
if (CPP11_FLAG)
|
||||
set(CMAKE_REQUIRED_FLAGS ${CPP11_FLAG})
|
||||
endif ()
|
||||
|
||||
if (BIICODE)
|
||||
include(support/cmake/biicode.cmake)
|
||||
return()
|
||||
endif ()
|
||||
|
||||
add_library(format ${FMT_SOURCES})
|
||||
if (CMAKE_COMPILER_IS_GNUCXX)
|
||||
set_target_properties(format PROPERTIES COMPILE_FLAGS
|
||||
"-Wall -Wextra -pedantic")
|
||||
"-Wall -Wextra -Wshadow -pedantic")
|
||||
endif ()
|
||||
if (HAVE_STD_CPP11_FLAG)
|
||||
set_target_properties(format PROPERTIES COMPILE_FLAGS "-std=c++11")
|
||||
if (CPP11_FLAG AND FMT_EXTRA_TESTS)
|
||||
set_target_properties(format PROPERTIES COMPILE_FLAGS ${CPP11_FLAG})
|
||||
# Test compilation with default flags.
|
||||
add_library(testformat format.cc)
|
||||
file(GLOB src RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} test/*.cc test/*.h)
|
||||
add_library(testformat STATIC ${FMT_SOURCE_FILES} ${src})
|
||||
endif ()
|
||||
|
||||
add_subdirectory(doc)
|
||||
|
||||
enable_testing()
|
||||
|
||||
add_test(compile-test ${CMAKE_CTEST_COMMAND}
|
||||
--build-and-test
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/test"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/test"
|
||||
--build-generator ${CMAKE_GENERATOR}
|
||||
--build-makeprogram ${CMAKE_MAKE_PROGRAM})
|
||||
include_directories(. gmock)
|
||||
|
||||
# We compile Google Test ourselves instead of using pre-compiled libraries.
|
||||
# See the Google Test FAQ "Why is it not recommended to install a
|
||||
# pre-compiled copy of Google Test (for example, into /usr/local)?"
|
||||
# at http://code.google.com/p/googletest/wiki/FAQ for more details.
|
||||
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/gtest/CMakeLists.txt)
|
||||
option(
|
||||
gtest_force_shared_crt
|
||||
"Use shared (DLL) run-time lib even when Google Test is built as static lib."
|
||||
ON)
|
||||
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
|
||||
add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1)
|
||||
endif ()
|
||||
|
||||
add_library(gmock STATIC gmock/gmock-gtest-all.cc)
|
||||
find_package(Threads)
|
||||
target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
||||
# Check if variadic templates are working and not affected by GCC bug 39653:
|
||||
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=39653
|
||||
check_cxx_source_compiles("
|
||||
template <class T, class ...Types>
|
||||
struct S { typedef typename S<Types...>::type type; };
|
||||
int main() {}" FMT_VARIADIC_TEMPLATES)
|
||||
|
||||
# Check if initializer lists are supported.
|
||||
check_cxx_source_compiles("
|
||||
#include <initilizer_list>
|
||||
int main() {}" FMT_INITIALIZER_LIST)
|
||||
|
||||
add_subdirectory(gtest)
|
||||
include_directories(gtest/include)
|
||||
link_directories(${CMAKE_CURRENT_BINARY_DIR}/gtest)
|
||||
|
||||
add_executable(format-test format-test.cc)
|
||||
target_link_libraries(format-test format gtest)
|
||||
if (CMAKE_COMPILER_IS_GNUCXX)
|
||||
set_target_properties(format-test PROPERTIES COMPILE_FLAGS
|
||||
"-Wall -Wextra -pedantic -Wno-long-long -Wno-variadic-macros")
|
||||
endif ()
|
||||
add_test(format-test format-test)
|
||||
if (HAVE_STD_CPP11_FLAG)
|
||||
set_target_properties(format-test PROPERTIES COMPILE_FLAGS "-std=c++11")
|
||||
endif ()
|
||||
if (NOT FMT_VARIADIC_TEMPLATES OR NOT FMT_INITIALIZER_LIST)
|
||||
add_definitions(-DGTEST_LANG_CXX11=0)
|
||||
endif ()
|
||||
|
||||
find_library(PROFILER_LIB profiler)
|
||||
find_path(PROFILER_INCLUDE_DIR gperftools/profiler.h)
|
||||
if (PROFILER_LIB AND PROFILER_INCLUDE_DIR)
|
||||
include_directories(${PROFILER_INCLUDE_DIR})
|
||||
set(HAVE_PROFILER TRUE)
|
||||
# This is disabled at the moment because format is compiled without -std=c++11
|
||||
# by default.
|
||||
#check_cxx_source_compiles("
|
||||
# void f() noexcept {}
|
||||
# int main(){ f(); }" FMT_BASIC_NOEXCEPT_SUPPORT)
|
||||
#if (FMT_BASIC_NOEXCEPT_SUPPORT)
|
||||
# add_definitions(-DFMT_USE_NOEXCEPT=1)
|
||||
#endif ()
|
||||
|
||||
#check_cxx_source_compiles("
|
||||
# struct C{
|
||||
# C()=delete;
|
||||
# C(const C&)=delete;
|
||||
# C& operator=(const C&)=delete;
|
||||
# };
|
||||
# int main(){}" FMT_DELETED_FUNCTIONS)
|
||||
#if (FMT_DELETED_FUNCTIONS)
|
||||
# add_definitions(-DFMT_USE_DELETED_FUNCTIONS=1)
|
||||
#endif ()
|
||||
|
||||
#check_cxx_source_compiles("
|
||||
# static_assert(true, \"\");
|
||||
# int main(){}" FMT_STATIC_ASSERT)
|
||||
#if (FMT_STATIC_ASSERT)
|
||||
# add_definitions(-DFMT_USE_STATIC_ASSERT=1)
|
||||
#endif ()
|
||||
|
||||
# GTest doesn't detect <tuple> with clang.
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
||||
target_compile_definitions(gmock PUBLIC GTEST_USE_OWN_TR1_TUPLE=1)
|
||||
endif ()
|
||||
|
||||
find_package(Boost)
|
||||
if (Boost_FOUND)
|
||||
add_definitions(-DHAVE_BOOST)
|
||||
include_directories(${Boost_INCLUDE_DIRS})
|
||||
endif ()
|
||||
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/format-benchmark/tinyformat_test.cpp)
|
||||
add_subdirectory(format-benchmark)
|
||||
|
||||
add_executable(tinyformat_speed_test format-benchmark/tinyformat_test.cpp)
|
||||
target_link_libraries(tinyformat_speed_test format)
|
||||
if (HAVE_PROFILER)
|
||||
target_link_libraries(tinyformat_speed_test ${PROFILER_LIB})
|
||||
set(PROFILE_DEFS ";FMT_PROFILE")
|
||||
endif ()
|
||||
set_target_properties(tinyformat_speed_test PROPERTIES COMPILE_DEFINITIONS
|
||||
"SPEED_TEST;HAVE_FORMAT;_SCL_SECURE_NO_WARNINGS;${PROFILE_DEFS}")
|
||||
if (HAVE_STD_CPP11_FLAG)
|
||||
set_target_properties(tinyformat_speed_test PROPERTIES COMPILE_FLAGS "-std=c++11")
|
||||
endif ()
|
||||
|
||||
add_custom_target(speed_test
|
||||
COMMAND @echo running speed tests...
|
||||
COMMAND @echo printf timings:
|
||||
COMMAND @time -p ./tinyformat_speed_test printf > /dev/null
|
||||
COMMAND @echo iostreams timings:
|
||||
COMMAND @time -p ./tinyformat_speed_test iostreams > /dev/null
|
||||
COMMAND @echo format timings:
|
||||
COMMAND @time -p ./tinyformat_speed_test format > /dev/null
|
||||
COMMAND @echo tinyformat timings:
|
||||
COMMAND @time -p ./tinyformat_speed_test tinyformat > /dev/null
|
||||
COMMAND @echo boost timings:
|
||||
COMMAND @time -p ./tinyformat_speed_test boost > /dev/null
|
||||
DEPENDS tinyformat_speed_test)
|
||||
|
||||
add_custom_target(bloat_test_debug
|
||||
COMMAND echo running bloat tests...
|
||||
COMMAND echo printf results:
|
||||
COMMAND ./bloat_test.sh
|
||||
COMMAND echo iostreams results:
|
||||
COMMAND ./bloat_test.sh -DUSE_IOSTREAMS
|
||||
COMMAND echo format results:
|
||||
COMMAND ./bloat_test.sh -DUSE_FORMAT -L.. -lformat
|
||||
COMMAND echo tinyformat results:
|
||||
COMMAND ./bloat_test.sh -DUSE_TINYFORMAT
|
||||
COMMAND echo boost results:
|
||||
COMMAND ./bloat_test.sh -DUSE_BOOST
|
||||
WORKING_DIRECTORY tinyformat
|
||||
DEPENDS format)
|
||||
|
||||
add_custom_target(bloat_test_optimized
|
||||
COMMAND echo running bloat tests...
|
||||
COMMAND echo printf results:
|
||||
COMMAND ./bloat_test.sh -O3
|
||||
COMMAND echo iostreams results:
|
||||
COMMAND ./bloat_test.sh -O3 -DUSE_IOSTREAMS
|
||||
COMMAND echo format results:
|
||||
COMMAND ./bloat_test.sh -O3 -DUSE_FORMAT -L.. -lformat
|
||||
COMMAND echo tinyformat results:
|
||||
COMMAND ./bloat_test.sh -O3 -DUSE_TINYFORMAT
|
||||
COMMAND echo boost results:
|
||||
COMMAND ./bloat_test.sh -O3 -DUSE_BOOST
|
||||
WORKING_DIRECTORY tinyformat
|
||||
DEPENDS format)
|
||||
endif ()
|
||||
enable_testing()
|
||||
add_subdirectory(test)
|
||||
|
||||
if (EXISTS .gitignore)
|
||||
# Get the list of ignored files from .gitignore.
|
||||
file (STRINGS ".gitignore" lines)
|
||||
LIST(REMOVE_ITEM lines /doc/html)
|
||||
foreach (line ${lines})
|
||||
string(REPLACE "." "[.]" line "${line}")
|
||||
string(REPLACE "*" ".*" line "${line}")
|
||||
set(ignored_files ${ignored_files} "${line}$" "${line}/")
|
||||
endforeach ()
|
||||
set(ignored_files ${ignored_files} /.git /breathe /format-benchmark /gtest)
|
||||
set(ignored_files ${ignored_files} /.git /breathe /format-benchmark sphinx/)
|
||||
|
||||
set(CPACK_SOURCE_GENERATOR ZIP)
|
||||
set(CPACK_SOURCE_IGNORE_FILES ${ignored_files})
|
||||
set(CPACK_PACKAGE_VERSION_MAJOR 0)
|
||||
set(CPACK_PACKAGE_VERSION_MINOR 8)
|
||||
set(CPACK_PACKAGE_VERSION_MAJOR 1)
|
||||
set(CPACK_PACKAGE_VERSION_MINOR 1)
|
||||
set(CPACK_PACKAGE_VERSION_PATCH 0)
|
||||
set(CPACK_SOURCE_PACKAGE_FILE_NAME
|
||||
format-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}-src)
|
||||
set(CPPFORMAT_VERSION
|
||||
${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH})
|
||||
set(CPACK_SOURCE_PACKAGE_FILE_NAME cppformat-${CPPFORMAT_VERSION})
|
||||
set(CPACK_RESOURCE_FILE_README ${FORMAT_SOURCE_DIR}/README.rst)
|
||||
include(CPack)
|
||||
endif ()
|
||||
|
||||
configure_file(doc/conf.py.in doc/conf.py @ONLY)
|
||||
# Install our targets
|
||||
install(TARGETS format DESTINATION lib)
|
||||
install(FILES format.h DESTINATION include)
|
||||
|
||||
335
ChangeLog.rst
Normal file
335
ChangeLog.rst
Normal file
@@ -0,0 +1,335 @@
|
||||
1.1.0 - 2015-03-06
|
||||
------------------
|
||||
|
||||
* Added ``BasicArrayWriter``, a class template that provides operations for
|
||||
formatting and writing data into a fixed-size array
|
||||
(`#105 <https://github.com/cppformat/cppformat/issues/105>`_ and
|
||||
`#122 <https://github.com/cppformat/cppformat/issues/122>`_):
|
||||
|
||||
.. code:: c++
|
||||
|
||||
char buffer[100];
|
||||
fmt::ArrayWriter w(buffer);
|
||||
w.write("The answer is {}", 42);
|
||||
|
||||
* Added `0 A.D. <http://play0ad.com/>`_ and `PenUltima Online (POL)
|
||||
<http://www.polserver.com/>`_ to the list of notable projects using C++ Format.
|
||||
|
||||
* C++ Format now uses MSVC intrinsics for better formatting performance
|
||||
(`#115 <https://github.com/cppformat/cppformat/pull/115>`_,
|
||||
`#116 <https://github.com/cppformat/cppformat/pull/116>`_,
|
||||
`#118 <https://github.com/cppformat/cppformat/pull/118>`_ and
|
||||
`#121 <https://github.com/cppformat/cppformat/pull/121>`_).
|
||||
Previously these optimizations where only used on GCC and Clang.
|
||||
Thanks to `@CarterLi <https://github.com/CarterLi>`_ and
|
||||
`@objectx <https://github.com/objectx>`_.
|
||||
|
||||
* CMake install target (`#119 <https://github.com/cppformat/cppformat/pull/119>`_).
|
||||
Thanks to `@TrentHouliston <https://github.com/TrentHouliston>`_.
|
||||
|
||||
You can now install C++ Format with ``make install`` command.
|
||||
|
||||
* Improved `Biicode <http://www.biicode.com/>`_ support
|
||||
(`#98 <https://github.com/cppformat/cppformat/pull/98>`_ and
|
||||
`#104 <https://github.com/cppformat/cppformat/pull/104>`_). Thanks to
|
||||
`@MariadeAnton <https://github.com/MariadeAnton>`_ and
|
||||
`@franramirez688 <https://github.com/franramirez688>`_.
|
||||
|
||||
* Improved support for bulding with `Android NDK
|
||||
<https://developer.android.com/tools/sdk/ndk/index.html>`_
|
||||
(`#107 <https://github.com/cppformat/cppformat/pull/107>`_).
|
||||
Thanks to `@newnon <https://github.com/newnon>`_.
|
||||
|
||||
The `android-ndk-example <https://github.com/cppformat/android-ndk-example>`_
|
||||
repository provides and example of using C++ Format with Android NDK:
|
||||
|
||||
.. image:: https://raw.githubusercontent.com/cppformat/android-ndk-example/
|
||||
master/screenshot.png
|
||||
|
||||
* Improved documentation of ``SystemError`` and ``WindowsError``
|
||||
(`#54 <https://github.com/cppformat/cppformat/issues/54>`_).
|
||||
|
||||
* Various code improvements
|
||||
(`#110 <https://github.com/cppformat/cppformat/pull/110>`_,
|
||||
`#111 <https://github.com/cppformat/cppformat/pull/111>`_
|
||||
`#112 <https://github.com/cppformat/cppformat/pull/112>`_).
|
||||
Thanks to `@CarterLi <https://github.com/CarterLi>`_.
|
||||
|
||||
* Improved compile-time errors when formatting wide into narrow strings
|
||||
(`#117 <https://github.com/cppformat/cppformat/issues/117>`_).
|
||||
|
||||
* Fixed ``BasicWriter::write`` without formatting arguments when C++11 support
|
||||
is disabled (`#109 <https://github.com/cppformat/cppformat/issues/109>`_).
|
||||
|
||||
* Fixed header-only build on OS X with GCC 4.9
|
||||
(`#124 <https://github.com/cppformat/cppformat/issues/124>`_).
|
||||
|
||||
* Fixed packaging issues (`#94 <https://github.com/cppformat/cppformat/issues/94>`_).
|
||||
|
||||
* Fixed warnings in GCC, MSVC and Xcode/Clang
|
||||
(`#95 <https://github.com/cppformat/cppformat/issues/95>`_,
|
||||
`#96 <https://github.com/cppformat/cppformat/issues/96>`_ and
|
||||
`#114 <https://github.com/cppformat/cppformat/pull/114>`_).
|
||||
|
||||
* Added `changelog <https://github.com/cppformat/cppformat/edit/master/ChangeLog.rst>`_
|
||||
(`#103 <https://github.com/cppformat/cppformat/issues/103>`_).
|
||||
|
||||
1.0.0 - 2015-02-05
|
||||
------------------
|
||||
|
||||
* Add support for a header-only configuration when ``FMT_HEADER_ONLY`` is
|
||||
defined before including ``format.h``:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
#define FMT_HEADER_ONLY
|
||||
#include "format.h"
|
||||
|
||||
* Compute string length in the constructor of ``BasicStringRef``
|
||||
instead of the ``size`` method
|
||||
(`#79 <https://github.com/cppformat/cppformat/issues/79>`_).
|
||||
This eliminates size computation for string literals on reasonable optimizing
|
||||
compilers.
|
||||
|
||||
* Fix formatting of types with overloaded ``operator <<`` for ``std::wostream``
|
||||
(`#86 <https://github.com/cppformat/cppformat/issues/86>`_):
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::format(L"The date is {0}", Date(2012, 12, 9));
|
||||
|
||||
* Fix linkage of tests on Arch Linux
|
||||
(`#89 <https://github.com/cppformat/cppformat/issues/89>`_).
|
||||
|
||||
* Allow precision specifier for non-float arguments
|
||||
(`#90 <https://github.com/cppformat/cppformat/issues/90>`_):
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::print("{:.3}\n", "Carpet"); // prints "Car"
|
||||
|
||||
* Fix build on Android NDK
|
||||
(`#93 <https://github.com/cppformat/cppformat/issues/93>`_)
|
||||
|
||||
* Improvements to documentation build procedure.
|
||||
|
||||
* Remove ``FMT_SHARED`` CMake variable in favor of standard `BUILD_SHARED_LIBS
|
||||
<http://www.cmake.org/cmake/help/v3.0/variable/BUILD_SHARED_LIBS.html>`_.
|
||||
|
||||
* Fix error handling in ``fmt::fprintf``.
|
||||
|
||||
* Fix a number of warnings.
|
||||
|
||||
0.12.0 - 2014-10-25
|
||||
-------------------
|
||||
|
||||
* [Breaking] Improved separation between formatting and buffer management.
|
||||
``Writer`` is now a base class that cannot be instantiated directly.
|
||||
The new ``MemoryWriter`` class implements the default buffer management
|
||||
with small allocations done on stack. So ``fmt::Writer`` should be replaced
|
||||
with ``fmt::MemoryWriter`` in variable declarations.
|
||||
|
||||
Old code:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::Writer w;
|
||||
|
||||
New code:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::MemoryWriter w;
|
||||
|
||||
If you pass ``fmt::Writer`` by reference, you can continue to do so:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
void f(fmt::Writer &w);
|
||||
|
||||
This doesn't affect the formatting API.
|
||||
|
||||
* Support for custom memory allocators
|
||||
(`#69 <https://github.com/cppformat/cppformat/issues/69>`_)
|
||||
|
||||
* Formatting functions now accept `signed char` and `unsigned char` strings as
|
||||
arguments (`#73 <https://github.com/cppformat/cppformat/issues/73>`_):
|
||||
|
||||
.. code:: c++
|
||||
|
||||
auto s = format("GLSL version: {}", glGetString(GL_VERSION));
|
||||
|
||||
* Reduced code bloat. According to the new `benchmark results
|
||||
<https://github.com/cppformat/cppformat#compile-time-and-code-bloat>`_,
|
||||
cppformat is close to ``printf`` and by the order of magnitude better than
|
||||
Boost Format in terms of compiled code size.
|
||||
|
||||
* Improved appearance of the documentation on mobile by using the `Sphinx
|
||||
Bootstrap theme <http://ryan-roemer.github.io/sphinx-bootstrap-theme/>`_:
|
||||
|
||||
.. |old| image:: https://cloud.githubusercontent.com/assets/576385/4792130/
|
||||
cd256436-5de3-11e4-9a62-c077d0c2b003.png
|
||||
|
||||
.. |new| image:: https://cloud.githubusercontent.com/assets/576385/4792131/
|
||||
cd29896c-5de3-11e4-8f59-cac952942bf0.png
|
||||
|
||||
+-------+-------+
|
||||
| Old | New |
|
||||
+-------+-------+
|
||||
| |old| | |new| |
|
||||
+-------+-------+
|
||||
|
||||
0.11.0 - 2014-08-21
|
||||
-------------------
|
||||
|
||||
* Safe printf implementation with a POSIX extension for positional arguments:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::printf("Elapsed time: %.2f seconds", 1.23);
|
||||
fmt::printf("%1$s, %3$d %2$s", weekday, month, day);
|
||||
|
||||
* Arguments of ``char`` type can now be formatted as integers
|
||||
(Issue `#55 <https://github.com/cppformat/cppformat/issues/55>`_):
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::format("0x{0:02X}", 'a');
|
||||
|
||||
* Deprecated parts of the API removed.
|
||||
|
||||
* The library is now built and tested on MinGW with Appveyor in addition to
|
||||
existing test platforms Linux/GCC, OS X/Clang, Windows/MSVC.
|
||||
|
||||
0.10.0 - 2014-07-01
|
||||
-------------------
|
||||
|
||||
**Improved API**
|
||||
|
||||
* All formatting methods are now implemented as variadic functions instead
|
||||
of using ``operator<<`` for feeding arbitrary arguments into a temporary
|
||||
formatter object. This works both with C++11 where variadic templates are
|
||||
used and with older standards where variadic functions are emulated by
|
||||
providing lightweight wrapper functions defined with the ``FMT_VARIADIC``
|
||||
macro. You can use this macro for defining your own portable variadic
|
||||
functions:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
void report_error(const char *format, const fmt::ArgList &args) {
|
||||
fmt::print("Error: {}");
|
||||
fmt::print(format, args);
|
||||
}
|
||||
FMT_VARIADIC(void, report_error, const char *)
|
||||
|
||||
report_error("file not found: {}", path);
|
||||
|
||||
Apart from a more natural syntax, this also improves performance as there
|
||||
is no need to construct temporary formatter objects and control arguments'
|
||||
lifetimes. Because the wrapper functions are very ligthweight, this doesn't
|
||||
cause code bloat even in pre-C++11 mode.
|
||||
|
||||
* Simplified common case of formatting an ``std::string``. Now it requires a
|
||||
single function call:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
std::string s = format("The answer is {}.", 42);
|
||||
|
||||
Previously it required 2 function calls:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
std::string s = str(Format("The answer is {}.") << 42);
|
||||
|
||||
Instead of unsafe ``c_str`` function, ``fmt::Writer`` should be used directly
|
||||
to bypass creation of ``std::string``:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::Writer w;
|
||||
w.write("The answer is {}.", 42);
|
||||
w.c_str(); // returns a C string
|
||||
|
||||
This doesn't do dynamic memory allocation for small strings and is less error
|
||||
prone as the lifetime of the string is the same as for ``std::string::c_str``
|
||||
which is well understood (hopefully).
|
||||
|
||||
* Improved consistency in naming functions that are a part of the public API.
|
||||
Now all public functions are lowercase following the standard library
|
||||
conventions. Previously it was a combination of lowercase and
|
||||
CapitalizedWords.
|
||||
Issue `#50 <https://github.com/cppformat/cppformat/issues/50>`_.
|
||||
|
||||
* Old functions are marked as deprecated and will be removed in the next
|
||||
release.
|
||||
|
||||
**Other Changes**
|
||||
|
||||
* Experimental support for printf format specifications (work in progress):
|
||||
|
||||
.. code:: c++
|
||||
|
||||
fmt::printf("The answer is %d.", 42);
|
||||
std::string s = fmt::sprintf("Look, a %s!", "string");
|
||||
|
||||
* Support for hexadecimal floating point format specifiers ``a`` and ``A``:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
print("{:a}", -42.0); // Prints -0x1.5p+5
|
||||
print("{:A}", -42.0); // Prints -0X1.5P+5
|
||||
|
||||
* CMake option ``FMT_SHARED`` that specifies whether to build format as a
|
||||
shared library (off by default).
|
||||
|
||||
0.9.0 - 2014-05-13
|
||||
------------------
|
||||
|
||||
* More efficient implementation of variadic formatting functions.
|
||||
|
||||
* ``Writer::Format`` now has a variadic overload:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
Writer out;
|
||||
out.Format("Look, I'm {}!", "variadic");
|
||||
|
||||
* For efficiency and consistency with other overloads, variadic overload of
|
||||
the ``Format`` function now returns ``Writer`` instead of ``std::string``.
|
||||
Use the ``str`` function to convert it to ``std::string``:
|
||||
|
||||
.. code:: c++
|
||||
|
||||
std::string s = str(Format("Look, I'm {}!", "variadic"));
|
||||
|
||||
* Replaced formatter actions with output sinks: ``NoAction`` -> ``NullSink``,
|
||||
``Write`` -> ``FileSink``, ``ColorWriter`` -> ``ANSITerminalSink``.
|
||||
This improves naming consistency and shouldn't affect client code unless
|
||||
these classes are used directly which should be rarely needed.
|
||||
|
||||
* Added ``ThrowSystemError`` function that formats a message and throws
|
||||
``SystemError`` containing the formatted message and system-specific error
|
||||
description. For example, the following code
|
||||
|
||||
.. code:: c++
|
||||
|
||||
FILE *f = fopen(filename, "r");
|
||||
if (!f)
|
||||
ThrowSystemError(errno, "Failed to open file '{}'") << filename;
|
||||
|
||||
will throw ``SystemError`` exception with description
|
||||
"Failed to open file '<filename>': No such file or directory" if file
|
||||
doesn't exist.
|
||||
|
||||
* Support for AppVeyor continuous integration platform.
|
||||
|
||||
* ``Format`` now throws ``SystemError`` in case of I/O errors.
|
||||
|
||||
* Improve test infrastructure. Print functions are now tested by redirecting
|
||||
the output to a pipe.
|
||||
|
||||
0.8.0 - 2014-04-14
|
||||
------------------
|
||||
|
||||
* Initial release
|
||||
320
README.rst
320
README.rst
@@ -1,11 +1,21 @@
|
||||
format
|
||||
======
|
||||
C++ Format
|
||||
==========
|
||||
|
||||
.. image:: https://travis-ci.org/cppformat/format.png?branch=master
|
||||
:target: https://travis-ci.org/cppformat/format
|
||||
.. image:: https://travis-ci.org/cppformat/cppformat.png?branch=master
|
||||
:target: https://travis-ci.org/cppformat/cppformat
|
||||
|
||||
Format is an open-source formatting library for C++.
|
||||
It can be used as a type-safe alternative to printf or as a fast
|
||||
.. image:: https://ci.appveyor.com/api/projects/status/qk0bhyhqp1ekpat8
|
||||
:target: https://ci.appveyor.com/project/vitaut/cppformat
|
||||
|
||||
.. image:: https://readthedocs.org/projects/cppformat/badge/?version=stable
|
||||
:target: http://cppformat.readthedocs.org/en/stable/
|
||||
:alt: Documentation Status
|
||||
|
||||
.. image:: https://webapi.biicode.com/v1/badges/vitaut/vitaut/cppformat/master
|
||||
:target: https://www.biicode.com/vitaut/cppformat
|
||||
|
||||
C++ Format is an open-source formatting library for C++.
|
||||
It can be used as a safe alternative to printf or as a fast
|
||||
alternative to IOStreams.
|
||||
|
||||
Features
|
||||
@@ -14,15 +24,18 @@ Features
|
||||
* Two APIs: faster concatenation-based write API and slower (but still
|
||||
very fast) replacement-based format API with positional arguments for
|
||||
localization.
|
||||
* Write API similar to the one used by IOStreams but much faster and more
|
||||
consistent.
|
||||
* Write API similar to the one used by IOStreams but stateless allowing
|
||||
faster implementation.
|
||||
* Format API with `format string syntax
|
||||
<http://cppformat.github.io/format#format-string-syntax>`__
|
||||
<http://cppformat.readthedocs.org/en/latest/syntax.html>`_
|
||||
similar to the one used by `str.format
|
||||
<http://docs.python.org/2/library/stdtypes.html#str.format>`__ in Python.
|
||||
<http://docs.python.org/2/library/stdtypes.html#str.format>`_ in Python.
|
||||
* Safe `printf implementation
|
||||
<http://cppformat.readthedocs.org/en/stable/reference.html#printf-formatting-functions>`_
|
||||
including the POSIX extension for positional arguments.
|
||||
* Support for user-defined types.
|
||||
* High speed: performance of the format API is close to that of
|
||||
glibc's `printf <http://en.cppreference.com/w/cpp/io/c/fprintf>`__
|
||||
glibc's `printf <http://en.cppreference.com/w/cpp/io/c/fprintf>`_
|
||||
and better than performance of IOStreams. See `Speed tests`_ and
|
||||
`Fast integer to string conversion in C++
|
||||
<http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html>`_.
|
||||
@@ -30,41 +43,43 @@ Features
|
||||
header file and a single source file) and compiled code.
|
||||
See `Compile time and code bloat`_.
|
||||
* Reliability: the library has an extensive set of `unit tests
|
||||
<https://github.com/cppformat/format/blob/master/format-test.cc>`__.
|
||||
<https://github.com/cppformat/cppformat/tree/master/test>`_.
|
||||
* Safety: the library is fully type safe, errors in format strings are
|
||||
reported using exceptions, automatic memory management prevents buffer
|
||||
overflow errors.
|
||||
* Ease of use: small self-contained code base, no external dependencies,
|
||||
permissive BSD `license`_.
|
||||
* `Portability`_ with consistent output across platforms and support
|
||||
for older compilers.
|
||||
* `Portability <http://cppformat.github.io#portability>`_ with consistent output
|
||||
across platforms and support for older compilers.
|
||||
* Clean warning-free codebase even on high warning levels
|
||||
(-Wall -Wextra -pedantic).
|
||||
* Support for wide strings.
|
||||
* Optional header-only configuration enabled with the ``FMT_HEADER_ONLY`` macro.
|
||||
|
||||
See the `documentation <http://cppformat.github.io/format/>`__ for more details.
|
||||
See the `documentation <http://cppformat.readthedocs.org/en/stable/>`_ for more details.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
This prints ``Hello, world!`` to stdout:
|
||||
|
||||
.. code-block:: c++
|
||||
.. code:: c++
|
||||
|
||||
fmt::Print("Hello, {}!") << "world";
|
||||
fmt::print("Hello, {}!", "world"); // uses Python-like format string syntax
|
||||
fmt::printf("Hello, %s!", "world"); // uses printf format string syntax
|
||||
|
||||
Arguments can be accessed by position and arguments' indices can be repeated:
|
||||
|
||||
.. code-block:: c++
|
||||
.. code:: c++
|
||||
|
||||
std::string s = str(fmt::Format("{0}{1}{0}") << "abra" << "cad");
|
||||
std::string s = fmt::format("{0}{1}{0}", "abra", "cad");
|
||||
// s == "abracadabra"
|
||||
|
||||
Format can be used as a safe portable replacement for ``itoa``:
|
||||
C++ Format can be used as a safe portable replacement for ``itoa``:
|
||||
|
||||
.. code-block:: c++
|
||||
.. code:: c++
|
||||
|
||||
fmt::Writer w;
|
||||
fmt::MemoryWriter w;
|
||||
w << 42; // replaces itoa(42, buffer, 10)
|
||||
w << fmt::hex(42); // replaces itoa(42, buffer, 16)
|
||||
// access the string using w.str() or w.c_str()
|
||||
@@ -72,7 +87,7 @@ Format can be used as a safe portable replacement for ``itoa``:
|
||||
An object of any user-defined type for which there is an overloaded
|
||||
:code:`std::ostream` insertion operator (``operator<<``) can be formatted:
|
||||
|
||||
.. code-block:: c++
|
||||
.. code:: c++
|
||||
|
||||
class Date {
|
||||
int year_, month_, day_;
|
||||
@@ -84,44 +99,64 @@ An object of any user-defined type for which there is an overloaded
|
||||
}
|
||||
};
|
||||
|
||||
std::string s = str(fmt::Format("The date is {}") << Date(2012, 12, 9));
|
||||
std::string s = fmt::format("The date is {}", Date(2012, 12, 9));
|
||||
// s == "The date is 2012-12-9"
|
||||
|
||||
You can use `fmt::Formatter
|
||||
<http://cppformat.github.io/format/#project0classfmt_1_1_formatter>`__
|
||||
to create your own functions similar to `fmt::Format
|
||||
<http://cppformat.github.io/format/#fmt::Format__StringRef>`__ and ``fmt::Print``
|
||||
with an arbitrary action performed when formatting is complete:
|
||||
You can use the `FMT_VARIADIC
|
||||
<http://cppformat.readthedocs.org/en/latest/reference.html#utilities>`_
|
||||
macro to create your own functions similar to `format
|
||||
<http://cppformat.readthedocs.org/en/latest/reference.html#format>`_ and
|
||||
`print <http://cppformat.readthedocs.org/en/latest/reference.html#print>`_
|
||||
which take arbitrary arguments:
|
||||
|
||||
.. code-block:: c++
|
||||
.. code:: c++
|
||||
|
||||
struct PrintError {
|
||||
void operator()(const fmt::Writer &w) const {
|
||||
std::cerr << "Error: " << w.str() << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
// Formats an error message and prints it to std::cerr.
|
||||
fmt::Formatter<PrintError> ReportError(const char *format) {
|
||||
fmt::Formatter<PrintError> f(format);
|
||||
return f;
|
||||
// Prints formatted error message.
|
||||
void report_error(const char *format, fmt::ArgList args) {
|
||||
fmt::print("Error: ");
|
||||
fmt::print(format, args);
|
||||
}
|
||||
FMT_VARIADIC(void, report_error, const char *)
|
||||
|
||||
ReportError("File not found: {}") << path;
|
||||
report_error("file not found: {}", path);
|
||||
|
||||
Note that you only need to define one function that takes ``fmt::ArgList``
|
||||
argument. ``FMT_VARIADIC`` automatically defines necessary wrappers that
|
||||
accept variable number of arguments.
|
||||
|
||||
Projects using this library
|
||||
---------------------------
|
||||
|
||||
* `AMPL <https://github.com/ampl/ampl>`__:
|
||||
Open-source AMPL solver interface, solver connections, table handlers
|
||||
and examples
|
||||
* `0 A.D. <http://play0ad.com/>`_: A free, open-source, cross-platform real-time strategy game
|
||||
|
||||
* `Saddy <https://code.google.com/p/saddy/>`__:
|
||||
* `AMPL/MP <https://github.com/ampl/mp>`_:
|
||||
An open-source library for mathematical programming
|
||||
|
||||
* `HarpyWar/pvpgn <https://github.com/HarpyWar/pvpgn>`_:
|
||||
Player vs Player Gaming Network with tweaks
|
||||
|
||||
* `KBEngine <http://www.kbengine.org/>`_: An open-source MMOG server engine
|
||||
|
||||
* `Lifeline <https://github.com/peter-clark/lifeline>`_: A 2D game
|
||||
|
||||
* `PenUltima Online (POL) <http://www.polserver.com/>`_:
|
||||
An MMO server, compatible with most Ultima Online clients
|
||||
|
||||
* `readpe <https://bitbucket.org/sys_dev/readpe>`_: Read Portable Executable
|
||||
|
||||
* `Saddy <https://code.google.com/p/saddy/>`_:
|
||||
Small crossplatform 2D graphic engine
|
||||
|
||||
If you are aware of other projects using ``format``, please let me know
|
||||
by `email <mailto:victor.zverovich@gmail.com>`__ or by submitting an
|
||||
`issue <https://github.com/cppformat/format/issues>`__.
|
||||
* `Salesforce Analytics Cloud <http://www.salesforce.com/analytics-cloud/overview/>`_:
|
||||
Business intelligence software
|
||||
|
||||
* `spdlog <https://github.com/gabime/spdlog>`_: Super fast C++ logging library
|
||||
|
||||
`More... <https://github.com/search?q=cppformat&type=Code>`_
|
||||
|
||||
If you are aware of other projects using this library, please let me know
|
||||
by `email <mailto:victor.zverovich@gmail.com>`_ or by submitting an
|
||||
`issue <https://github.com/cppformat/cppformat/issues>`_.
|
||||
|
||||
Motivation
|
||||
----------
|
||||
@@ -138,12 +173,12 @@ Printf
|
||||
~~~~~~
|
||||
|
||||
The good thing about printf is that it is very fast and readily available
|
||||
being the part of the C standard library. The main drawback is that it
|
||||
being a part of the C standard library. The main drawback is that it
|
||||
doesn't support user-defined types. Printf also has safety issues although
|
||||
they are mostly solved with `__attribute__ ((format (printf, ...))
|
||||
<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`__ in GCC.
|
||||
they are mostly solved with `_attribute__ ((format (printf, ...))
|
||||
<http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_ in GCC.
|
||||
There is a POSIX extension that adds positional arguments required for
|
||||
`i18n <http://en.wikipedia.org/wiki/Internationalization_and_localization>`__
|
||||
`i18n <http://en.wikipedia.org/wiki/Internationalization_and_localization>`_
|
||||
to printf but it is not a part of C99 and may not be available on some
|
||||
platforms.
|
||||
|
||||
@@ -152,13 +187,13 @@ IOStreams
|
||||
|
||||
The main issue with IOStreams is best illustrated with an example:
|
||||
|
||||
.. code-block:: c++
|
||||
.. code:: c++
|
||||
|
||||
std::cout << std::setprecision(2) << std::fixed << 1.23456 << "\n";
|
||||
|
||||
which is a lot of typing compared to printf:
|
||||
|
||||
.. code-block:: c++
|
||||
.. code:: c++
|
||||
|
||||
printf("%.2f\n", 1.23456);
|
||||
|
||||
@@ -207,9 +242,8 @@ Tinyformat
|
||||
|
||||
This library supports printf-like format strings and is very small and
|
||||
fast. Unfortunately it doesn't support positional arguments and wrapping
|
||||
it in C++98 is somewhat difficult. However if you only need a type-safe
|
||||
printf replacement with support for user-defined types, I highly recommend
|
||||
this library.
|
||||
it in C++98 is somewhat difficult. Also its performance and code compactness
|
||||
are limited by IOStreams.
|
||||
|
||||
Boost Spirit.Karma
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
@@ -219,7 +253,16 @@ for completeness. As IOStreams it suffers from the problem of mixing
|
||||
verbatim text with arguments. The library is pretty fast, but slower
|
||||
on integer formatting than ``fmt::Writer`` on Karma's own benchmark,
|
||||
see `Fast integer to string conversion in C++
|
||||
<http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html>`__.
|
||||
<http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html>`_.
|
||||
|
||||
What Users Say
|
||||
--------------
|
||||
|
||||
Thanks for creating this library. It’s been a hole in C++ for a long time.
|
||||
I’ve used both boost::format and loki::SPrintf, and neither felt like the
|
||||
right answer. This does.
|
||||
|
||||
-- Kurt Haas
|
||||
|
||||
Benchmarks
|
||||
----------
|
||||
@@ -228,107 +271,105 @@ Speed tests
|
||||
~~~~~~~~~~~
|
||||
|
||||
The following speed tests results were generated by building
|
||||
``tinyformat_test.cpp`` on Ubuntu GNU/Linux 12.10 with
|
||||
``g++-4.7.2 -O3 -DSPEED_TEST -DHAVE_FORMAT``, and taking the best of three
|
||||
``tinyformat_test.cpp`` on Ubuntu GNU/Linux 14.04.1 with
|
||||
``g++-4.8.2 -O3 -DSPEED_TEST -DHAVE_FORMAT``, and taking the best of three
|
||||
runs. In the test, the format string ``"%0.10f:%04d:%+g:%s:%p:%c:%%\n"`` or
|
||||
equivalent is filled 2000000 times with output sent to ``/dev/null``; for
|
||||
further details see the `source
|
||||
<https://github.com/cppformat/format-benchmark/blob/master/tinyformat_test.cpp>`__.
|
||||
<https://github.com/cppformat/format-benchmark/blob/master/tinyformat_test.cpp>`_.
|
||||
|
||||
============== ========
|
||||
test name run time
|
||||
============== ========
|
||||
libc printf 1.28s
|
||||
std::ostream 2.09s
|
||||
format 1.32s
|
||||
tinyformat 2.55s
|
||||
boost::format 10.42s
|
||||
============== ========
|
||||
================= ============= ===========
|
||||
Library Method Run Time, s
|
||||
================= ============= ===========
|
||||
EGLIBC 2.19 printf 1.30
|
||||
libstdc++ 4.8.2 std::ostream 1.85
|
||||
C++ Format 1.0 fmt::print 1.42
|
||||
tinyformat 2.0.1 tfm::printf 2.25
|
||||
Boost Format 1.54 boost::format 9.94
|
||||
================= ============= ===========
|
||||
|
||||
As you can see boost::format is much slower than the alternative methods; this
|
||||
is confirmed by `other tests <http://accu.org/index.php/journals/1539>`__.
|
||||
is confirmed by `other tests <http://accu.org/index.php/journals/1539>`_.
|
||||
Tinyformat is quite good coming close to IOStreams. Unfortunately tinyformat
|
||||
cannot be faster than the IOStreams because it uses them internally.
|
||||
Performance of format is close to that of printf.
|
||||
Performance of cppformat is close to that of printf, being `faster than printf on integer
|
||||
formatting <http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html>`_,
|
||||
but slower on floating-point formatting which dominates this benchmark.
|
||||
|
||||
Compile time and code bloat
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The script ``bloat_test.sh`` from the `tinyformat
|
||||
<https://github.com/c42f/tinyformat>`__ repository tests compile time and
|
||||
code bloat for nontrivial projects. It generates 100 translation units
|
||||
and uses ``printf()`` or its alternative five times in each to simulate
|
||||
a medium sized project. The resulting executable size and compile time
|
||||
(g++-4.7.2, Ubuntu GNU/Linux 12.10, best of three) is shown in the following
|
||||
tables.
|
||||
|
||||
**Non-optimized build**
|
||||
|
||||
====================== ================== ==========================
|
||||
test name total compile time executable size (stripped)
|
||||
====================== ================== ==========================
|
||||
libc printf 2.8s 44K (32K)
|
||||
std::ostream 12.9s 84K (60K)
|
||||
format 16.0s 152K (128K)
|
||||
tinyformat 20.6s 240K (200K)
|
||||
boost::format 76.0s 888K (780K)
|
||||
====================== ================== ==========================
|
||||
The script `bloat-test.py
|
||||
<https://github.com/cppformat/format-benchmark/blob/master/bloat-test.py>`_
|
||||
from `format-benchmark <https://github.com/cppformat/format-benchmark>`_
|
||||
tests compile time and code bloat for nontrivial projects.
|
||||
It generates 100 translation units and uses ``printf()`` or its alternative
|
||||
five times in each to simulate a medium sized project. The resulting
|
||||
executable size and compile time (g++-4.8.1, Ubuntu GNU/Linux 13.10,
|
||||
best of three) is shown in the following tables.
|
||||
|
||||
**Optimized build (-O3)**
|
||||
|
||||
====================== ================== ==========================
|
||||
test name total compile time executable size (stripped)
|
||||
====================== ================== ==========================
|
||||
libc printf 3.5s 40K (28K)
|
||||
std::ostream 14.1s 88K (64K)
|
||||
format 25.1s 552K (536K)
|
||||
tinyformat 56.3s 200K (164K)
|
||||
boost::format 169.4s 1.7M (1.6M)
|
||||
====================== ================== ==========================
|
||||
============ =============== ==================== ==================
|
||||
Method Compile Time, s Executable size, KiB Stripped size, KiB
|
||||
============ =============== ==================== ==================
|
||||
printf 2.6 41 30
|
||||
IOStreams 19.4 92 70
|
||||
C++ Format 46.8 46 34
|
||||
tinyformat 64.6 418 386
|
||||
Boost Format 222.8 990 923
|
||||
============ =============== ==================== ==================
|
||||
|
||||
Printf and std::ostream win here which is not surprising considering
|
||||
that they are included in the standard library. Tinyformat has somewhat
|
||||
slower compilation times compared to format. Interestingly optimized
|
||||
executable size is smaller with tinyformat then with format and for
|
||||
non-optimized build its the other way around. Boost::format has by far
|
||||
the largest overheads.
|
||||
As you can see, C++ Format has 80% less overhead in terms of resulting
|
||||
code size compared to IOStreams and comes pretty close to ``printf``.
|
||||
Boost Format has by far the largest overheads.
|
||||
|
||||
**Non-optimized build**
|
||||
|
||||
============ =============== ==================== ==================
|
||||
Method Compile Time, s Executable size, KiB Stripped size, KiB
|
||||
============ =============== ==================== ==================
|
||||
printf 2.1 41 30
|
||||
IOStreams 19.7 86 62
|
||||
C++ Format 47.9 108 86
|
||||
tinyformat 27.7 234 190
|
||||
Boost Format 122.6 884 763
|
||||
============ =============== ==================== ==================
|
||||
|
||||
``libc``, ``libstdc++`` and ``libformat`` are all linked as shared
|
||||
libraries to compare formatting function overhead only. Boost Format
|
||||
and tinyformat are header-only libraries so they don't provide any
|
||||
linkage options.
|
||||
|
||||
Running the tests
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
To run the tests you first need to get the format repository with submodules::
|
||||
Please refer to `Building the library`__ for the instructions on how to build
|
||||
the library and run the unit tests.
|
||||
|
||||
$ git clone --recursive git://github.com/cppformat/format.git
|
||||
__ http://cppformat.readthedocs.org/en/latest/usage.html#building-the-library
|
||||
|
||||
Then go to the format directory and generate Makefiles with
|
||||
`CMake <http://www.cmake.org/>`__::
|
||||
Benchmarks reside in a separate repository,
|
||||
`format-benchmarks <https://github.com/cppformat/format-benchmark>`_,
|
||||
so to run the benchmarks you first need to clone this repository and
|
||||
generate Makefiles with CMake::
|
||||
|
||||
$ cd format
|
||||
$ git clone --recursive https://github.com/cppformat/format-benchmark.git
|
||||
$ cd format-benchmark
|
||||
$ cmake .
|
||||
|
||||
Next use the following commands to run the speed test::
|
||||
Then you can run the speed test::
|
||||
|
||||
$ make speed_test
|
||||
$ make speed-test
|
||||
|
||||
or the bloat test::
|
||||
|
||||
$ make bloat_test
|
||||
|
||||
Portability
|
||||
-----------
|
||||
|
||||
The format library has been tested on the following systems and compilers:
|
||||
|
||||
* 64-bit (amd64) GNU/Linux with GCC 4.6.3, 4.7.2 and Intel C++ Compiler (ICC) 14.0.2
|
||||
* 32-bit (i386) GNU/Linux with GCC 4.6.3
|
||||
* Mac OS X with GCC 4.2.1 and Clang 4.2
|
||||
* 64-bit Windows with Visual C++ 2010
|
||||
* 32-bit Windows with Visual C++ 2010
|
||||
$ make bloat-test
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
Copyright (c) 2012, Victor Zverovich
|
||||
Copyright (c) 2012 - 2015, Victor Zverovich
|
||||
|
||||
All rights reserved.
|
||||
|
||||
@@ -356,30 +397,31 @@ Documentation License
|
||||
---------------------
|
||||
|
||||
The `Format String Syntax
|
||||
<http://cppformat.github.io/format#format-string-syntax>`__
|
||||
<http://cppformat.readthedocs.org/en/latest/syntax.html>`_
|
||||
section in the documentation is based on the one from Python `string module
|
||||
documentation <http://docs.python.org/3/library/string.html#module-string>`__
|
||||
documentation <http://docs.python.org/3/library/string.html#module-string>`_
|
||||
adapted for the current library. For this reason the documentation is
|
||||
distributed under the Python Software Foundation license available in
|
||||
`doc/LICENSE.python
|
||||
<https://raw.github.com/cppformat/format/master/doc/LICENSE.python>`__.
|
||||
<https://raw.github.com/cppformat/cppformat/master/doc/LICENSE.python>`_.
|
||||
|
||||
Acknowledgments
|
||||
---------------
|
||||
|
||||
The benchmark section of this readme file and the performance tests are taken
|
||||
from the excellent `tinyformat <https://github.com/c42f/tinyformat>`__ library
|
||||
from the excellent `tinyformat <https://github.com/c42f/tinyformat>`_ library
|
||||
written by Chris Foster. Boost Format library is acknowledged transitively
|
||||
since it had some influence on tinyformat.
|
||||
Some ideas used in the implementation are borrowed from `Loki
|
||||
<http://loki-lib.sourceforge.net/>`__ SafeFormat and `Diagnostic API
|
||||
<http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html>`__ in
|
||||
`Clang <http://clang.llvm.org/>`__.
|
||||
<http://loki-lib.sourceforge.net/>`_ SafeFormat and `Diagnostic API
|
||||
<http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html>`_ in
|
||||
`Clang <http://clang.llvm.org/>`_.
|
||||
Format string syntax and the documentation are based on Python's `str.format
|
||||
<http://docs.python.org/2/library/stdtypes.html#str.format>`__.
|
||||
Thanks `Doug Turnbull <https://github.com/softwaredoug>`__ for his valuable
|
||||
<http://docs.python.org/2/library/stdtypes.html#str.format>`_.
|
||||
Thanks `Doug Turnbull <https://github.com/softwaredoug>`_ for his valuable
|
||||
comments and contribution to the design of the type-safe API and
|
||||
`Gregory Czajkowski <https://github.com/gcflymoto>`__ for implementing binary
|
||||
formatting. Thanks `Ruslan Baratov <https://github.com/ruslo>`__ for comprehensive
|
||||
`comparison of integer formatting algorithms <https://github.com/ruslo/int-dec-format-tests>`__
|
||||
and useful comments regarding performance.
|
||||
`Gregory Czajkowski <https://github.com/gcflymoto>`_ for implementing binary
|
||||
formatting. Thanks `Ruslan Baratov <https://github.com/ruslo>`_ for comprehensive
|
||||
`comparison of integer formatting algorithms <https://github.com/ruslo/int-dec-format-tests>`_
|
||||
and useful comments regarding performance, `Boris Kaul <https://github.com/localvoid>`_ for
|
||||
`C++ counting digits benchmark <https://github.com/localvoid/cxx-benchmark-count-digits>`_.
|
||||
|
||||
18
appveyor.yml
Normal file
18
appveyor.yml
Normal file
@@ -0,0 +1,18 @@
|
||||
environment:
|
||||
CTEST_OUTPUT_ON_FAILURE: 1
|
||||
matrix:
|
||||
- BUILD: msvc
|
||||
CONFIG: Debug
|
||||
- BUILD: msvc
|
||||
CONFIG: Release
|
||||
- BUILD: mingw
|
||||
CONFIG: Debug
|
||||
- BUILD: mingw
|
||||
CONFIG: Release
|
||||
|
||||
build_script:
|
||||
- python support/appveyor-build.py
|
||||
|
||||
on_failure:
|
||||
- appveyor PushArtifact Testing/Temporary/LastTest.log
|
||||
- appveyor AddTest test
|
||||
2
breathe
2
breathe
Submodule breathe updated: 1254e9510e...33a6cce142
@@ -1,6 +1,16 @@
|
||||
add_custom_command(OUTPUT html/index.html
|
||||
COMMAND doxygen
|
||||
COMMAND rm -rf html
|
||||
COMMAND sphinx-build -b html . html
|
||||
DEPENDS conf.py index.rst)
|
||||
add_custom_target(doc DEPENDS html/index.html)
|
||||
if (NOT EXISTS ${FORMAT_SOURCE_DIR}/breathe/breathe)
|
||||
message(STATUS "Target 'doc' disabled (requires breathe module)")
|
||||
return ()
|
||||
endif ()
|
||||
|
||||
foreach (program doxygen sphinx-build)
|
||||
find_program(${program} ${program})
|
||||
if (NOT ${program})
|
||||
message(STATUS "Target 'doc' disabled (requires ${program})")
|
||||
return ()
|
||||
endif ()
|
||||
endforeach ()
|
||||
|
||||
add_custom_target(doc
|
||||
COMMAND ${doxygen}
|
||||
COMMAND ${sphinx-build} -b html . html)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
PROJECT_NAME = Format
|
||||
PROJECT_NAME = C++ Format
|
||||
GENERATE_LATEX = NO
|
||||
GENERATE_MAN = NO
|
||||
GENERATE_RTF = NO
|
||||
@@ -12,3 +12,9 @@ GENERATE_XML = YES
|
||||
XML_OUTPUT = doxyxml
|
||||
ALIASES = "rst=\verbatim embed:rst"
|
||||
ALIASES += "endrst=\endverbatim"
|
||||
PREDEFINED = _WIN32=1 \
|
||||
FMT_NO_DEPRECATED=1 \
|
||||
FMT_USE_VARIADIC_TEMPLATES=1 \
|
||||
FMT_USE_RVALUE_REFERENCES=1
|
||||
EXCLUDE_SYMBOLS = fmt::internal::* BasicArg FormatParser StringValue \
|
||||
write_str
|
||||
|
||||
16
doc/_static/breathe.css
vendored
Normal file
16
doc/_static/breathe.css
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
/* -- breathe specific styles ----------------------------------------------- */
|
||||
|
||||
/* So enum value descriptions are displayed inline to the item */
|
||||
.breatheenumvalues li tt + p {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
/* So parameter descriptions are displayed inline to the item */
|
||||
.breatheparameterlist li tt + p {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
.container .breathe-sectiondef {
|
||||
width: inherit;
|
||||
}
|
||||
7
doc/_static/cppformat.css
vendored
Normal file
7
doc/_static/cppformat.css
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
.class dd, .define dd, .function dd {
|
||||
margin-left: 30px;
|
||||
}
|
||||
|
||||
.public-func dd {
|
||||
margin-left: 0px;
|
||||
}
|
||||
14
doc/_templates/layout.html
vendored
Normal file
14
doc/_templates/layout.html
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{% extends "!layout.html" %}
|
||||
|
||||
{% block footer %}
|
||||
{{ super() }}
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-20116650-4', 'cppformat.github.io');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -11,7 +11,7 @@
|
||||
# All configuration values have a default; values that are commented out
|
||||
# serve to show the default.
|
||||
|
||||
import sys, os
|
||||
import sys, os, re, subprocess
|
||||
|
||||
# If extensions (or modules to document with autodoc) are in another directory,
|
||||
# add these directories to sys.path here. If the directory is relative to the
|
||||
@@ -25,6 +25,9 @@ import sys, os
|
||||
|
||||
sys.path.append("../breathe")
|
||||
|
||||
if os.environ.get('READTHEDOCS', None) == 'True':
|
||||
subprocess.call('doxygen')
|
||||
|
||||
# Add any Sphinx extension module names here, as strings. They can be extensions
|
||||
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
|
||||
extensions = ['sphinx.ext.ifconfig', 'breathe']
|
||||
@@ -46,15 +49,25 @@ source_suffix = '.rst'
|
||||
master_doc = 'index'
|
||||
|
||||
# General information about the project.
|
||||
project = u'format'
|
||||
copyright = u'1990-2012, Python Software Foundation'
|
||||
project = u'C++ Format'
|
||||
copyright = u'2012-2014, Victor Zverovich'
|
||||
|
||||
# The version info for the project you're documenting, acts as replacement for
|
||||
# |version| and |release|, also used in various other places throughout the
|
||||
# built documents.
|
||||
#
|
||||
# The short X.Y version.
|
||||
version = '@CPACK_PACKAGE_VERSION_MAJOR@.@CPACK_PACKAGE_VERSION_MINOR@'
|
||||
|
||||
# Get version from CMakeLists.txt.
|
||||
version = {}
|
||||
with open('../CMakeLists.txt') as f:
|
||||
for line in f:
|
||||
m = re.match(r'set\(CPACK_PACKAGE_VERSION_([A-Z]+) ([0-9]+)\)', line.strip())
|
||||
if m:
|
||||
kind, value = m.groups()
|
||||
version[kind] = value
|
||||
version = '{}.{}.{}'.format(version['MAJOR'], version['MINOR'], version['PATCH'])
|
||||
|
||||
# The full version, including alpha/beta/rc tags.
|
||||
release = version
|
||||
|
||||
@@ -70,7 +83,7 @@ release = version
|
||||
|
||||
# List of patterns, relative to source directory, that match files and
|
||||
# directories to ignore when looking for source files.
|
||||
exclude_patterns = []
|
||||
exclude_patterns = ['sphinx-bootstrap-theme/*']
|
||||
|
||||
# The reST default role (used for this markup: `text`) to use for all documents.
|
||||
#default_role = None
|
||||
@@ -89,6 +102,10 @@ exclude_patterns = []
|
||||
# The name of the Pygments (syntax highlighting) style to use.
|
||||
pygments_style = 'sphinx'
|
||||
|
||||
highlight_language = 'c++'
|
||||
|
||||
primary_domain = 'cpp'
|
||||
|
||||
# A list of ignored prefixes for module index sorting.
|
||||
#modindex_common_prefix = []
|
||||
|
||||
@@ -97,15 +114,30 @@ pygments_style = 'sphinx'
|
||||
|
||||
# The theme to use for HTML and HTML Help pages. See the documentation for
|
||||
# a list of builtin themes.
|
||||
html_theme = 'sphinxdoc'
|
||||
html_theme = 'bootstrap'
|
||||
|
||||
# Theme options are theme-specific and customize the look and feel of a theme
|
||||
# further. For a list of options available for each theme, see the
|
||||
# documentation.
|
||||
#html_theme_options = {}
|
||||
html_theme_options = {
|
||||
# HTML navbar class (Default: "navbar") to attach to <div>.
|
||||
# For black navbar, do "navbar navbar-inverse"
|
||||
#'navbar_class': "navbar navbar-inverse",
|
||||
|
||||
# Fix navigation bar to top of page?
|
||||
# Values: "true" (default) or "false"
|
||||
'navbar_fixed_top': "true",
|
||||
|
||||
# Location of link to source.
|
||||
# Options are "nav" (default), "footer".
|
||||
'source_link_position': "footer",
|
||||
|
||||
# Render the next and previous page links in navbar. (Default: true)
|
||||
'navbar_sidebarrel': False
|
||||
}
|
||||
|
||||
# Add any paths that contain custom themes here, relative to this directory.
|
||||
#html_theme_path = []
|
||||
html_theme_path = ['sphinx-bootstrap-theme']
|
||||
|
||||
# The name for this set of Sphinx documents. If None, it defaults to
|
||||
# "<project> v<release> documentation".
|
||||
@@ -189,7 +221,7 @@ latex_elements = {
|
||||
# Grouping the document tree into LaTeX files. List of tuples
|
||||
# (source start file, target name, title, author, documentclass [howto/manual]).
|
||||
latex_documents = [
|
||||
('index', 'format.tex', u'format Documentation',
|
||||
('index', 'format.tex', u'C++ Format Documentation',
|
||||
u'Victor Zverovich', 'manual'),
|
||||
]
|
||||
|
||||
433
doc/index.rst
433
doc/index.rst
@@ -1,427 +1,10 @@
|
||||
.. highlight:: c++
|
||||
##########
|
||||
C++ Format
|
||||
##########
|
||||
|
||||
.. _string-formatting-api:
|
||||
|
||||
C++ Format Library API
|
||||
----------------------
|
||||
|
||||
.. doxygenfunction:: fmt::Format(StringRef)
|
||||
|
||||
.. doxygenclass:: fmt::BasicWriter
|
||||
:members:
|
||||
|
||||
.. doxygenclass:: fmt::Formatter
|
||||
:members:
|
||||
|
||||
.. doxygenclass:: fmt::NoAction
|
||||
:members:
|
||||
|
||||
.. doxygenclass:: fmt::Write
|
||||
:members:
|
||||
|
||||
.. doxygenclass:: fmt::BasicStringRef
|
||||
:members:
|
||||
|
||||
.. doxygenfunction:: fmt::str
|
||||
|
||||
.. doxygenfunction:: fmt::c_str
|
||||
|
||||
Write API
|
||||
---------
|
||||
|
||||
.. doxygenfunction:: fmt::bin
|
||||
|
||||
.. doxygenfunction:: fmt::oct
|
||||
|
||||
.. doxygenfunction:: fmt::hex
|
||||
|
||||
.. doxygenfunction:: fmt::hexu
|
||||
|
||||
.. doxygenfunction:: fmt::pad
|
||||
|
||||
.. _formatstrings:
|
||||
|
||||
Format String Syntax
|
||||
--------------------
|
||||
|
||||
The :cpp:func:`fmt::Format()` function and the :cpp:class:`fmt::Formatter`
|
||||
class share the same syntax for format strings.
|
||||
|
||||
Format strings contain "replacement fields" surrounded by curly braces ``{}``.
|
||||
Anything that is not contained in braces is considered literal text, which is
|
||||
copied unchanged to the output. If you need to include a brace character in the
|
||||
literal text, it can be escaped by doubling: ``{{`` and ``}}``.
|
||||
|
||||
The grammar for a replacement field is as follows:
|
||||
|
||||
.. productionlist:: sf
|
||||
replacement_field: "{" [`arg_index`] [":" `format_spec`] "}"
|
||||
arg_index: `integer`
|
||||
|
||||
In less formal terms, the replacement field can start with an *arg_index*
|
||||
that specifies the argument whose value is to be formatted and inserted into
|
||||
the output instead of the replacement field.
|
||||
The *arg_index* is optionally followed by a *format_spec*, which is preceded
|
||||
by a colon ``':'``. These specify a non-default format for the replacement value.
|
||||
|
||||
See also the :ref:`formatspec` section.
|
||||
|
||||
If the numerical arg_indexes in a format string are 0, 1, 2, ... in sequence,
|
||||
they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be
|
||||
automatically inserted in that order.
|
||||
|
||||
Some simple format string examples::
|
||||
|
||||
"First, thou shalt count to {0}" // References the first argument
|
||||
"Bring me a {}" // Implicitly references the first argument
|
||||
"From {} to {}" // Same as "From {0} to {1}"
|
||||
|
||||
The *format_spec* field contains a specification of how the value should be
|
||||
presented, including such details as field width, alignment, padding, decimal
|
||||
precision and so on. Each value type can define its own "formatting
|
||||
mini-language" or interpretation of the *format_spec*.
|
||||
|
||||
Most built-in types support a common formatting mini-language, which is
|
||||
described in the next section.
|
||||
|
||||
A *format_spec* field can also include nested replacement fields within it.
|
||||
These nested replacement fields can contain only an argument index;
|
||||
format specifications are not allowed. Formatting is performed as if the
|
||||
replacement fields within the format_spec are substituted before the
|
||||
*format_spec* string is interpreted. This allows the formatting of a value
|
||||
to be dynamically specified.
|
||||
|
||||
See the :ref:`formatexamples` section for some examples.
|
||||
|
||||
|
||||
.. _formatspec:
|
||||
|
||||
Format Specification Mini-Language
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
"Format specifications" are used within replacement fields contained within a
|
||||
format string to define how individual values are presented (see
|
||||
:ref:`formatstrings`). They can also be passed directly to the
|
||||
:func:`Format` function. Each formattable type may define how the format
|
||||
specification is to be interpreted.
|
||||
|
||||
Most built-in types implement the following options for format specifications,
|
||||
although some of the formatting options are only supported by the numeric types.
|
||||
|
||||
The general form of a *standard format specifier* is:
|
||||
|
||||
.. productionlist:: sf
|
||||
format_spec: [[`fill`]`align`][`sign`]["#"]["0"][`width`]["." `precision`][`type`]
|
||||
fill: <a character other than '{' or '}'>
|
||||
align: "<" | ">" | "=" | "^"
|
||||
sign: "+" | "-" | " "
|
||||
width: `integer`
|
||||
precision: `integer` | "{" `arg_index` "}"
|
||||
type: `int_type` | "c" | "e" | "E" | "f" | "F" | "g" | "G" | "p" | s"
|
||||
int_type: "b" | "B" | "d" | "o" | "x" | "X"
|
||||
|
||||
The *fill* character can be any character other than '{' or '}'. The presence
|
||||
of a fill character is signaled by the character following it, which must be
|
||||
one of the alignment options. If the second character of *format_spec* is not
|
||||
a valid alignment option, then it is assumed that both the fill character and
|
||||
the alignment option are absent.
|
||||
|
||||
The meaning of the various alignment options is as follows:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Option | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'<'`` | Forces the field to be left-aligned within the available |
|
||||
| | space (this is the default for most objects). |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'>'`` | Forces the field to be right-aligned within the |
|
||||
| | available space (this is the default for numbers). |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'='`` | Forces the padding to be placed after the sign (if any) |
|
||||
| | but before the digits. This is used for printing fields |
|
||||
| | in the form '+000000120'. This alignment option is only |
|
||||
| | valid for numeric types. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'^'`` | Forces the field to be centered within the available |
|
||||
| | space. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
Note that unless a minimum field width is defined, the field width will always
|
||||
be the same size as the data to fill it, so that the alignment option has no
|
||||
meaning in this case.
|
||||
|
||||
The *sign* option is only valid for number types, and can be one of the
|
||||
following:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Option | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'+'`` | indicates that a sign should be used for both |
|
||||
| | positive as well as negative numbers. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'-'`` | indicates that a sign should be used only for negative |
|
||||
| | numbers (this is the default behavior). |
|
||||
+---------+----------------------------------------------------------+
|
||||
| space | indicates that a leading space should be used on |
|
||||
| | positive numbers, and a minus sign on negative numbers. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
|
||||
The ``'#'`` option causes the "alternate form" to be used for the
|
||||
conversion. The alternate form is defined differently for different
|
||||
types. This option is only valid for integer and floating-point types.
|
||||
For integers, when binary, octal, or hexadecimal output is used, this
|
||||
option adds the prefix respective ``"0b"`` (``"0B"``), ``"0"``, or
|
||||
``"0x"`` (``"0X"``) to the output value. Whether the prefix is
|
||||
lower-case or upper-case is determined by the case of the type
|
||||
specifier, for example, the prefix ``"0x"`` is used for the type ``'x'``
|
||||
and ``"0X"`` is used for ``'X'``. For floating-point numbers the
|
||||
alternate form causes the result of the conversion to always contain a
|
||||
decimal-point character, even if no digits follow it. Normally, a
|
||||
decimal-point character appears in the result of these conversions
|
||||
only if a digit follows it. In addition, for ``'g'`` and ``'G'``
|
||||
conversions, trailing zeros are not removed from the result.
|
||||
|
||||
.. ifconfig:: False
|
||||
|
||||
The ``','`` option signals the use of a comma for a thousands separator.
|
||||
For a locale aware separator, use the ``'n'`` integer presentation type
|
||||
instead.
|
||||
|
||||
*width* is a decimal integer defining the minimum field width. If not
|
||||
specified, then the field width will be determined by the content.
|
||||
|
||||
Preceding the *width* field by a zero (``'0'``) character enables
|
||||
sign-aware zero-padding for numeric types. This is equivalent to a *fill*
|
||||
character of ``'0'`` with an *alignment* type of ``'='``.
|
||||
|
||||
The *precision* is a decimal number indicating how many digits should be
|
||||
displayed after the decimal point for a floating-point value formatted with
|
||||
``'f'`` and ``'F'``, or before and after the decimal point for a floating-point
|
||||
value formatted with ``'g'`` or ``'G'``. For non-number types the field
|
||||
indicates the maximum field size - in other words, how many characters will be
|
||||
used from the field content. The *precision* is not allowed for integer values.
|
||||
|
||||
Finally, the *type* determines how the data should be presented.
|
||||
|
||||
The available string presentation types are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'s'`` | String format. This is the default type for strings and |
|
||||
| | may be omitted. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'s'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The available character presentation types are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'c'`` | Character format. This is the default type for |
|
||||
| | characters and may be omitted. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'c'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The available integer presentation types are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'b'`` | Binary format. Outputs the number in base 2. Using the |
|
||||
| | ``'#'`` option with this type adds the prefix ``"0b"`` |
|
||||
| | to the output value. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'B'`` | Binary format. Outputs the number in base 2. Using the |
|
||||
| | ``'#'`` option with this type adds the prefix ``"0B"`` |
|
||||
| | to the output value. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'d'`` | Decimal integer. Outputs the number in base 10. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'o'`` | Octal format. Outputs the number in base 8. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'x'`` | Hex format. Outputs the number in base 16, using |
|
||||
| | lower-case letters for the digits above 9. Using the |
|
||||
| | ``'#'`` option with this type adds the prefix ``"0x"`` |
|
||||
| | to the output value. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'X'`` | Hex format. Outputs the number in base 16, using |
|
||||
| | upper-case letters for the digits above 9. Using the |
|
||||
| | ``'#'`` option with this type adds the prefix ``"0X"`` |
|
||||
| | to the output value. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'d'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The available presentation types for floating-point values are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'e'`` | Exponent notation. Prints the number in scientific |
|
||||
| | notation using the letter 'e' to indicate the exponent. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an |
|
||||
| | upper case 'E' as the separator character. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'f'`` | Fixed point. Displays the number as a fixed-point |
|
||||
| | number. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'F'`` | Fixed point. Same as ``'f'``, but converts ``nan`` to |
|
||||
| | ``NAN`` and ``inf`` to ``INF``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'g'`` | General format. For a given precision ``p >= 1``, |
|
||||
| | this rounds the number to ``p`` significant digits and |
|
||||
| | then formats the result in either fixed-point format |
|
||||
| | or in scientific notation, depending on its magnitude. |
|
||||
| | |
|
||||
| | A precision of ``0`` is treated as equivalent to a |
|
||||
| | precision of ``1``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'G'`` | General format. Same as ``'g'`` except switches to |
|
||||
| | ``'E'`` if the number gets too large. The |
|
||||
| | representations of infinity and NaN are uppercased, too. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'g'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
.. ifconfig:: False
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| | The precise rules are as follows: suppose that the |
|
||||
| | result formatted with presentation type ``'e'`` and |
|
||||
| | precision ``p-1`` would have exponent ``exp``. Then |
|
||||
| | if ``-4 <= exp < p``, the number is formatted |
|
||||
| | with presentation type ``'f'`` and precision |
|
||||
| | ``p-1-exp``. Otherwise, the number is formatted |
|
||||
| | with presentation type ``'e'`` and precision ``p-1``. |
|
||||
| | In both cases insignificant trailing zeros are removed |
|
||||
| | from the significand, and the decimal point is also |
|
||||
| | removed if there are no remaining digits following it. |
|
||||
| | |
|
||||
| | Positive and negative infinity, positive and negative |
|
||||
| | zero, and nans, are formatted as ``inf``, ``-inf``, |
|
||||
| | ``0``, ``-0`` and ``nan`` respectively, regardless of |
|
||||
| | the precision. |
|
||||
| | |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The available presentation types for pointers are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'p'`` | Pointer format. This is the default type for |
|
||||
| | pointers and may be omitted. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'p'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
|
||||
.. _formatexamples:
|
||||
|
||||
Format examples
|
||||
^^^^^^^^^^^^^^^
|
||||
|
||||
This section contains examples of the format syntax and comparison with
|
||||
the printf formatting.
|
||||
|
||||
In most of the cases the syntax is similar to the printf formatting, with the
|
||||
addition of the ``{}`` and with ``:`` used instead of ``%``.
|
||||
For example, ``"%03.2f"`` can be translated to ``"{:03.2f}"``.
|
||||
|
||||
The new format syntax also supports new and different options, shown in the
|
||||
following examples.
|
||||
|
||||
Accessing arguments by position::
|
||||
|
||||
Format("{0}, {1}, {2}") << 'a' << 'b' << 'c';
|
||||
// Result: "a, b, c"
|
||||
Format("{}, {}, {}") << 'a' << 'b' << 'c';
|
||||
// Result: "a, b, c"
|
||||
Format("{2}, {1}, {0}") << 'a' << 'b' << 'c';
|
||||
// Result: "c, b, a"
|
||||
Format("{0}{1}{0}") << "abra" << "cad"; // arguments' indices can be repeated
|
||||
// Result: "abracadabra"
|
||||
|
||||
Aligning the text and specifying a width::
|
||||
|
||||
Format("{:<30}") << "left aligned";
|
||||
// Result: "left aligned "
|
||||
Format("{:>30}") << "right aligned"
|
||||
// Result: " right aligned"
|
||||
Format("{:^30}") << "centered"
|
||||
// Result: " centered "
|
||||
Format("{:*^30}") << "centered" // use '*' as a fill char
|
||||
// Result: "***********centered***********"
|
||||
|
||||
Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign::
|
||||
|
||||
Format("{:+f}; {:+f}") << 3.14 << -3.14; // show it always
|
||||
// Result: "+3.140000; -3.140000"
|
||||
Format("{: f}; {: f}") << 3.14 << -3.14; // show a space for positive numbers
|
||||
// Result: " 3.140000; -3.140000"
|
||||
Format("{:-f}; {:-f}") << 3.14 << -3.14; // show only the minus -- same as '{:f}; {:f}'
|
||||
// Result: "3.140000; -3.140000"
|
||||
|
||||
Replacing ``%x`` and ``%o`` and converting the value to different bases::
|
||||
|
||||
Format("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}") << 42;
|
||||
// Result: "int: 42; hex: 2a; oct: 52; bin: 101010"
|
||||
// with 0x or 0 or 0b as prefix:
|
||||
Format("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}") << 42;
|
||||
// Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010"
|
||||
|
||||
.. ifconfig:: False
|
||||
|
||||
Using the comma as a thousands separator::
|
||||
|
||||
Format("{:,}") << 1234567890)
|
||||
'1,234,567,890'
|
||||
|
||||
Expressing a percentage::
|
||||
|
||||
>>> points = 19
|
||||
>>> total = 22
|
||||
Format("Correct answers: {:.2%}") << points/total)
|
||||
'Correct answers: 86.36%'
|
||||
|
||||
Using type-specific formatting::
|
||||
|
||||
>>> import datetime
|
||||
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
|
||||
Format("{:%Y-%m-%d %H:%M:%S}") << d)
|
||||
'2010-07-04 12:15:58'
|
||||
|
||||
Nesting arguments and more complex examples::
|
||||
|
||||
>>> for align, text in zip('<^>', ['left', 'center', 'right']):
|
||||
... '{0:{fill}{align}16}") << text, fill=align, align=align)
|
||||
...
|
||||
'left<<<<<<<<<<<<'
|
||||
'^^^^^center^^^^^'
|
||||
'>>>>>>>>>>>right'
|
||||
>>>
|
||||
>>> octets = [192, 168, 0, 1]
|
||||
Format("{:02X}{:02X}{:02X}{:02X}") << *octets)
|
||||
'C0A80001'
|
||||
>>> int(_, 16)
|
||||
3232235521
|
||||
>>>
|
||||
>>> width = 5
|
||||
>>> for num in range(5,12):
|
||||
... for base in 'dXob':
|
||||
... print('{0:{width}{base}}") << num, base=base, width=width), end=' ')
|
||||
... print()
|
||||
...
|
||||
5 5 5 101
|
||||
6 6 6 110
|
||||
7 7 7 111
|
||||
8 8 10 1000
|
||||
9 9 11 1001
|
||||
10 A 12 1010
|
||||
11 B 13 1011
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
||||
usage
|
||||
reference
|
||||
syntax
|
||||
|
||||
116
doc/reference.rst
Normal file
116
doc/reference.rst
Normal file
@@ -0,0 +1,116 @@
|
||||
.. cpp:namespace:: fmt
|
||||
|
||||
.. _string-formatting-api:
|
||||
|
||||
*************
|
||||
API Reference
|
||||
*************
|
||||
|
||||
All functions and classes provided by the C++ Format library reside
|
||||
in namespace ``fmt`` and macros have prefix ``FMT_``. For brevity the
|
||||
namespace is usually omitted in examples.
|
||||
|
||||
Formatting functions
|
||||
====================
|
||||
|
||||
The following functions use :ref:`format string syntax <syntax>` similar
|
||||
to the one used by Python's `str.format
|
||||
<http://docs.python.org/3/library/stdtypes.html#str.format>`_ function.
|
||||
They take *format_str* and *args* as arguments.
|
||||
|
||||
*format_str* is a format string that contains literal text and replacement
|
||||
fields surrounded by braces ``{}``. The fields are replaced with formatted
|
||||
arguments in the resulting string.
|
||||
|
||||
*args* is an argument list representing arbitrary arguments.
|
||||
|
||||
.. _format:
|
||||
|
||||
.. doxygenfunction:: format(StringRef, ArgList)
|
||||
|
||||
.. _print:
|
||||
|
||||
.. doxygenfunction:: print(StringRef, ArgList)
|
||||
|
||||
.. doxygenfunction:: print(std::FILE*, StringRef, ArgList)
|
||||
|
||||
.. doxygenfunction:: print(std::ostream&, StringRef, ArgList)
|
||||
|
||||
Printf formatting functions
|
||||
===========================
|
||||
|
||||
The following functions use `printf format string syntax
|
||||
<http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html>`_ with
|
||||
a POSIX extension for positional arguments.
|
||||
|
||||
.. doxygenfunction:: printf(StringRef, ArgList)
|
||||
|
||||
.. doxygenfunction:: fprintf(std::FILE*, StringRef, ArgList)
|
||||
|
||||
.. doxygenfunction:: sprintf(StringRef, ArgList)
|
||||
|
||||
Write API
|
||||
=========
|
||||
|
||||
.. doxygenclass:: fmt::BasicWriter
|
||||
:members:
|
||||
|
||||
.. doxygenclass:: fmt::BasicMemoryWriter
|
||||
:members:
|
||||
|
||||
.. doxygenclass:: fmt::BasicArrayWriter
|
||||
:members:
|
||||
|
||||
.. doxygenfunction:: bin
|
||||
|
||||
.. doxygenfunction:: oct
|
||||
|
||||
.. doxygenfunction:: hex
|
||||
|
||||
.. doxygenfunction:: hexu
|
||||
|
||||
.. doxygenfunction:: pad(int, unsigned int, Char)
|
||||
|
||||
Utilities
|
||||
=========
|
||||
|
||||
.. doxygendefine:: FMT_VARIADIC
|
||||
|
||||
.. doxygenclass:: fmt::ArgList
|
||||
:members:
|
||||
|
||||
.. doxygenclass:: fmt::BasicStringRef
|
||||
:members:
|
||||
|
||||
System Errors
|
||||
=============
|
||||
|
||||
.. doxygenclass:: fmt::SystemError
|
||||
:members:
|
||||
|
||||
.. doxygenclass:: fmt::WindowsError
|
||||
:members:
|
||||
|
||||
.. _formatstrings:
|
||||
|
||||
Custom allocators
|
||||
=================
|
||||
|
||||
The C++ Format library supports custom dynamic memory allocators.
|
||||
A custom allocator class can be specified as a template argument to
|
||||
:class:`fmt::BasicMemoryWriter`::
|
||||
|
||||
typedef fmt::BasicMemoryWriter<char, CustomAllocator> CustomMemoryWriter;
|
||||
|
||||
It is also possible to write a formatting function that uses a custom
|
||||
allocator::
|
||||
|
||||
typedef std::basic_string<char, std::char_traits<char>, CustomAllocator> CustomString;
|
||||
|
||||
CustomString format(CustomAllocator alloc, fmt::StringRef format_str,
|
||||
fmt::ArgList args) {
|
||||
CustomMemoryWriter writer(alloc);
|
||||
writer.write(format_str, args);
|
||||
return CustomString(writer.data(), writer.size(), alloc);
|
||||
}
|
||||
FMT_VARIADIC(CustomString, format, CustomAllocator, fmt::StringRef)
|
||||
1
doc/sphinx-bootstrap-theme
Submodule
1
doc/sphinx-bootstrap-theme
Submodule
Submodule doc/sphinx-bootstrap-theme added at c26c975e8a
391
doc/syntax.rst
Normal file
391
doc/syntax.rst
Normal file
@@ -0,0 +1,391 @@
|
||||
.. _syntax:
|
||||
|
||||
********************
|
||||
Format String Syntax
|
||||
********************
|
||||
|
||||
Formatting functions such as :ref:`fmt::format() <format>` and :ref:`fmt::print() <print>`
|
||||
use the same format string syntax described in this section.
|
||||
|
||||
Format strings contain "replacement fields" surrounded by curly braces ``{}``.
|
||||
Anything that is not contained in braces is considered literal text, which is
|
||||
copied unchanged to the output. If you need to include a brace character in the
|
||||
literal text, it can be escaped by doubling: ``{{`` and ``}}``.
|
||||
|
||||
The grammar for a replacement field is as follows:
|
||||
|
||||
.. productionlist:: sf
|
||||
replacement_field: "{" [`arg_index`] [":" `format_spec`] "}"
|
||||
arg_index: `integer`
|
||||
|
||||
In less formal terms, the replacement field can start with an *arg_index*
|
||||
that specifies the argument whose value is to be formatted and inserted into
|
||||
the output instead of the replacement field.
|
||||
The *arg_index* is optionally followed by a *format_spec*, which is preceded
|
||||
by a colon ``':'``. These specify a non-default format for the replacement value.
|
||||
|
||||
See also the :ref:`formatspec` section.
|
||||
|
||||
If the numerical arg_indexes in a format string are 0, 1, 2, ... in sequence,
|
||||
they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be
|
||||
automatically inserted in that order.
|
||||
|
||||
Some simple format string examples::
|
||||
|
||||
"First, thou shalt count to {0}" // References the first argument
|
||||
"Bring me a {}" // Implicitly references the first argument
|
||||
"From {} to {}" // Same as "From {0} to {1}"
|
||||
|
||||
The *format_spec* field contains a specification of how the value should be
|
||||
presented, including such details as field width, alignment, padding, decimal
|
||||
precision and so on. Each value type can define its own "formatting
|
||||
mini-language" or interpretation of the *format_spec*.
|
||||
|
||||
Most built-in types support a common formatting mini-language, which is
|
||||
described in the next section.
|
||||
|
||||
A *format_spec* field can also include nested replacement fields within it.
|
||||
These nested replacement fields can contain only an argument index;
|
||||
format specifications are not allowed. Formatting is performed as if the
|
||||
replacement fields within the format_spec are substituted before the
|
||||
*format_spec* string is interpreted. This allows the formatting of a value
|
||||
to be dynamically specified.
|
||||
|
||||
See the :ref:`formatexamples` section for some examples.
|
||||
|
||||
.. _formatspec:
|
||||
|
||||
Format Specification Mini-Language
|
||||
==================================
|
||||
|
||||
"Format specifications" are used within replacement fields contained within a
|
||||
format string to define how individual values are presented (see
|
||||
:ref:`syntax`). Each formattable type may define how the format
|
||||
specification is to be interpreted.
|
||||
|
||||
Most built-in types implement the following options for format specifications,
|
||||
although some of the formatting options are only supported by the numeric types.
|
||||
|
||||
The general form of a *standard format specifier* is:
|
||||
|
||||
.. productionlist:: sf
|
||||
format_spec: [[`fill`]`align`][`sign`]["#"]["0"][`width`]["." `precision`][`type`]
|
||||
fill: <a character other than '{' or '}'>
|
||||
align: "<" | ">" | "=" | "^"
|
||||
sign: "+" | "-" | " "
|
||||
width: `integer`
|
||||
precision: `integer` | "{" `arg_index` "}"
|
||||
type: `int_type` | "c" | "e" | "E" | "f" | "F" | "g" | "G" | "p" | "s"
|
||||
int_type: "b" | "B" | "d" | "o" | "x" | "X"
|
||||
|
||||
The *fill* character can be any character other than '{' or '}'. The presence
|
||||
of a fill character is signaled by the character following it, which must be
|
||||
one of the alignment options. If the second character of *format_spec* is not
|
||||
a valid alignment option, then it is assumed that both the fill character and
|
||||
the alignment option are absent.
|
||||
|
||||
The meaning of the various alignment options is as follows:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Option | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'<'`` | Forces the field to be left-aligned within the available |
|
||||
| | space (this is the default for most objects). |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'>'`` | Forces the field to be right-aligned within the |
|
||||
| | available space (this is the default for numbers). |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'='`` | Forces the padding to be placed after the sign (if any) |
|
||||
| | but before the digits. This is used for printing fields |
|
||||
| | in the form '+000000120'. This alignment option is only |
|
||||
| | valid for numeric types. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'^'`` | Forces the field to be centered within the available |
|
||||
| | space. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
Note that unless a minimum field width is defined, the field width will always
|
||||
be the same size as the data to fill it, so that the alignment option has no
|
||||
meaning in this case.
|
||||
|
||||
The *sign* option is only valid for number types, and can be one of the
|
||||
following:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Option | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'+'`` | indicates that a sign should be used for both |
|
||||
| | positive as well as negative numbers. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'-'`` | indicates that a sign should be used only for negative |
|
||||
| | numbers (this is the default behavior). |
|
||||
+---------+----------------------------------------------------------+
|
||||
| space | indicates that a leading space should be used on |
|
||||
| | positive numbers, and a minus sign on negative numbers. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The ``'#'`` option causes the "alternate form" to be used for the
|
||||
conversion. The alternate form is defined differently for different
|
||||
types. This option is only valid for integer and floating-point types.
|
||||
For integers, when binary, octal, or hexadecimal output is used, this
|
||||
option adds the prefix respective ``"0b"`` (``"0B"``), ``"0"``, or
|
||||
``"0x"`` (``"0X"``) to the output value. Whether the prefix is
|
||||
lower-case or upper-case is determined by the case of the type
|
||||
specifier, for example, the prefix ``"0x"`` is used for the type ``'x'``
|
||||
and ``"0X"`` is used for ``'X'``. For floating-point numbers the
|
||||
alternate form causes the result of the conversion to always contain a
|
||||
decimal-point character, even if no digits follow it. Normally, a
|
||||
decimal-point character appears in the result of these conversions
|
||||
only if a digit follows it. In addition, for ``'g'`` and ``'G'``
|
||||
conversions, trailing zeros are not removed from the result.
|
||||
|
||||
.. ifconfig:: False
|
||||
|
||||
The ``','`` option signals the use of a comma for a thousands separator.
|
||||
For a locale aware separator, use the ``'n'`` integer presentation type
|
||||
instead.
|
||||
|
||||
*width* is a decimal integer defining the minimum field width. If not
|
||||
specified, then the field width will be determined by the content.
|
||||
|
||||
Preceding the *width* field by a zero (``'0'``) character enables
|
||||
sign-aware zero-padding for numeric types. This is equivalent to a *fill*
|
||||
character of ``'0'`` with an *alignment* type of ``'='``.
|
||||
|
||||
The *precision* is a decimal number indicating how many digits should be
|
||||
displayed after the decimal point for a floating-point value formatted with
|
||||
``'f'`` and ``'F'``, or before and after the decimal point for a floating-point
|
||||
value formatted with ``'g'`` or ``'G'``. For non-number types the field
|
||||
indicates the maximum field size - in other words, how many characters will be
|
||||
used from the field content. The *precision* is not allowed for integer values
|
||||
or pointers.
|
||||
|
||||
Finally, the *type* determines how the data should be presented.
|
||||
|
||||
The available string presentation types are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'s'`` | String format. This is the default type for strings and |
|
||||
| | may be omitted. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'s'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The available character presentation types are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'c'`` | Character format. This is the default type for |
|
||||
| | characters and may be omitted. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'c'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The available integer presentation types are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'b'`` | Binary format. Outputs the number in base 2. Using the |
|
||||
| | ``'#'`` option with this type adds the prefix ``"0b"`` |
|
||||
| | to the output value. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'B'`` | Binary format. Outputs the number in base 2. Using the |
|
||||
| | ``'#'`` option with this type adds the prefix ``"0B"`` |
|
||||
| | to the output value. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'d'`` | Decimal integer. Outputs the number in base 10. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'o'`` | Octal format. Outputs the number in base 8. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'x'`` | Hex format. Outputs the number in base 16, using |
|
||||
| | lower-case letters for the digits above 9. Using the |
|
||||
| | ``'#'`` option with this type adds the prefix ``"0x"`` |
|
||||
| | to the output value. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'X'`` | Hex format. Outputs the number in base 16, using |
|
||||
| | upper-case letters for the digits above 9. Using the |
|
||||
| | ``'#'`` option with this type adds the prefix ``"0X"`` |
|
||||
| | to the output value. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'d'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The available presentation types for floating-point values are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'a'`` | Hexadecimal floating point format. Prints the number in |
|
||||
| | base 16 with prefix ``"0x"`` and lower-case letters for |
|
||||
| | digits above 9. Uses 'p' to indicate the exponent. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'A'`` | Same as ``'a'`` except it uses upper-case letters for |
|
||||
| | the prefix, digits above 9 and to indicate the exponent. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'e'`` | Exponent notation. Prints the number in scientific |
|
||||
| | notation using the letter 'e' to indicate the exponent. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an |
|
||||
| | upper-case 'E' as the separator character. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'f'`` | Fixed point. Displays the number as a fixed-point |
|
||||
| | number. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'F'`` | Fixed point. Same as ``'f'``, but converts ``nan`` to |
|
||||
| | ``NAN`` and ``inf`` to ``INF``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'g'`` | General format. For a given precision ``p >= 1``, |
|
||||
| | this rounds the number to ``p`` significant digits and |
|
||||
| | then formats the result in either fixed-point format |
|
||||
| | or in scientific notation, depending on its magnitude. |
|
||||
| | |
|
||||
| | A precision of ``0`` is treated as equivalent to a |
|
||||
| | precision of ``1``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| ``'G'`` | General format. Same as ``'g'`` except switches to |
|
||||
| | ``'E'`` if the number gets too large. The |
|
||||
| | representations of infinity and NaN are uppercased, too. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'g'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
.. ifconfig:: False
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| | The precise rules are as follows: suppose that the |
|
||||
| | result formatted with presentation type ``'e'`` and |
|
||||
| | precision ``p-1`` would have exponent ``exp``. Then |
|
||||
| | if ``-4 <= exp < p``, the number is formatted |
|
||||
| | with presentation type ``'f'`` and precision |
|
||||
| | ``p-1-exp``. Otherwise, the number is formatted |
|
||||
| | with presentation type ``'e'`` and precision ``p-1``. |
|
||||
| | In both cases insignificant trailing zeros are removed |
|
||||
| | from the significand, and the decimal point is also |
|
||||
| | removed if there are no remaining digits following it. |
|
||||
| | |
|
||||
| | Positive and negative infinity, positive and negative |
|
||||
| | zero, and nans, are formatted as ``inf``, ``-inf``, |
|
||||
| | ``0``, ``-0`` and ``nan`` respectively, regardless of |
|
||||
| | the precision. |
|
||||
| | |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
The available presentation types for pointers are:
|
||||
|
||||
+---------+----------------------------------------------------------+
|
||||
| Type | Meaning |
|
||||
+=========+==========================================================+
|
||||
| ``'p'`` | Pointer format. This is the default type for |
|
||||
| | pointers and may be omitted. |
|
||||
+---------+----------------------------------------------------------+
|
||||
| none | The same as ``'p'``. |
|
||||
+---------+----------------------------------------------------------+
|
||||
|
||||
.. _formatexamples:
|
||||
|
||||
Format examples
|
||||
===============
|
||||
|
||||
This section contains examples of the format syntax and comparison with
|
||||
the printf formatting.
|
||||
|
||||
In most of the cases the syntax is similar to the printf formatting, with the
|
||||
addition of the ``{}`` and with ``:`` used instead of ``%``.
|
||||
For example, ``"%03.2f"`` can be translated to ``"{:03.2f}"``.
|
||||
|
||||
The new format syntax also supports new and different options, shown in the
|
||||
following examples.
|
||||
|
||||
Accessing arguments by position::
|
||||
|
||||
format("{0}, {1}, {2}", 'a', 'b', 'c');
|
||||
// Result: "a, b, c"
|
||||
format("{}, {}, {}", 'a', 'b', 'c');
|
||||
// Result: "a, b, c"
|
||||
format("{2}, {1}, {0}", 'a', 'b', 'c');
|
||||
// Result: "c, b, a"
|
||||
format("{0}{1}{0}", "abra", "cad"); // arguments' indices can be repeated
|
||||
// Result: "abracadabra"
|
||||
|
||||
Aligning the text and specifying a width::
|
||||
|
||||
format("{:<30}", "left aligned");
|
||||
// Result: "left aligned "
|
||||
format("{:>30}", "right aligned");
|
||||
// Result: " right aligned"
|
||||
format("{:^30}", "centered");
|
||||
// Result: " centered "
|
||||
format("{:*^30}", "centered"); // use '*' as a fill char
|
||||
// Result: "***********centered***********"
|
||||
|
||||
Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign::
|
||||
|
||||
format("{:+f}; {:+f}", 3.14, -3.14); // show it always
|
||||
// Result: "+3.140000; -3.140000"
|
||||
format("{: f}; {: f}", 3.14, -3.14); // show a space for positive numbers
|
||||
// Result: " 3.140000; -3.140000"
|
||||
format("{:-f}; {:-f}", 3.14, -3.14); // show only the minus -- same as '{:f}; {:f}'
|
||||
// Result: "3.140000; -3.140000"
|
||||
|
||||
Replacing ``%x`` and ``%o`` and converting the value to different bases::
|
||||
|
||||
format("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
|
||||
// Result: "int: 42; hex: 2a; oct: 52; bin: 101010"
|
||||
// with 0x or 0 or 0b as prefix:
|
||||
format("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}", 42);
|
||||
// Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010"
|
||||
|
||||
.. ifconfig:: False
|
||||
|
||||
Using the comma as a thousands separator::
|
||||
|
||||
format("{:,}", 1234567890);
|
||||
'1,234,567,890'
|
||||
|
||||
Expressing a percentage::
|
||||
|
||||
>>> points = 19
|
||||
>>> total = 22
|
||||
Format("Correct answers: {:.2%}") << points/total)
|
||||
'Correct answers: 86.36%'
|
||||
|
||||
Using type-specific formatting::
|
||||
|
||||
>>> import datetime
|
||||
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
|
||||
Format("{:%Y-%m-%d %H:%M:%S}") << d)
|
||||
'2010-07-04 12:15:58'
|
||||
|
||||
Nesting arguments and more complex examples::
|
||||
|
||||
>>> for align, text in zip('<^>', ['left', 'center', 'right']):
|
||||
... '{0:{fill}{align}16}") << text, fill=align, align=align)
|
||||
...
|
||||
'left<<<<<<<<<<<<'
|
||||
'^^^^^center^^^^^'
|
||||
'>>>>>>>>>>>right'
|
||||
>>>
|
||||
>>> octets = [192, 168, 0, 1]
|
||||
Format("{:02X}{:02X}{:02X}{:02X}") << *octets)
|
||||
'C0A80001'
|
||||
>>> int(_, 16)
|
||||
3232235521
|
||||
>>>
|
||||
>>> width = 5
|
||||
>>> for num in range(5,12):
|
||||
... for base in 'dXob':
|
||||
... print('{0:{width}{base}}") << num, base=base, width=width), end=' ')
|
||||
... print()
|
||||
...
|
||||
5 5 5 101
|
||||
6 6 6 110
|
||||
7 7 7 111
|
||||
8 8 10 1000
|
||||
9 9 11 1001
|
||||
10 A 12 1010
|
||||
11 B 13 1011
|
||||
|
||||
66
doc/usage.rst
Normal file
66
doc/usage.rst
Normal file
@@ -0,0 +1,66 @@
|
||||
*****
|
||||
Usage
|
||||
*****
|
||||
|
||||
To use the C++ Format library, add :file:`format.h` and :file:`format.cc` from
|
||||
a `release archive <https://github.com/cppformat/cppformat/releases/latest>`_
|
||||
or the `Git repository <https://github.com/cppformat/cppformat>`_ to your project.
|
||||
Alternatively, you can :ref:`build the library with CMake <building>`.
|
||||
|
||||
If you are using Visual C++ with precompiled headers, you might need to add
|
||||
the line ::
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
before other includes in :file:`format.cc`.
|
||||
|
||||
.. _building:
|
||||
|
||||
Building the library
|
||||
====================
|
||||
|
||||
The included `CMake build script`__ can be used to build the C++ Format
|
||||
library on a wide range of platforms. CMake is freely available for
|
||||
download from http://www.cmake.org/download/.
|
||||
|
||||
__ https://github.com/cppformat/cppformat/blob/master/CMakeLists.txt
|
||||
|
||||
CMake works by generating native makefiles or project files that can
|
||||
be used in the compiler environment of your choice. The typical
|
||||
workflow starts with::
|
||||
|
||||
mkdir build # Create a directory to hold the build output.
|
||||
cd build
|
||||
cmake <path/to/cppformat> # Generate native build scripts.
|
||||
|
||||
where :file:`{<path/to/cppformat>}` is a path to the ``cppformat`` repository.
|
||||
|
||||
If you are on a \*nix system, you should now see a Makefile in the
|
||||
current directory. Now you can build C++ Format by running :command:`make`.
|
||||
|
||||
Once the library has been built you can invoke :command:`make test` to run
|
||||
the tests.
|
||||
|
||||
If you use Windows and have Vistual Studio installed, a :file:`FORMAT.sln`
|
||||
file and several :file:`.vcproj` files will be created. You can then build them
|
||||
using Visual Studio or msbuild.
|
||||
|
||||
On Mac OS X with Xcode installed, an :file:`.xcodeproj` file will be generated.
|
||||
|
||||
To build a `shared library`__ set the ``BUILD_SHARED_LIBS`` CMake variable to
|
||||
``TRUE``::
|
||||
|
||||
cmake -DBUILD_SHARED_LIBS=TRUE ...
|
||||
|
||||
__ http://en.wikipedia.org/wiki/Library_%28computing%29#Shared_libraries
|
||||
|
||||
Android NDK
|
||||
===========
|
||||
|
||||
C++ Format provides `Android.mk file`__ that can be used to build the library
|
||||
with `Android NDK <https://developer.android.com/tools/sdk/ndk/index.html>`_.
|
||||
For an example of using C++ Format with Android NDK, see the
|
||||
`android-ndk-example <https://github.com/cppformat/android-ndk-example>`_
|
||||
repository.
|
||||
|
||||
__ https://github.com/cppformat/cppformat/blob/master/Android.mk
|
||||
Submodule format-benchmark deleted from 1b2df8bc3d
1514
format-test.cc
1514
format-test.cc
File diff suppressed because it is too large
Load Diff
11443
gmock/gmock-gtest-all.cc
Normal file
11443
gmock/gmock-gtest-all.cc
Normal file
File diff suppressed because it is too large
Load Diff
14198
gmock/gmock/gmock.h
Normal file
14198
gmock/gmock/gmock.h
Normal file
File diff suppressed because it is too large
Load Diff
232
gmock/gtest/gtest-spi.h
Normal file
232
gmock/gtest/gtest-spi.h
Normal file
@@ -0,0 +1,232 @@
|
||||
// Copyright 2007, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Author: wan@google.com (Zhanyong Wan)
|
||||
//
|
||||
// Utilities for testing Google Test itself and code that uses Google Test
|
||||
// (e.g. frameworks built on top of Google Test).
|
||||
|
||||
#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_
|
||||
#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace testing {
|
||||
|
||||
// This helper class can be used to mock out Google Test failure reporting
|
||||
// so that we can test Google Test or code that builds on Google Test.
|
||||
//
|
||||
// An object of this class appends a TestPartResult object to the
|
||||
// TestPartResultArray object given in the constructor whenever a Google Test
|
||||
// failure is reported. It can either intercept only failures that are
|
||||
// generated in the same thread that created this object or it can intercept
|
||||
// all generated failures. The scope of this mock object can be controlled with
|
||||
// the second argument to the two arguments constructor.
|
||||
class GTEST_API_ ScopedFakeTestPartResultReporter
|
||||
: public TestPartResultReporterInterface {
|
||||
public:
|
||||
// The two possible mocking modes of this object.
|
||||
enum InterceptMode {
|
||||
INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures.
|
||||
INTERCEPT_ALL_THREADS // Intercepts all failures.
|
||||
};
|
||||
|
||||
// The c'tor sets this object as the test part result reporter used
|
||||
// by Google Test. The 'result' parameter specifies where to report the
|
||||
// results. This reporter will only catch failures generated in the current
|
||||
// thread. DEPRECATED
|
||||
explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
|
||||
|
||||
// Same as above, but you can choose the interception scope of this object.
|
||||
ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
|
||||
TestPartResultArray* result);
|
||||
|
||||
// The d'tor restores the previous test part result reporter.
|
||||
virtual ~ScopedFakeTestPartResultReporter();
|
||||
|
||||
// Appends the TestPartResult object to the TestPartResultArray
|
||||
// received in the constructor.
|
||||
//
|
||||
// This method is from the TestPartResultReporterInterface
|
||||
// interface.
|
||||
virtual void ReportTestPartResult(const TestPartResult& result);
|
||||
private:
|
||||
void Init();
|
||||
|
||||
const InterceptMode intercept_mode_;
|
||||
TestPartResultReporterInterface* old_reporter_;
|
||||
TestPartResultArray* const result_;
|
||||
|
||||
GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
|
||||
// A helper class for implementing EXPECT_FATAL_FAILURE() and
|
||||
// EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given
|
||||
// TestPartResultArray contains exactly one failure that has the given
|
||||
// type and contains the given substring. If that's not the case, a
|
||||
// non-fatal failure will be generated.
|
||||
class GTEST_API_ SingleFailureChecker {
|
||||
public:
|
||||
// The constructor remembers the arguments.
|
||||
SingleFailureChecker(const TestPartResultArray* results,
|
||||
TestPartResult::Type type,
|
||||
const string& substr);
|
||||
~SingleFailureChecker();
|
||||
private:
|
||||
const TestPartResultArray* const results_;
|
||||
const TestPartResult::Type type_;
|
||||
const string substr_;
|
||||
|
||||
GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
} // namespace testing
|
||||
|
||||
// A set of macros for testing Google Test assertions or code that's expected
|
||||
// to generate Google Test fatal failures. It verifies that the given
|
||||
// statement will cause exactly one fatal Google Test failure with 'substr'
|
||||
// being part of the failure message.
|
||||
//
|
||||
// There are two different versions of this macro. EXPECT_FATAL_FAILURE only
|
||||
// affects and considers failures generated in the current thread and
|
||||
// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
|
||||
//
|
||||
// The verification of the assertion is done correctly even when the statement
|
||||
// throws an exception or aborts the current function.
|
||||
//
|
||||
// Known restrictions:
|
||||
// - 'statement' cannot reference local non-static variables or
|
||||
// non-static members of the current object.
|
||||
// - 'statement' cannot return a value.
|
||||
// - You cannot stream a failure message to this macro.
|
||||
//
|
||||
// Note that even though the implementations of the following two
|
||||
// macros are much alike, we cannot refactor them to use a common
|
||||
// helper macro, due to some peculiarity in how the preprocessor
|
||||
// works. The AcceptsMacroThatExpandsToUnprotectedComma test in
|
||||
// gtest_unittest.cc will fail to compile if we do that.
|
||||
#define EXPECT_FATAL_FAILURE(statement, substr) \
|
||||
do { \
|
||||
class GTestExpectFatalFailureHelper {\
|
||||
public:\
|
||||
static void Execute() { statement; }\
|
||||
};\
|
||||
::testing::TestPartResultArray gtest_failures;\
|
||||
::testing::internal::SingleFailureChecker gtest_checker(\
|
||||
>est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
|
||||
{\
|
||||
::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
|
||||
::testing::ScopedFakeTestPartResultReporter:: \
|
||||
INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\
|
||||
GTestExpectFatalFailureHelper::Execute();\
|
||||
}\
|
||||
} while (::testing::internal::AlwaysFalse())
|
||||
|
||||
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
|
||||
do { \
|
||||
class GTestExpectFatalFailureHelper {\
|
||||
public:\
|
||||
static void Execute() { statement; }\
|
||||
};\
|
||||
::testing::TestPartResultArray gtest_failures;\
|
||||
::testing::internal::SingleFailureChecker gtest_checker(\
|
||||
>est_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
|
||||
{\
|
||||
::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
|
||||
::testing::ScopedFakeTestPartResultReporter:: \
|
||||
INTERCEPT_ALL_THREADS, >est_failures);\
|
||||
GTestExpectFatalFailureHelper::Execute();\
|
||||
}\
|
||||
} while (::testing::internal::AlwaysFalse())
|
||||
|
||||
// A macro for testing Google Test assertions or code that's expected to
|
||||
// generate Google Test non-fatal failures. It asserts that the given
|
||||
// statement will cause exactly one non-fatal Google Test failure with 'substr'
|
||||
// being part of the failure message.
|
||||
//
|
||||
// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
|
||||
// affects and considers failures generated in the current thread and
|
||||
// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
|
||||
//
|
||||
// 'statement' is allowed to reference local variables and members of
|
||||
// the current object.
|
||||
//
|
||||
// The verification of the assertion is done correctly even when the statement
|
||||
// throws an exception or aborts the current function.
|
||||
//
|
||||
// Known restrictions:
|
||||
// - You cannot stream a failure message to this macro.
|
||||
//
|
||||
// Note that even though the implementations of the following two
|
||||
// macros are much alike, we cannot refactor them to use a common
|
||||
// helper macro, due to some peculiarity in how the preprocessor
|
||||
// works. If we do that, the code won't compile when the user gives
|
||||
// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
|
||||
// expands to code containing an unprotected comma. The
|
||||
// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
|
||||
// catches that.
|
||||
//
|
||||
// For the same reason, we have to write
|
||||
// if (::testing::internal::AlwaysTrue()) { statement; }
|
||||
// instead of
|
||||
// GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
|
||||
// to avoid an MSVC warning on unreachable code.
|
||||
#define EXPECT_NONFATAL_FAILURE(statement, substr) \
|
||||
do {\
|
||||
::testing::TestPartResultArray gtest_failures;\
|
||||
::testing::internal::SingleFailureChecker gtest_checker(\
|
||||
>est_failures, ::testing::TestPartResult::kNonFatalFailure, \
|
||||
(substr));\
|
||||
{\
|
||||
::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
|
||||
::testing::ScopedFakeTestPartResultReporter:: \
|
||||
INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);\
|
||||
if (::testing::internal::AlwaysTrue()) { statement; }\
|
||||
}\
|
||||
} while (::testing::internal::AlwaysFalse())
|
||||
|
||||
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
|
||||
do {\
|
||||
::testing::TestPartResultArray gtest_failures;\
|
||||
::testing::internal::SingleFailureChecker gtest_checker(\
|
||||
>est_failures, ::testing::TestPartResult::kNonFatalFailure, \
|
||||
(substr));\
|
||||
{\
|
||||
::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
|
||||
::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
|
||||
>est_failures);\
|
||||
if (::testing::internal::AlwaysTrue()) { statement; }\
|
||||
}\
|
||||
} while (::testing::internal::AlwaysFalse())
|
||||
|
||||
#endif // GTEST_INCLUDE_GTEST_GTEST_SPI_H_
|
||||
20061
gmock/gtest/gtest.h
Normal file
20061
gmock/gtest/gtest.h
Normal file
File diff suppressed because it is too large
Load Diff
1
gtest
1
gtest
Submodule gtest deleted from 9f357d549d
242
posix.cc
Normal file
242
posix.cc
Normal file
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
A C++ interface to POSIX functions.
|
||||
|
||||
Copyright (c) 2014 - 2015, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// Disable bogus MSVC warnings.
|
||||
#ifndef _CRT_SECURE_NO_WARNINGS
|
||||
# define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
#include "posix.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
# include <unistd.h>
|
||||
#else
|
||||
# include <windows.h>
|
||||
# include <io.h>
|
||||
|
||||
# define O_CREAT _O_CREAT
|
||||
# define O_TRUNC _O_TRUNC
|
||||
|
||||
#ifndef S_IRUSR
|
||||
# define S_IRUSR _S_IREAD
|
||||
#endif
|
||||
|
||||
#ifndef S_IWUSR
|
||||
# define S_IWUSR _S_IWRITE
|
||||
#endif
|
||||
|
||||
# ifdef __MINGW32__
|
||||
# define _SH_DENYNO 0x40
|
||||
# endif
|
||||
|
||||
#endif // _WIN32
|
||||
|
||||
namespace {
|
||||
#ifdef _WIN32
|
||||
// Return type of read and write functions.
|
||||
typedef int RWResult;
|
||||
|
||||
// On Windows the count argument to read and write is unsigned, so convert
|
||||
// it from size_t preventing integer overflow.
|
||||
inline unsigned convert_rwcount(std::size_t count) {
|
||||
return count <= UINT_MAX ? static_cast<unsigned>(count) : UINT_MAX;
|
||||
}
|
||||
#else
|
||||
// Return type of read and write functions.
|
||||
typedef ssize_t RWResult;
|
||||
|
||||
inline std::size_t convert_rwcount(std::size_t count) { return count; }
|
||||
#endif
|
||||
}
|
||||
|
||||
fmt::BufferedFile::~BufferedFile() FMT_NOEXCEPT {
|
||||
if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
|
||||
fmt::report_system_error(errno, "cannot close file");
|
||||
}
|
||||
|
||||
fmt::BufferedFile::BufferedFile(fmt::StringRef filename, fmt::StringRef mode) {
|
||||
FMT_RETRY_VAL(file_, FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())), 0);
|
||||
if (!file_)
|
||||
throw SystemError(errno, "cannot open file {}", filename);
|
||||
}
|
||||
|
||||
void fmt::BufferedFile::close() {
|
||||
if (!file_)
|
||||
return;
|
||||
int result = FMT_SYSTEM(fclose(file_));
|
||||
file_ = 0;
|
||||
if (result != 0)
|
||||
throw SystemError(errno, "cannot close file");
|
||||
}
|
||||
|
||||
int fmt::BufferedFile::fileno() const {
|
||||
int fd = FMT_POSIX_CALL(fileno(file_));
|
||||
if (fd == -1)
|
||||
throw SystemError(errno, "cannot get file descriptor");
|
||||
return fd;
|
||||
}
|
||||
|
||||
fmt::File::File(fmt::StringRef path, int oflag) {
|
||||
int mode = S_IRUSR | S_IWUSR;
|
||||
#ifdef _WIN32
|
||||
fd_ = -1;
|
||||
FMT_POSIX_CALL(sopen_s(&fd_, path.c_str(), oflag, _SH_DENYNO, mode));
|
||||
#else
|
||||
FMT_RETRY(fd_, FMT_POSIX_CALL(open(path.c_str(), oflag, mode)));
|
||||
#endif
|
||||
if (fd_ == -1)
|
||||
throw SystemError(errno, "cannot open file {}", path);
|
||||
}
|
||||
|
||||
fmt::File::~File() FMT_NOEXCEPT {
|
||||
// Don't retry close in case of EINTR!
|
||||
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
|
||||
if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0)
|
||||
fmt::report_system_error(errno, "cannot close file");
|
||||
}
|
||||
|
||||
void fmt::File::close() {
|
||||
if (fd_ == -1)
|
||||
return;
|
||||
// Don't retry close in case of EINTR!
|
||||
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
|
||||
int result = FMT_POSIX_CALL(close(fd_));
|
||||
fd_ = -1;
|
||||
if (result != 0)
|
||||
throw SystemError(errno, "cannot close file");
|
||||
}
|
||||
|
||||
fmt::LongLong fmt::File::size() const {
|
||||
#ifdef _WIN32
|
||||
LARGE_INTEGER filesize = {};
|
||||
HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd_));
|
||||
if (!FMT_SYSTEM(GetFileSizeEx(handle, &filesize)))
|
||||
throw WindowsError(GetLastError(), "cannot get file size");
|
||||
FMT_STATIC_ASSERT(sizeof(fmt::LongLong) >= sizeof(filesize.QuadPart),
|
||||
"return type of File::size is not large enough");
|
||||
return filesize.QuadPart;
|
||||
#else
|
||||
typedef struct stat Stat;
|
||||
Stat file_stat = Stat();
|
||||
if (FMT_POSIX_CALL(fstat(fd_, &file_stat)) == -1)
|
||||
throw SystemError(errno, "cannot get file attributes");
|
||||
FMT_STATIC_ASSERT(sizeof(fmt::LongLong) >= sizeof(file_stat.st_size),
|
||||
"return type of File::size is not large enough");
|
||||
return file_stat.st_size;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::size_t fmt::File::read(void *buffer, std::size_t count) {
|
||||
RWResult result = 0;
|
||||
FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count))));
|
||||
if (result < 0)
|
||||
throw SystemError(errno, "cannot read from file");
|
||||
return result;
|
||||
}
|
||||
|
||||
std::size_t fmt::File::write(const void *buffer, std::size_t count) {
|
||||
RWResult result = 0;
|
||||
FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count))));
|
||||
if (result < 0)
|
||||
throw SystemError(errno, "cannot write to file");
|
||||
return result;
|
||||
}
|
||||
|
||||
fmt::File fmt::File::dup(int fd) {
|
||||
// Don't retry as dup doesn't return EINTR.
|
||||
// http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html
|
||||
int new_fd = FMT_POSIX_CALL(dup(fd));
|
||||
if (new_fd == -1)
|
||||
throw SystemError(errno, "cannot duplicate file descriptor {}", fd);
|
||||
return File(new_fd);
|
||||
}
|
||||
|
||||
void fmt::File::dup2(int fd) {
|
||||
int result = 0;
|
||||
FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd)));
|
||||
if (result == -1) {
|
||||
throw SystemError(errno,
|
||||
"cannot duplicate file descriptor {} to {}", fd_, fd);
|
||||
}
|
||||
}
|
||||
|
||||
void fmt::File::dup2(int fd, ErrorCode &ec) FMT_NOEXCEPT {
|
||||
int result = 0;
|
||||
FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd)));
|
||||
if (result == -1)
|
||||
ec = ErrorCode(errno);
|
||||
}
|
||||
|
||||
void fmt::File::pipe(File &read_end, File &write_end) {
|
||||
// Close the descriptors first to make sure that assignments don't throw
|
||||
// and there are no leaks.
|
||||
read_end.close();
|
||||
write_end.close();
|
||||
int fds[2] = {};
|
||||
#ifdef _WIN32
|
||||
// Make the default pipe capacity same as on Linux 2.6.11+.
|
||||
enum { DEFAULT_CAPACITY = 65536 };
|
||||
int result = FMT_POSIX_CALL(pipe(fds, DEFAULT_CAPACITY, _O_BINARY));
|
||||
#else
|
||||
// Don't retry as the pipe function doesn't return EINTR.
|
||||
// http://pubs.opengroup.org/onlinepubs/009696799/functions/pipe.html
|
||||
int result = FMT_POSIX_CALL(pipe(fds));
|
||||
#endif
|
||||
if (result != 0)
|
||||
throw SystemError(errno, "cannot create pipe");
|
||||
// The following assignments don't throw because read_fd and write_fd
|
||||
// are closed.
|
||||
read_end = File(fds[0]);
|
||||
write_end = File(fds[1]);
|
||||
}
|
||||
|
||||
fmt::BufferedFile fmt::File::fdopen(const char *mode) {
|
||||
// Don't retry as fdopen doesn't return EINTR.
|
||||
FILE *f = FMT_POSIX_CALL(fdopen(fd_, mode));
|
||||
if (!f)
|
||||
throw SystemError(errno, "cannot associate stream with file descriptor");
|
||||
BufferedFile file(f);
|
||||
fd_ = -1;
|
||||
return file;
|
||||
}
|
||||
|
||||
long fmt::getpagesize() {
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO si;
|
||||
GetSystemInfo(&si);
|
||||
return si.dwPageSize;
|
||||
#else
|
||||
long size = FMT_POSIX_CALL(sysconf(_SC_PAGESIZE));
|
||||
if (size < 0)
|
||||
throw SystemError(errno, "cannot get memory page size");
|
||||
return size;
|
||||
#endif
|
||||
}
|
||||
337
posix.h
Normal file
337
posix.h
Normal file
@@ -0,0 +1,337 @@
|
||||
/*
|
||||
A C++ interface to POSIX functions.
|
||||
|
||||
Copyright (c) 2014 - 2015, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef FMT_POSIX_H_
|
||||
#define FMT_POSIX_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h> // for O_RDONLY
|
||||
#include <stdio.h>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "format.h"
|
||||
|
||||
#ifdef FMT_INCLUDE_POSIX_TEST
|
||||
# include "test/posix-test.h"
|
||||
#endif
|
||||
|
||||
#ifndef FMT_POSIX
|
||||
# ifdef _WIN32
|
||||
// Fix warnings about deprecated symbols.
|
||||
# define FMT_POSIX(call) _##call
|
||||
# else
|
||||
# define FMT_POSIX(call) call
|
||||
# endif
|
||||
#endif
|
||||
|
||||
// Calls to system functions are wrapped in FMT_SYSTEM for testability.
|
||||
#ifdef FMT_SYSTEM
|
||||
# define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
|
||||
#else
|
||||
# define FMT_SYSTEM(call) call
|
||||
# ifdef _WIN32
|
||||
// Fix warnings about deprecated symbols.
|
||||
# define FMT_POSIX_CALL(call) ::_##call
|
||||
# else
|
||||
# define FMT_POSIX_CALL(call) ::call
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if FMT_GCC_VERSION >= 407
|
||||
# define FMT_UNUSED __attribute__((unused))
|
||||
#else
|
||||
# define FMT_UNUSED
|
||||
#endif
|
||||
|
||||
#if FMT_USE_STATIC_ASSERT || FMT_HAS_CPP_ATTRIBUTE(cxx_static_assert) || \
|
||||
(FMT_GCC_VERSION >= 403 && FMT_HAS_GXX_CXX11) || _MSC_VER >= 1600
|
||||
# define FMT_STATIC_ASSERT(cond, message) static_assert(cond, message)
|
||||
#else
|
||||
# define FMT_CONCAT_(a, b) FMT_CONCAT(a, b)
|
||||
# define FMT_STATIC_ASSERT(cond, message) \
|
||||
typedef int FMT_CONCAT_(Assert, __LINE__)[(cond) ? 1 : -1] FMT_UNUSED
|
||||
#endif
|
||||
|
||||
// Retries the expression while it evaluates to error_result and errno
|
||||
// equals to EINTR.
|
||||
#ifndef _WIN32
|
||||
# define FMT_RETRY_VAL(result, expression, error_result) \
|
||||
do { \
|
||||
result = (expression); \
|
||||
} while (result == error_result && errno == EINTR)
|
||||
#else
|
||||
# define FMT_RETRY_VAL(result, expression, error_result) result = (expression)
|
||||
#endif
|
||||
|
||||
#define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1)
|
||||
|
||||
namespace fmt {
|
||||
|
||||
// An error code.
|
||||
class ErrorCode {
|
||||
private:
|
||||
int value_;
|
||||
|
||||
public:
|
||||
explicit ErrorCode(int value = 0) FMT_NOEXCEPT : value_(value) {}
|
||||
|
||||
int get() const FMT_NOEXCEPT { return value_; }
|
||||
};
|
||||
|
||||
// A buffered file.
|
||||
class BufferedFile {
|
||||
private:
|
||||
FILE *file_;
|
||||
|
||||
friend class File;
|
||||
|
||||
explicit BufferedFile(FILE *f) : file_(f) {}
|
||||
|
||||
public:
|
||||
// Constructs a BufferedFile object which doesn't represent any file.
|
||||
BufferedFile() FMT_NOEXCEPT : file_(0) {}
|
||||
|
||||
// Destroys the object closing the file it represents if any.
|
||||
~BufferedFile() FMT_NOEXCEPT;
|
||||
|
||||
#if !FMT_USE_RVALUE_REFERENCES
|
||||
// Emulate a move constructor and a move assignment operator if rvalue
|
||||
// references are not supported.
|
||||
|
||||
private:
|
||||
// A proxy object to emulate a move constructor.
|
||||
// It is private to make it impossible call operator Proxy directly.
|
||||
struct Proxy {
|
||||
FILE *file;
|
||||
};
|
||||
|
||||
public:
|
||||
// A "move constructor" for moving from a temporary.
|
||||
BufferedFile(Proxy p) FMT_NOEXCEPT : file_(p.file) {}
|
||||
|
||||
// A "move constructor" for for moving from an lvalue.
|
||||
BufferedFile(BufferedFile &f) FMT_NOEXCEPT : file_(f.file_) {
|
||||
f.file_ = 0;
|
||||
}
|
||||
|
||||
// A "move assignment operator" for moving from a temporary.
|
||||
BufferedFile &operator=(Proxy p) {
|
||||
close();
|
||||
file_ = p.file;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// A "move assignment operator" for moving from an lvalue.
|
||||
BufferedFile &operator=(BufferedFile &other) {
|
||||
close();
|
||||
file_ = other.file_;
|
||||
other.file_ = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Returns a proxy object for moving from a temporary:
|
||||
// BufferedFile file = BufferedFile(...);
|
||||
operator Proxy() FMT_NOEXCEPT {
|
||||
Proxy p = {file_};
|
||||
file_ = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
#else
|
||||
private:
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(BufferedFile);
|
||||
|
||||
public:
|
||||
BufferedFile(BufferedFile &&other) FMT_NOEXCEPT : file_(other.file_) {
|
||||
other.file_ = 0;
|
||||
}
|
||||
|
||||
BufferedFile& operator=(BufferedFile &&other) {
|
||||
close();
|
||||
file_ = other.file_;
|
||||
other.file_ = 0;
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Opens a file.
|
||||
BufferedFile(fmt::StringRef filename, fmt::StringRef mode);
|
||||
|
||||
// Closes the file.
|
||||
void close();
|
||||
|
||||
// Returns the pointer to a FILE object representing this file.
|
||||
FILE *get() const FMT_NOEXCEPT { return file_; }
|
||||
|
||||
int fileno() const;
|
||||
|
||||
void print(fmt::StringRef format_str, const ArgList &args) {
|
||||
fmt::print(file_, format_str, args);
|
||||
}
|
||||
FMT_VARIADIC(void, print, fmt::StringRef)
|
||||
};
|
||||
|
||||
// A file. Closed file is represented by a File object with descriptor -1.
|
||||
// Methods that are not declared with FMT_NOEXCEPT may throw
|
||||
// fmt::SystemError in case of failure. Note that some errors such as
|
||||
// closing the file multiple times will cause a crash on Windows rather
|
||||
// than an exception. You can get standard behavior by overriding the
|
||||
// invalid parameter handler with _set_invalid_parameter_handler.
|
||||
class File {
|
||||
private:
|
||||
int fd_; // File descriptor.
|
||||
|
||||
// Constructs a File object with a given descriptor.
|
||||
explicit File(int fd) : fd_(fd) {}
|
||||
|
||||
public:
|
||||
// Possible values for the oflag argument to the constructor.
|
||||
enum {
|
||||
RDONLY = FMT_POSIX(O_RDONLY), // Open for reading only.
|
||||
WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only.
|
||||
RDWR = FMT_POSIX(O_RDWR) // Open for reading and writing.
|
||||
};
|
||||
|
||||
// Constructs a File object which doesn't represent any file.
|
||||
File() FMT_NOEXCEPT : fd_(-1) {}
|
||||
|
||||
// Opens a file and constructs a File object representing this file.
|
||||
File(fmt::StringRef path, int oflag);
|
||||
|
||||
#if !FMT_USE_RVALUE_REFERENCES
|
||||
// Emulate a move constructor and a move assignment operator if rvalue
|
||||
// references are not supported.
|
||||
|
||||
private:
|
||||
// A proxy object to emulate a move constructor.
|
||||
// It is private to make it impossible call operator Proxy directly.
|
||||
struct Proxy {
|
||||
int fd;
|
||||
};
|
||||
|
||||
public:
|
||||
// A "move constructor" for moving from a temporary.
|
||||
File(Proxy p) FMT_NOEXCEPT : fd_(p.fd) {}
|
||||
|
||||
// A "move constructor" for for moving from an lvalue.
|
||||
File(File &other) FMT_NOEXCEPT : fd_(other.fd_) {
|
||||
other.fd_ = -1;
|
||||
}
|
||||
|
||||
// A "move assignment operator" for moving from a temporary.
|
||||
File &operator=(Proxy p) {
|
||||
close();
|
||||
fd_ = p.fd;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// A "move assignment operator" for moving from an lvalue.
|
||||
File &operator=(File &other) {
|
||||
close();
|
||||
fd_ = other.fd_;
|
||||
other.fd_ = -1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Returns a proxy object for moving from a temporary:
|
||||
// File file = File(...);
|
||||
operator Proxy() FMT_NOEXCEPT {
|
||||
Proxy p = {fd_};
|
||||
fd_ = -1;
|
||||
return p;
|
||||
}
|
||||
|
||||
#else
|
||||
private:
|
||||
FMT_DISALLOW_COPY_AND_ASSIGN(File);
|
||||
|
||||
public:
|
||||
File(File &&other) FMT_NOEXCEPT : fd_(other.fd_) {
|
||||
other.fd_ = -1;
|
||||
}
|
||||
|
||||
File& operator=(File &&other) {
|
||||
close();
|
||||
fd_ = other.fd_;
|
||||
other.fd_ = -1;
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Destroys the object closing the file it represents if any.
|
||||
~File() FMT_NOEXCEPT;
|
||||
|
||||
// Returns the file descriptor.
|
||||
int descriptor() const FMT_NOEXCEPT { return fd_; }
|
||||
|
||||
// Closes the file.
|
||||
void close();
|
||||
|
||||
// Returns the file size.
|
||||
fmt::LongLong size() const;
|
||||
|
||||
// Attempts to read count bytes from the file into the specified buffer.
|
||||
std::size_t read(void *buffer, std::size_t count);
|
||||
|
||||
// Attempts to write count bytes from the specified buffer to the file.
|
||||
std::size_t write(const void *buffer, std::size_t count);
|
||||
|
||||
// Duplicates a file descriptor with the dup function and returns
|
||||
// the duplicate as a file object.
|
||||
static File dup(int fd);
|
||||
|
||||
// Makes fd be the copy of this file descriptor, closing fd first if
|
||||
// necessary.
|
||||
void dup2(int fd);
|
||||
|
||||
// Makes fd be the copy of this file descriptor, closing fd first if
|
||||
// necessary.
|
||||
void dup2(int fd, ErrorCode &ec) FMT_NOEXCEPT;
|
||||
|
||||
// Creates a pipe setting up read_end and write_end file objects for reading
|
||||
// and writing respectively.
|
||||
static void pipe(File &read_end, File &write_end);
|
||||
|
||||
// Creates a BufferedFile object associated with this file and detaches
|
||||
// this File object from the file.
|
||||
BufferedFile fdopen(const char *mode);
|
||||
};
|
||||
|
||||
// Returns the memory page size.
|
||||
long getpagesize();
|
||||
} // namespace fmt
|
||||
|
||||
#if !FMT_USE_RVALUE_REFERENCES
|
||||
namespace std {
|
||||
// For compatibility with C++98.
|
||||
inline fmt::BufferedFile &move(fmt::BufferedFile &f) { return f; }
|
||||
inline fmt::File &move(fmt::File &f) { return f; }
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // FMT_POSIX_H_
|
||||
4
support/README
Normal file
4
support/README
Normal file
@@ -0,0 +1,4 @@
|
||||
This directory contains build support files such as
|
||||
|
||||
* CMake modules
|
||||
* Build scripts
|
||||
30
support/appveyor-build.py
Executable file
30
support/appveyor-build.py
Executable file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
# Build the project on AppVeyor.
|
||||
|
||||
import os
|
||||
from download import Downloader
|
||||
from subprocess import check_call
|
||||
|
||||
build = os.environ['BUILD']
|
||||
cmake_command = ['cmake', '-DFMT_EXTRA_TESTS=ON', '-DCMAKE_BUILD_TYPE=' + os.environ['CONFIG']]
|
||||
build_command = ['msbuild', '/m:4', '/p:Config=' + os.environ['CONFIG'], 'FORMAT.sln']
|
||||
test_command = ['msbuild', 'RUN_TESTS.vcxproj']
|
||||
if build == 'mingw':
|
||||
# Install MinGW.
|
||||
mingw_url = 'http://ufpr.dl.sourceforge.net/project/mingw-w64/' + \
|
||||
'Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/' + \
|
||||
'4.9.2/threads-win32/seh/x86_64-4.9.2-release-win32-seh-rt_v3-rev1.7z'
|
||||
with Downloader().download(mingw_url) as f:
|
||||
check_call(['7z', 'x', '-oC:\\', f])
|
||||
|
||||
# Remove path to Git bin directory from $PATH because it breaks MinGW config.
|
||||
path = os.environ['PATH'].replace(r'C:\Program Files (x86)\Git\bin', '')
|
||||
|
||||
os.environ['PATH'] = r'C:\Program Files (x86)\MSBUILD\12.0\bin\;' + path + r';C:\mingw64\bin'
|
||||
cmake_command.append('-GMinGW Makefiles')
|
||||
build_command = ['mingw32-make', '-j4']
|
||||
test_command = ['mingw32-make', 'test']
|
||||
|
||||
check_call(cmake_command)
|
||||
check_call(build_command)
|
||||
check_call(test_command)
|
||||
13
support/biicode-build.py
Executable file
13
support/biicode-build.py
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
# Build the project with Biicode.
|
||||
|
||||
import glob, os, shutil
|
||||
from subprocess import check_call
|
||||
|
||||
project_dir = 'biicode_project'
|
||||
check_call(['bii', 'init', project_dir])
|
||||
cppformat_dir = os.path.join(project_dir, 'blocks/vitaut/cppformat')
|
||||
shutil.copytree('.', cppformat_dir, ignore=shutil.ignore_patterns(project_dir))
|
||||
for f in glob.glob('support/biicode/*'):
|
||||
shutil.copy(f, cppformat_dir)
|
||||
check_call(['bii', 'cpp:build'], cwd=project_dir)
|
||||
19
support/biicode/biicode.conf
Normal file
19
support/biicode/biicode.conf
Normal file
@@ -0,0 +1,19 @@
|
||||
# Biicode configuration file
|
||||
|
||||
[paths]
|
||||
# Local directories to look for headers (within block)
|
||||
/
|
||||
|
||||
[dependencies]
|
||||
# Manual adjust file implicit dependencies, add (+), remove (-), or overwrite (=)
|
||||
CMakeLists.txt + cmake/FindSetEnv.cmake
|
||||
format.h = format.cc
|
||||
format.cc - test/* posix.cc
|
||||
support/biicode/sample.cc - test/*
|
||||
|
||||
[mains]
|
||||
# Manual adjust of files that define an executable
|
||||
!test/test-main.cc
|
||||
|
||||
[parent]
|
||||
vitaut/cppformat: 0
|
||||
3
support/biicode/ignore.bii
Normal file
3
support/biicode/ignore.bii
Normal file
@@ -0,0 +1,3 @@
|
||||
doc/*
|
||||
breathe/*
|
||||
gmock/*
|
||||
17
support/biicode/sample.cc
Normal file
17
support/biicode/sample.cc
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "vitaut/cppformat/format.h"
|
||||
|
||||
class Date {
|
||||
int year_, month_, day_;
|
||||
public:
|
||||
Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Date &d) {
|
||||
return os << d.year_ << '-' << d.month_ << '-' << d.day_;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::string s = fmt::format("The date is {}", Date(2012, 12, 9));
|
||||
fmt::print("Hello, {}!", "world"); // uses Python-like format string syntax
|
||||
fmt::printf("\n%s", s); // uses printf format string syntax
|
||||
}
|
||||
7
support/cmake/FindSetEnv.cmake
Normal file
7
support/cmake/FindSetEnv.cmake
Normal file
@@ -0,0 +1,7 @@
|
||||
# A CMake script to find SetEnv.cmd.
|
||||
|
||||
find_program(WINSDK_SETENV NAMES SetEnv.cmd
|
||||
PATHS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows;CurrentInstallFolder]/bin")
|
||||
if (WINSDK_SETENV AND PRINT_PATH)
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} -E echo "${WINSDK_SETENV}")
|
||||
endif ()
|
||||
18
support/cmake/biicode.cmake
Normal file
18
support/cmake/biicode.cmake
Normal file
@@ -0,0 +1,18 @@
|
||||
# Initializes block variables
|
||||
INIT_BIICODE_BLOCK()
|
||||
|
||||
# Actually create targets: EXEcutables and libraries.
|
||||
ADD_BIICODE_TARGETS()
|
||||
|
||||
target_include_directories(${BII_BLOCK_TARGET} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
if (HAVE_OPEN)
|
||||
target_compile_definitions(${BII_BLOCK_TARGET} INTERFACE -DFMT_USE_FILE_DESCRIPTORS=1)
|
||||
endif ()
|
||||
|
||||
if (CMAKE_COMPILER_IS_GNUCXX)
|
||||
target_compile_options(${BII_BLOCK_TARGET} INTERFACE -Wall -Wextra -Wshadow -pedantic)
|
||||
endif ()
|
||||
if (CPP11_FLAG AND FMT_EXTRA_TESTS)
|
||||
target_compile_options(${BII_BLOCK_TARGET} INTERFACE ${CPP11_FLAG})
|
||||
endif ()
|
||||
11
support/cmake/run-cmake.bat
Normal file
11
support/cmake/run-cmake.bat
Normal file
@@ -0,0 +1,11 @@
|
||||
@echo on
|
||||
rem This scripts configures build environment and runs CMake.
|
||||
rem Use it instead of running CMake directly when building with
|
||||
rem the Microsoft SDK toolchain rather than Visual Studio.
|
||||
rem It is used in the same way as cmake, for example:
|
||||
rem
|
||||
rem run-cmake -G "Visual Studio 10 Win64" .
|
||||
|
||||
for /F "delims=" %%i IN ('cmake "-DPRINT_PATH=1" -P %~dp0/FindSetEnv.cmake') DO set setenv=%%i
|
||||
if NOT "%setenv%" == "" call "%setenv%"
|
||||
cmake %*
|
||||
46
support/download.py
Normal file
46
support/download.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# A file downloader.
|
||||
|
||||
import contextlib, os, tempfile, timer, urllib2, urlparse
|
||||
|
||||
class Downloader:
|
||||
def __init__(self, dir=None):
|
||||
self.dir = dir
|
||||
|
||||
# Downloads a file and removes it when exiting a block.
|
||||
# Usage:
|
||||
# d = Downloader()
|
||||
# with d.download(url) as f:
|
||||
# use_file(f)
|
||||
def download(self, url, cookie=None):
|
||||
suffix = os.path.splitext(urlparse.urlsplit(url)[2])[1]
|
||||
fd, filename = tempfile.mkstemp(suffix=suffix, dir=self.dir)
|
||||
os.close(fd)
|
||||
with timer.print_time('Downloading', url, 'to', filename):
|
||||
opener = urllib2.build_opener()
|
||||
if cookie:
|
||||
opener.addheaders.append(('Cookie', cookie))
|
||||
num_tries = 2
|
||||
for i in range(num_tries):
|
||||
try:
|
||||
f = opener.open(url)
|
||||
except urllib2.URLError, e:
|
||||
print('Failed to open url', url)
|
||||
continue
|
||||
length = f.headers.get('content-length')
|
||||
if not length:
|
||||
print('Failed to get content-length')
|
||||
continue
|
||||
length = int(length)
|
||||
with open(filename, 'wb') as out:
|
||||
count = 0
|
||||
while count < length:
|
||||
data = f.read(1024 * 1024)
|
||||
count += len(data)
|
||||
out.write(data)
|
||||
@contextlib.contextmanager
|
||||
def remove(filename):
|
||||
try:
|
||||
yield filename
|
||||
finally:
|
||||
os.remove(filename)
|
||||
return remove(filename)
|
||||
35
support/timer.py
Normal file
35
support/timer.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# A with statement based timer.
|
||||
|
||||
from __future__ import print_function
|
||||
from contextlib import contextmanager
|
||||
import timeit
|
||||
|
||||
class Timer:
|
||||
"""
|
||||
A with statement based timer.
|
||||
Usage:
|
||||
t = Timer()
|
||||
with t:
|
||||
do_something()
|
||||
time = t.time
|
||||
"""
|
||||
|
||||
def __enter__(self):
|
||||
self.start = timeit.default_timer()
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
finish = timeit.default_timer()
|
||||
self.time = finish - self.start
|
||||
|
||||
@contextmanager
|
||||
def print_time(*args):
|
||||
"""
|
||||
Measures and prints the time taken to execute nested code.
|
||||
args: Additional arguments to print.
|
||||
"""
|
||||
t = Timer()
|
||||
print(*args)
|
||||
with t:
|
||||
yield
|
||||
print(*args, end=' ')
|
||||
print('finished in {0:.2f} second(s)'.format(t.time))
|
||||
@@ -1,27 +1,80 @@
|
||||
# Test if compile errors are produced where necessary.
|
||||
set(TEST_MAIN_SRC test-main.cc gtest-extra.cc gtest-extra.h util.cc)
|
||||
add_library(test-main STATIC ${TEST_MAIN_SRC})
|
||||
target_link_libraries(test-main format gmock)
|
||||
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
include(CheckCXXSourceCompiles)
|
||||
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
||||
|
||||
function (expect_compile_error code)
|
||||
check_cxx_source_compiles("
|
||||
#include \"format.cc\"
|
||||
int main() {
|
||||
${code}
|
||||
}
|
||||
" compiles)
|
||||
set (does_compile ${compiles})
|
||||
# Unset the CMake cache variable compiles. Otherwise the compile test will
|
||||
# just use cached information next time it runs.
|
||||
unset(compiles CACHE)
|
||||
if (does_compile)
|
||||
message(FATAL_ERROR "No compile error for: ${code}")
|
||||
# Adds a test.
|
||||
# Usage: add_fmt_test(name [CUSTOM_LINK] srcs...)
|
||||
function(add_fmt_test name)
|
||||
cmake_parse_arguments(add_fmt_test CUSTOM_LINK "" "" ${ARGN})
|
||||
add_executable(${name} ${name}.cc ${add_fmt_test_UNPARSED_ARGUMENTS})
|
||||
target_link_libraries(${name} test-main)
|
||||
if (NOT add_fmt_test_CUSTOM_LINK)
|
||||
target_link_libraries(${name} format)
|
||||
endif ()
|
||||
endfunction ()
|
||||
add_test(NAME ${name} COMMAND ${name})
|
||||
endfunction()
|
||||
|
||||
# Writing a wide character to a character stream Writer is forbidden.
|
||||
expect_compile_error("fmt::Writer() << L'a';")
|
||||
expect_compile_error("fmt::Writer() << fmt::pad(\"abc\", 5, L' ');")
|
||||
expect_compile_error("fmt::Writer() << fmt::pad(42, 5, L' ');")
|
||||
add_fmt_test(gtest-extra-test)
|
||||
add_fmt_test(format-test)
|
||||
add_fmt_test(format-impl-test CUSTOM_LINK)
|
||||
add_fmt_test(printf-test)
|
||||
foreach (target format-test printf-test)
|
||||
if (CMAKE_COMPILER_IS_GNUCXX)
|
||||
set_target_properties(${target} PROPERTIES COMPILE_FLAGS
|
||||
"-Wall -Wextra -pedantic -Wno-long-long -Wno-variadic-macros")
|
||||
endif ()
|
||||
if (CPP11_FLAG)
|
||||
set_target_properties(${target} PROPERTIES COMPILE_FLAGS ${CPP11_FLAG})
|
||||
endif ()
|
||||
endforeach ()
|
||||
add_fmt_test(util-test mock-allocator.h)
|
||||
if (CPP11_FLAG)
|
||||
set_target_properties(util-test PROPERTIES COMPILE_FLAGS ${CPP11_FLAG})
|
||||
endif ()
|
||||
|
||||
foreach (src ${FMT_SOURCES})
|
||||
set(FMT_TEST_SOURCES ${FMT_TEST_SOURCES} ../${src})
|
||||
endforeach ()
|
||||
|
||||
check_cxx_source_compiles("
|
||||
#include <type_traits>
|
||||
class C { void operator=(const C&); };
|
||||
int main() { static_assert(!std::is_copy_assignable<C>::value, \"\"); }"
|
||||
HAVE_TYPE_TRAITS)
|
||||
if (HAVE_TYPE_TRAITS)
|
||||
add_definitions(-DFMT_USE_TYPE_TRAITS=1)
|
||||
endif ()
|
||||
|
||||
add_executable(macro-test macro-test.cc ${FMT_TEST_SOURCES} ${TEST_MAIN_SRC})
|
||||
set_target_properties(macro-test
|
||||
PROPERTIES COMPILE_DEFINITIONS "FMT_USE_VARIADIC_TEMPLATES=0")
|
||||
target_link_libraries(macro-test gmock)
|
||||
|
||||
if (HAVE_OPEN)
|
||||
add_executable(posix-test posix-test.cc ${FMT_TEST_SOURCES} ${TEST_MAIN_SRC})
|
||||
set_target_properties(posix-test
|
||||
PROPERTIES COMPILE_DEFINITIONS "FMT_INCLUDE_POSIX_TEST=1")
|
||||
target_link_libraries(posix-test gmock)
|
||||
add_test(NAME posix-test COMMAND posix-test)
|
||||
endif ()
|
||||
|
||||
add_executable(header-only-test
|
||||
header-only-test.cc header-only-test2.cc test-main.cc)
|
||||
set_target_properties(header-only-test
|
||||
PROPERTIES COMPILE_DEFINITIONS "FMT_HEADER_ONLY=1")
|
||||
target_link_libraries(header-only-test gmock)
|
||||
|
||||
# Test that the library can be compiled with exceptions disabled.
|
||||
check_cxx_compiler_flag(-fno-exceptions HAVE_FNO_EXCEPTIONS_FLAG)
|
||||
if (HAVE_FNO_EXCEPTIONS_FLAG)
|
||||
add_library(noexception-test STATIC ../format.cc)
|
||||
set_target_properties(noexception-test
|
||||
PROPERTIES COMPILE_FLAGS -fno-exceptions)
|
||||
endif ()
|
||||
|
||||
add_test(compile-test ${CMAKE_CTEST_COMMAND}
|
||||
--build-and-test
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/compile-test"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/compile-test"
|
||||
--build-generator ${CMAKE_GENERATOR}
|
||||
--build-makeprogram ${CMAKE_MAKE_PROGRAM})
|
||||
|
||||
41
test/compile-test/CMakeLists.txt
Normal file
41
test/compile-test/CMakeLists.txt
Normal file
@@ -0,0 +1,41 @@
|
||||
# Test if compile errors are produced where necessary.
|
||||
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
include(CheckCXXSourceCompiles)
|
||||
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../..)
|
||||
|
||||
function (expect_compile_error code)
|
||||
check_cxx_source_compiles("
|
||||
#include \"format.cc\"
|
||||
#include \"posix.h\"
|
||||
int main() {
|
||||
${code}
|
||||
}
|
||||
" compiles)
|
||||
set (does_compile ${compiles})
|
||||
# Unset the CMake cache variable compiles. Otherwise the compile test will
|
||||
# just use cached information next time it runs.
|
||||
unset(compiles CACHE)
|
||||
if (does_compile)
|
||||
message(FATAL_ERROR "No compile error for: ${code}")
|
||||
endif ()
|
||||
endfunction ()
|
||||
|
||||
# MakeArg doesn't accept [const] volatile char *.
|
||||
expect_compile_error("volatile char s[] = \"test\"; (fmt::internal::MakeArg<char>)(s);")
|
||||
expect_compile_error("const volatile char s[] = \"test\"; (fmt::internal::MakeArg<char>)(s);")
|
||||
|
||||
# MakeArg<char> doesn't accept wchar_t.
|
||||
expect_compile_error("fmt::internal::MakeValue<char>(L'a');")
|
||||
expect_compile_error("fmt::internal::MakeValue<char>(L\"test\");")
|
||||
|
||||
# Writing a wide character to a character stream Writer is forbidden.
|
||||
expect_compile_error("fmt::MemoryWriter() << L'a';")
|
||||
expect_compile_error("fmt::MemoryWriter() << fmt::pad(\"abc\", 5, L' ');")
|
||||
expect_compile_error("fmt::MemoryWriter() << fmt::pad(42, 5, L' ');")
|
||||
|
||||
# Formatting a wide character with a narrow format string is forbidden.
|
||||
expect_compile_error("fmt::format(\"{}\", L'a';")
|
||||
|
||||
expect_compile_error("FMT_STATIC_ASSERT(0 > 1, \"oops\");")
|
||||
112
test/format-impl-test.cc
Normal file
112
test/format-impl-test.cc
Normal file
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
Formatting library implementation tests.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// Include format.cc instead of format.h to test implementation-specific stuff.
|
||||
#include "format.cc"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "gtest-extra.h"
|
||||
#include "util.h"
|
||||
|
||||
#undef max
|
||||
|
||||
TEST(FormatTest, ArgConverter) {
|
||||
using fmt::internal::Arg;
|
||||
Arg arg = Arg();
|
||||
arg.type = Arg::LONG_LONG;
|
||||
arg.long_long_value = std::numeric_limits<fmt::LongLong>::max();
|
||||
ArgConverter<fmt::LongLong>(arg, 'd').visit(arg);
|
||||
EXPECT_EQ(Arg::LONG_LONG, arg.type);
|
||||
}
|
||||
|
||||
TEST(FormatTest, FormatNegativeNaN) {
|
||||
double nan = std::numeric_limits<double>::quiet_NaN();
|
||||
if (fmt::internal::getsign(-nan))
|
||||
EXPECT_EQ("-nan", fmt::format("{}", -nan));
|
||||
else
|
||||
fmt::print("Warning: compiler doesn't handle negative NaN correctly");
|
||||
}
|
||||
|
||||
TEST(FormatTest, StrError) {
|
||||
char *message = 0;
|
||||
char buffer[BUFFER_SIZE];
|
||||
#ifndef NDEBUG
|
||||
EXPECT_DEBUG_DEATH(safe_strerror(EDOM, message = 0, 0), "Assertion");
|
||||
EXPECT_DEBUG_DEATH(safe_strerror(EDOM, message = buffer, 0), "Assertion");
|
||||
#endif
|
||||
buffer[0] = 'x';
|
||||
#ifdef _GNU_SOURCE
|
||||
// Use invalid error code to make sure that safe_strerror returns an error
|
||||
// message in the buffer rather than a pointer to a static string.
|
||||
int error_code = -1;
|
||||
#else
|
||||
int error_code = EDOM;
|
||||
#endif
|
||||
|
||||
int result = safe_strerror(error_code, message = buffer, BUFFER_SIZE);
|
||||
EXPECT_EQ(0, result);
|
||||
std::size_t message_size = std::strlen(message);
|
||||
EXPECT_GE(BUFFER_SIZE - 1u, message_size);
|
||||
EXPECT_EQ(get_system_error(error_code), message);
|
||||
|
||||
// safe_strerror never uses buffer on MinGW.
|
||||
#ifndef __MINGW32__
|
||||
result = safe_strerror(error_code, message = buffer, message_size);
|
||||
EXPECT_EQ(ERANGE, result);
|
||||
result = safe_strerror(error_code, message = buffer, 1);
|
||||
EXPECT_EQ(buffer, message); // Message should point to buffer.
|
||||
EXPECT_EQ(ERANGE, result);
|
||||
EXPECT_STREQ("", message);
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(FormatTest, FormatErrorCode) {
|
||||
std::string msg = "error 42", sep = ": ";
|
||||
{
|
||||
fmt::MemoryWriter w;
|
||||
w << "garbage";
|
||||
format_error_code(w, 42, "test");
|
||||
EXPECT_EQ("test: " + msg, w.str());
|
||||
}
|
||||
{
|
||||
fmt::MemoryWriter w;
|
||||
std::string prefix(
|
||||
fmt::internal::INLINE_BUFFER_SIZE - msg.size() - sep.size() + 1, 'x');
|
||||
format_error_code(w, 42, prefix);
|
||||
EXPECT_EQ(msg, w.str());
|
||||
}
|
||||
{
|
||||
fmt::MemoryWriter w;
|
||||
std::string prefix(
|
||||
fmt::internal::INLINE_BUFFER_SIZE - msg.size() - sep.size(), 'x');
|
||||
format_error_code(w, 42, prefix);
|
||||
EXPECT_EQ(prefix + sep + msg, w.str());
|
||||
std::size_t size = fmt::internal::INLINE_BUFFER_SIZE;
|
||||
EXPECT_EQ(size, w.size());
|
||||
}
|
||||
}
|
||||
1465
test/format-test.cc
Normal file
1465
test/format-test.cc
Normal file
File diff suppressed because it is too large
Load Diff
834
test/gtest-extra-test.cc
Normal file
834
test/gtest-extra-test.cc
Normal file
@@ -0,0 +1,834 @@
|
||||
/*
|
||||
Tests of custom Google Test assertions.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gtest-extra.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <gtest/gtest-spi.h>
|
||||
|
||||
#if defined(_WIN32) && !defined(__MINGW32__)
|
||||
# include <crtdbg.h> // for _CrtSetReportMode
|
||||
#endif // _WIN32
|
||||
|
||||
namespace {
|
||||
|
||||
#if defined(_WIN32) && !defined(__MINGW32__)
|
||||
|
||||
// Suppresses Windows assertions on invalid file descriptors, making
|
||||
// POSIX functions return proper error codes instead of crashing on Windows.
|
||||
class SuppressAssert {
|
||||
private:
|
||||
_invalid_parameter_handler original_handler_;
|
||||
int original_report_mode_;
|
||||
|
||||
static void handle_invalid_parameter(const wchar_t *,
|
||||
const wchar_t *, const wchar_t *, unsigned , uintptr_t) {}
|
||||
|
||||
public:
|
||||
SuppressAssert()
|
||||
: original_handler_(_set_invalid_parameter_handler(handle_invalid_parameter)),
|
||||
original_report_mode_(_CrtSetReportMode(_CRT_ASSERT, 0)) {
|
||||
}
|
||||
~SuppressAssert() {
|
||||
_set_invalid_parameter_handler(original_handler_);
|
||||
_CrtSetReportMode(_CRT_ASSERT, original_report_mode_);
|
||||
}
|
||||
};
|
||||
|
||||
# define SUPPRESS_ASSERT(statement) { SuppressAssert sa; statement; }
|
||||
|
||||
// Fix "secure" warning about using fopen without defining
|
||||
// _CRT_SECURE_NO_WARNINGS.
|
||||
FILE *safe_fopen(const char *filename, const char *mode) {
|
||||
FILE *f = 0;
|
||||
errno = fopen_s(&f, filename, mode);
|
||||
return f;
|
||||
}
|
||||
#define fopen safe_fopen
|
||||
#else
|
||||
# define SUPPRESS_ASSERT(statement) statement
|
||||
using std::fopen;
|
||||
#endif // _WIN32
|
||||
|
||||
#define EXPECT_SYSTEM_ERROR_NOASSERT(statement, error_code, message) \
|
||||
EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message)
|
||||
|
||||
// Tests that assertion macros evaluate their arguments exactly once.
|
||||
class SingleEvaluationTest : public ::testing::Test {
|
||||
protected:
|
||||
SingleEvaluationTest() {
|
||||
p_ = s_;
|
||||
a_ = 0;
|
||||
b_ = 0;
|
||||
}
|
||||
|
||||
static const char* const s_;
|
||||
static const char* p_;
|
||||
|
||||
static int a_;
|
||||
static int b_;
|
||||
};
|
||||
|
||||
const char* const SingleEvaluationTest::s_ = "01234";
|
||||
const char* SingleEvaluationTest::p_;
|
||||
int SingleEvaluationTest::a_;
|
||||
int SingleEvaluationTest::b_;
|
||||
|
||||
void do_nothing() {}
|
||||
|
||||
void throw_exception() {
|
||||
throw std::runtime_error("test");
|
||||
}
|
||||
|
||||
void throw_system_error() {
|
||||
throw fmt::SystemError(EDOM, "test");
|
||||
}
|
||||
|
||||
// Tests that when EXPECT_THROW_MSG fails, it evaluates its message argument
|
||||
// exactly once.
|
||||
TEST_F(SingleEvaluationTest, FailedEXPECT_THROW_MSG) {
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THROW_MSG(throw_exception(), std::exception, p_++), "01234");
|
||||
EXPECT_EQ(s_ + 1, p_);
|
||||
}
|
||||
|
||||
// Tests that when EXPECT_SYSTEM_ERROR fails, it evaluates its message argument
|
||||
// exactly once.
|
||||
TEST_F(SingleEvaluationTest, FailedEXPECT_SYSTEM_ERROR) {
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, p_++), "01234");
|
||||
EXPECT_EQ(s_ + 1, p_);
|
||||
}
|
||||
|
||||
// Tests that when EXPECT_WRITE fails, it evaluates its message argument
|
||||
// exactly once.
|
||||
TEST_F(SingleEvaluationTest, FailedEXPECT_WRITE) {
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_WRITE(stdout, std::printf("test"), p_++), "01234");
|
||||
EXPECT_EQ(s_ + 1, p_);
|
||||
}
|
||||
|
||||
// Tests that assertion arguments are evaluated exactly once.
|
||||
TEST_F(SingleEvaluationTest, ExceptionTests) {
|
||||
// successful EXPECT_THROW_MSG
|
||||
EXPECT_THROW_MSG({ // NOLINT
|
||||
a_++;
|
||||
throw_exception();
|
||||
}, std::exception, (b_++, "test"));
|
||||
EXPECT_EQ(1, a_);
|
||||
EXPECT_EQ(1, b_);
|
||||
|
||||
// failed EXPECT_THROW_MSG, throws different type
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG({ // NOLINT
|
||||
a_++;
|
||||
throw_exception();
|
||||
}, std::logic_error, (b_++, "test")), "throws a different type");
|
||||
EXPECT_EQ(2, a_);
|
||||
EXPECT_EQ(2, b_);
|
||||
|
||||
// failed EXPECT_THROW_MSG, throws an exception with different message
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG({ // NOLINT
|
||||
a_++;
|
||||
throw_exception();
|
||||
}, std::exception, (b_++, "other")),
|
||||
"throws an exception with a different message");
|
||||
EXPECT_EQ(3, a_);
|
||||
EXPECT_EQ(3, b_);
|
||||
|
||||
// failed EXPECT_THROW_MSG, throws nothing
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THROW_MSG(a_++, std::exception, (b_++, "test")), "throws nothing");
|
||||
EXPECT_EQ(4, a_);
|
||||
EXPECT_EQ(4, b_);
|
||||
}
|
||||
|
||||
TEST_F(SingleEvaluationTest, SystemErrorTests) {
|
||||
// successful EXPECT_SYSTEM_ERROR
|
||||
EXPECT_SYSTEM_ERROR({ // NOLINT
|
||||
a_++;
|
||||
throw_system_error();
|
||||
}, EDOM, (b_++, "test"));
|
||||
EXPECT_EQ(1, a_);
|
||||
EXPECT_EQ(1, b_);
|
||||
|
||||
// failed EXPECT_SYSTEM_ERROR, throws different type
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR({ // NOLINT
|
||||
a_++;
|
||||
throw_exception();
|
||||
}, EDOM, (b_++, "test")), "throws a different type");
|
||||
EXPECT_EQ(2, a_);
|
||||
EXPECT_EQ(2, b_);
|
||||
|
||||
// failed EXPECT_SYSTEM_ERROR, throws an exception with different message
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR({ // NOLINT
|
||||
a_++;
|
||||
throw_system_error();
|
||||
}, EDOM, (b_++, "other")),
|
||||
"throws an exception with a different message");
|
||||
EXPECT_EQ(3, a_);
|
||||
EXPECT_EQ(3, b_);
|
||||
|
||||
// failed EXPECT_SYSTEM_ERROR, throws nothing
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(a_++, EDOM, (b_++, "test")), "throws nothing");
|
||||
EXPECT_EQ(4, a_);
|
||||
EXPECT_EQ(4, b_);
|
||||
}
|
||||
|
||||
// Tests that assertion arguments are evaluated exactly once.
|
||||
TEST_F(SingleEvaluationTest, WriteTests) {
|
||||
// successful EXPECT_WRITE
|
||||
EXPECT_WRITE(stdout, { // NOLINT
|
||||
a_++;
|
||||
std::printf("test");
|
||||
}, (b_++, "test"));
|
||||
EXPECT_EQ(1, a_);
|
||||
EXPECT_EQ(1, b_);
|
||||
|
||||
// failed EXPECT_WRITE
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_WRITE(stdout, { // NOLINT
|
||||
a_++;
|
||||
std::printf("test");
|
||||
}, (b_++, "other")), "Actual: test");
|
||||
EXPECT_EQ(2, a_);
|
||||
EXPECT_EQ(2, b_);
|
||||
}
|
||||
|
||||
// Tests that the compiler will not complain about unreachable code in the
|
||||
// EXPECT_THROW_MSG macro.
|
||||
TEST(ExpectThrowTest, DoesNotGenerateUnreachableCodeWarning) {
|
||||
int n = 0;
|
||||
using std::runtime_error;
|
||||
EXPECT_THROW_MSG(throw runtime_error(""), runtime_error, "");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(n++, runtime_error, ""), "");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(throw 1, runtime_error, ""), "");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_THROW_MSG(
|
||||
throw runtime_error("a"), runtime_error, "b"), "");
|
||||
}
|
||||
|
||||
// Tests that the compiler will not complain about unreachable code in the
|
||||
// EXPECT_SYSTEM_ERROR macro.
|
||||
TEST(ExpectSystemErrorTest, DoesNotGenerateUnreachableCodeWarning) {
|
||||
int n = 0;
|
||||
EXPECT_SYSTEM_ERROR(throw fmt::SystemError(EDOM, "test"), EDOM, "test");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(n++, EDOM, ""), "");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(throw 1, EDOM, ""), "");
|
||||
EXPECT_NONFATAL_FAILURE(EXPECT_SYSTEM_ERROR(
|
||||
throw fmt::SystemError(EDOM, "aaa"), EDOM, "bbb"), "");
|
||||
}
|
||||
|
||||
TEST(AssertionSyntaxTest, ExceptionAssertionBehavesLikeSingleStatement) {
|
||||
if (::testing::internal::AlwaysFalse())
|
||||
EXPECT_THROW_MSG(do_nothing(), std::exception, "");
|
||||
|
||||
if (::testing::internal::AlwaysTrue())
|
||||
EXPECT_THROW_MSG(throw_exception(), std::exception, "test");
|
||||
else
|
||||
do_nothing();
|
||||
}
|
||||
|
||||
TEST(AssertionSyntaxTest, SystemErrorAssertionBehavesLikeSingleStatement) {
|
||||
if (::testing::internal::AlwaysFalse())
|
||||
EXPECT_SYSTEM_ERROR(do_nothing(), EDOM, "");
|
||||
|
||||
if (::testing::internal::AlwaysTrue())
|
||||
EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test");
|
||||
else
|
||||
do_nothing();
|
||||
}
|
||||
|
||||
TEST(AssertionSyntaxTest, WriteAssertionBehavesLikeSingleStatement) {
|
||||
if (::testing::internal::AlwaysFalse())
|
||||
EXPECT_WRITE(stdout, std::printf("x"), "x");
|
||||
|
||||
if (::testing::internal::AlwaysTrue())
|
||||
EXPECT_WRITE(stdout, std::printf("x"), "x");
|
||||
else
|
||||
do_nothing();
|
||||
}
|
||||
|
||||
// Tests EXPECT_THROW_MSG.
|
||||
TEST(ExpectTest, EXPECT_THROW_MSG) {
|
||||
EXPECT_THROW_MSG(throw_exception(), std::exception, "test");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THROW_MSG(throw_exception(), std::logic_error, "test"),
|
||||
"Expected: throw_exception() throws an exception of "
|
||||
"type std::logic_error.\n Actual: it throws a different type.");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THROW_MSG(do_nothing(), std::exception, "test"),
|
||||
"Expected: do_nothing() throws an exception of type std::exception.\n"
|
||||
" Actual: it throws nothing.");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THROW_MSG(throw_exception(), std::exception, "other"),
|
||||
"throw_exception() throws an exception with a different message.\n"
|
||||
"Expected: other\n"
|
||||
" Actual: test");
|
||||
}
|
||||
|
||||
// Tests EXPECT_SYSTEM_ERROR.
|
||||
TEST(ExpectTest, EXPECT_SYSTEM_ERROR) {
|
||||
EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(throw_exception(), EDOM, "test"),
|
||||
"Expected: throw_exception() throws an exception of "
|
||||
"type fmt::SystemError.\n Actual: it throws a different type.");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(do_nothing(), EDOM, "test"),
|
||||
"Expected: do_nothing() throws an exception of type fmt::SystemError.\n"
|
||||
" Actual: it throws nothing.");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "other"),
|
||||
fmt::format(
|
||||
"throw_system_error() throws an exception with a different message.\n"
|
||||
"Expected: {}\n"
|
||||
" Actual: {}",
|
||||
format_system_error(EDOM, "other"),
|
||||
format_system_error(EDOM, "test")));
|
||||
}
|
||||
|
||||
// Tests EXPECT_WRITE.
|
||||
TEST(ExpectTest, EXPECT_WRITE) {
|
||||
EXPECT_WRITE(stdout, do_nothing(), "");
|
||||
EXPECT_WRITE(stdout, std::printf("test"), "test");
|
||||
EXPECT_WRITE(stderr, std::fprintf(stderr, "test"), "test");
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_WRITE(stdout, std::printf("that"), "this"),
|
||||
"Expected: this\n"
|
||||
" Actual: that");
|
||||
}
|
||||
|
||||
TEST(StreamingAssertionsTest, EXPECT_THROW_MSG) {
|
||||
EXPECT_THROW_MSG(throw_exception(), std::exception, "test")
|
||||
<< "unexpected failure";
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THROW_MSG(throw_exception(), std::exception, "other")
|
||||
<< "expected failure", "expected failure");
|
||||
}
|
||||
|
||||
TEST(StreamingAssertionsTest, EXPECT_SYSTEM_ERROR) {
|
||||
EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test")
|
||||
<< "unexpected failure";
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "other")
|
||||
<< "expected failure", "expected failure");
|
||||
}
|
||||
|
||||
TEST(StreamingAssertionsTest, EXPECT_WRITE) {
|
||||
EXPECT_WRITE(stdout, std::printf("test"), "test")
|
||||
<< "unexpected failure";
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_WRITE(stdout, std::printf("test"), "other")
|
||||
<< "expected failure", "expected failure");
|
||||
}
|
||||
|
||||
TEST(UtilTest, FormatSystemError) {
|
||||
fmt::MemoryWriter out;
|
||||
fmt::internal::format_system_error(out, EDOM, "test message");
|
||||
EXPECT_EQ(out.str(), format_system_error(EDOM, "test message"));
|
||||
}
|
||||
|
||||
#if FMT_USE_FILE_DESCRIPTORS
|
||||
|
||||
using fmt::BufferedFile;
|
||||
using fmt::ErrorCode;
|
||||
using fmt::File;
|
||||
|
||||
// Checks if the file is open by reading one character from it.
|
||||
bool isopen(int fd) {
|
||||
char buffer;
|
||||
return FMT_POSIX(read(fd, &buffer, 1)) == 1;
|
||||
}
|
||||
|
||||
bool isclosed(int fd) {
|
||||
char buffer;
|
||||
std::streamsize result = 0;
|
||||
SUPPRESS_ASSERT(result = FMT_POSIX(read(fd, &buffer, 1)));
|
||||
return result == -1 && errno == EBADF;
|
||||
}
|
||||
|
||||
// Attempts to read count characters from a file.
|
||||
std::string read(File &f, std::size_t count) {
|
||||
std::string buffer(count, '\0');
|
||||
std::streamsize n = 0;
|
||||
std::size_t offset = 0;
|
||||
do {
|
||||
n = f.read(&buffer[offset], count - offset);
|
||||
// We can't read more than size_t bytes since count has type size_t.
|
||||
offset += static_cast<std::size_t>(n);
|
||||
} while (offset < count && n != 0);
|
||||
buffer.resize(offset);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// Attempts to write a string to a file.
|
||||
void write(File &f, fmt::StringRef s) {
|
||||
std::size_t num_chars_left = s.size();
|
||||
const char *ptr = s.c_str();
|
||||
do {
|
||||
std::streamsize count = f.write(ptr, num_chars_left);
|
||||
ptr += count;
|
||||
// We can't write more than size_t bytes since num_chars_left
|
||||
// has type size_t.
|
||||
num_chars_left -= static_cast<std::size_t>(count);
|
||||
} while (num_chars_left != 0);
|
||||
}
|
||||
|
||||
#define EXPECT_READ(file, expected_content) \
|
||||
EXPECT_EQ(expected_content, read(file, std::strlen(expected_content)))
|
||||
|
||||
TEST(ErrorCodeTest, Ctor) {
|
||||
EXPECT_EQ(0, ErrorCode().get());
|
||||
EXPECT_EQ(42, ErrorCode(42).get());
|
||||
}
|
||||
|
||||
const char FILE_CONTENT[] = "Don't panic!";
|
||||
|
||||
// Opens a file for reading.
|
||||
File open_file() {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
write_end.write(FILE_CONTENT, sizeof(FILE_CONTENT) - 1);
|
||||
write_end.close();
|
||||
return read_end;
|
||||
}
|
||||
|
||||
// Opens a buffered file for reading.
|
||||
BufferedFile open_buffered_file(FILE **fp = 0) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
write_end.write(FILE_CONTENT, sizeof(FILE_CONTENT) - 1);
|
||||
write_end.close();
|
||||
BufferedFile f = read_end.fdopen("r");
|
||||
if (fp)
|
||||
*fp = f.get();
|
||||
return f;
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, DefaultCtor) {
|
||||
BufferedFile f;
|
||||
EXPECT_TRUE(f.get() == 0);
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, MoveCtor) {
|
||||
BufferedFile bf = open_buffered_file();
|
||||
FILE *fp = bf.get();
|
||||
EXPECT_TRUE(fp != 0);
|
||||
BufferedFile bf2(std::move(bf));
|
||||
EXPECT_EQ(fp, bf2.get());
|
||||
EXPECT_TRUE(bf.get() == 0);
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, MoveAssignment) {
|
||||
BufferedFile bf = open_buffered_file();
|
||||
FILE *fp = bf.get();
|
||||
EXPECT_TRUE(fp != 0);
|
||||
BufferedFile bf2;
|
||||
bf2 = std::move(bf);
|
||||
EXPECT_EQ(fp, bf2.get());
|
||||
EXPECT_TRUE(bf.get() == 0);
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, MoveAssignmentClosesFile) {
|
||||
BufferedFile bf = open_buffered_file();
|
||||
BufferedFile bf2 = open_buffered_file();
|
||||
int old_fd = bf2.fileno();
|
||||
bf2 = std::move(bf);
|
||||
EXPECT_TRUE(isclosed(old_fd));
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, MoveFromTemporaryInCtor) {
|
||||
FILE *fp = 0;
|
||||
BufferedFile f(open_buffered_file(&fp));
|
||||
EXPECT_EQ(fp, f.get());
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, MoveFromTemporaryInAssignment) {
|
||||
FILE *fp = 0;
|
||||
BufferedFile f;
|
||||
f = open_buffered_file(&fp);
|
||||
EXPECT_EQ(fp, f.get());
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, MoveFromTemporaryInAssignmentClosesFile) {
|
||||
BufferedFile f = open_buffered_file();
|
||||
int old_fd = f.fileno();
|
||||
f = open_buffered_file();
|
||||
EXPECT_TRUE(isclosed(old_fd));
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, CloseFileInDtor) {
|
||||
int fd = 0;
|
||||
{
|
||||
BufferedFile f = open_buffered_file();
|
||||
fd = f.fileno();
|
||||
}
|
||||
EXPECT_TRUE(isclosed(fd));
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, CloseErrorInDtor) {
|
||||
BufferedFile *f = new BufferedFile(open_buffered_file());
|
||||
EXPECT_WRITE(stderr, {
|
||||
// The close function must be called inside EXPECT_WRITE, otherwise
|
||||
// the system may recycle closed file descriptor when redirecting the
|
||||
// output in EXPECT_STDERR and the second close will break output
|
||||
// redirection.
|
||||
FMT_POSIX(close(f->fileno()));
|
||||
SUPPRESS_ASSERT(delete f);
|
||||
}, format_system_error(EBADF, "cannot close file") + "\n");
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, Close) {
|
||||
BufferedFile f = open_buffered_file();
|
||||
int fd = f.fileno();
|
||||
f.close();
|
||||
EXPECT_TRUE(f.get() == 0);
|
||||
EXPECT_TRUE(isclosed(fd));
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, CloseError) {
|
||||
BufferedFile f = open_buffered_file();
|
||||
FMT_POSIX(close(f.fileno()));
|
||||
EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
|
||||
EXPECT_TRUE(f.get() == 0);
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, Fileno) {
|
||||
BufferedFile f;
|
||||
// fileno on a null FILE pointer either crashes or returns an error.
|
||||
EXPECT_DEATH({
|
||||
try {
|
||||
f.fileno();
|
||||
} catch (fmt::SystemError) {
|
||||
std::exit(1);
|
||||
}
|
||||
}, "");
|
||||
f = open_buffered_file();
|
||||
EXPECT_TRUE(f.fileno() != -1);
|
||||
File copy = File::dup(f.fileno());
|
||||
EXPECT_READ(copy, FILE_CONTENT);
|
||||
}
|
||||
|
||||
TEST(FileTest, DefaultCtor) {
|
||||
File f;
|
||||
EXPECT_EQ(-1, f.descriptor());
|
||||
}
|
||||
|
||||
TEST(FileTest, OpenBufferedFileInCtor) {
|
||||
FILE *fp = fopen("test-file", "w");
|
||||
std::fputs(FILE_CONTENT, fp);
|
||||
std::fclose(fp);
|
||||
File f("test-file", File::RDONLY);
|
||||
ASSERT_TRUE(isopen(f.descriptor()));
|
||||
}
|
||||
|
||||
TEST(FileTest, OpenBufferedFileError) {
|
||||
EXPECT_SYSTEM_ERROR(File("nonexistent", File::RDONLY),
|
||||
ENOENT, "cannot open file nonexistent");
|
||||
}
|
||||
|
||||
TEST(FileTest, MoveCtor) {
|
||||
File f = open_file();
|
||||
int fd = f.descriptor();
|
||||
EXPECT_NE(-1, fd);
|
||||
File f2(std::move(f));
|
||||
EXPECT_EQ(fd, f2.descriptor());
|
||||
EXPECT_EQ(-1, f.descriptor());
|
||||
}
|
||||
|
||||
TEST(FileTest, MoveAssignment) {
|
||||
File f = open_file();
|
||||
int fd = f.descriptor();
|
||||
EXPECT_NE(-1, fd);
|
||||
File f2;
|
||||
f2 = std::move(f);
|
||||
EXPECT_EQ(fd, f2.descriptor());
|
||||
EXPECT_EQ(-1, f.descriptor());
|
||||
}
|
||||
|
||||
TEST(FileTest, MoveAssignmentClosesFile) {
|
||||
File f = open_file();
|
||||
File f2 = open_file();
|
||||
int old_fd = f2.descriptor();
|
||||
f2 = std::move(f);
|
||||
EXPECT_TRUE(isclosed(old_fd));
|
||||
}
|
||||
|
||||
File OpenBufferedFile(int &fd) {
|
||||
File f = open_file();
|
||||
fd = f.descriptor();
|
||||
return std::move(f);
|
||||
}
|
||||
|
||||
TEST(FileTest, MoveFromTemporaryInCtor) {
|
||||
int fd = 0xdeadbeef;
|
||||
File f(OpenBufferedFile(fd));
|
||||
EXPECT_EQ(fd, f.descriptor());
|
||||
}
|
||||
|
||||
TEST(FileTest, MoveFromTemporaryInAssignment) {
|
||||
int fd = 0xdeadbeef;
|
||||
File f;
|
||||
f = OpenBufferedFile(fd);
|
||||
EXPECT_EQ(fd, f.descriptor());
|
||||
}
|
||||
|
||||
TEST(FileTest, MoveFromTemporaryInAssignmentClosesFile) {
|
||||
int fd = 0xdeadbeef;
|
||||
File f = open_file();
|
||||
int old_fd = f.descriptor();
|
||||
f = OpenBufferedFile(fd);
|
||||
EXPECT_TRUE(isclosed(old_fd));
|
||||
}
|
||||
|
||||
TEST(FileTest, CloseFileInDtor) {
|
||||
int fd = 0;
|
||||
{
|
||||
File f = open_file();
|
||||
fd = f.descriptor();
|
||||
}
|
||||
EXPECT_TRUE(isclosed(fd));
|
||||
}
|
||||
|
||||
TEST(FileTest, CloseErrorInDtor) {
|
||||
File *f = new File(open_file());
|
||||
EXPECT_WRITE(stderr, {
|
||||
// The close function must be called inside EXPECT_WRITE, otherwise
|
||||
// the system may recycle closed file descriptor when redirecting the
|
||||
// output in EXPECT_STDERR and the second close will break output
|
||||
// redirection.
|
||||
FMT_POSIX(close(f->descriptor()));
|
||||
SUPPRESS_ASSERT(delete f);
|
||||
}, format_system_error(EBADF, "cannot close file") + "\n");
|
||||
}
|
||||
|
||||
TEST(FileTest, Close) {
|
||||
File f = open_file();
|
||||
int fd = f.descriptor();
|
||||
f.close();
|
||||
EXPECT_EQ(-1, f.descriptor());
|
||||
EXPECT_TRUE(isclosed(fd));
|
||||
}
|
||||
|
||||
TEST(FileTest, CloseError) {
|
||||
File f = open_file();
|
||||
FMT_POSIX(close(f.descriptor()));
|
||||
EXPECT_SYSTEM_ERROR_NOASSERT(f.close(), EBADF, "cannot close file");
|
||||
EXPECT_EQ(-1, f.descriptor());
|
||||
}
|
||||
|
||||
TEST(FileTest, Read) {
|
||||
File f = open_file();
|
||||
EXPECT_READ(f, FILE_CONTENT);
|
||||
}
|
||||
|
||||
TEST(FileTest, ReadError) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
char buf;
|
||||
// We intentionally read from write_end to cause error.
|
||||
EXPECT_SYSTEM_ERROR(write_end.read(&buf, 1), EBADF, "cannot read from file");
|
||||
}
|
||||
|
||||
TEST(FileTest, Write) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
write(write_end, "test");
|
||||
write_end.close();
|
||||
EXPECT_READ(read_end, "test");
|
||||
}
|
||||
|
||||
TEST(FileTest, WriteError) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
// We intentionally write to read_end to cause error.
|
||||
EXPECT_SYSTEM_ERROR(read_end.write(" ", 1), EBADF, "cannot write to file");
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup) {
|
||||
File f = open_file();
|
||||
File copy = File::dup(f.descriptor());
|
||||
EXPECT_NE(f.descriptor(), copy.descriptor());
|
||||
EXPECT_EQ(FILE_CONTENT, read(copy, sizeof(FILE_CONTENT) - 1));
|
||||
}
|
||||
|
||||
TEST(FileTest, DupError) {
|
||||
EXPECT_SYSTEM_ERROR_NOASSERT(File::dup(-1),
|
||||
EBADF, "cannot duplicate file descriptor -1");
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup2) {
|
||||
File f = open_file();
|
||||
File copy = open_file();
|
||||
f.dup2(copy.descriptor());
|
||||
EXPECT_NE(f.descriptor(), copy.descriptor());
|
||||
EXPECT_READ(copy, FILE_CONTENT);
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup2Error) {
|
||||
File f = open_file();
|
||||
EXPECT_SYSTEM_ERROR_NOASSERT(f.dup2(-1), EBADF,
|
||||
fmt::format("cannot duplicate file descriptor {} to -1", f.descriptor()));
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup2NoExcept) {
|
||||
File f = open_file();
|
||||
File copy = open_file();
|
||||
ErrorCode ec;
|
||||
f.dup2(copy.descriptor(), ec);
|
||||
EXPECT_EQ(0, ec.get());
|
||||
EXPECT_NE(f.descriptor(), copy.descriptor());
|
||||
EXPECT_READ(copy, FILE_CONTENT);
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup2NoExceptError) {
|
||||
File f = open_file();
|
||||
ErrorCode ec;
|
||||
SUPPRESS_ASSERT(f.dup2(-1, ec));
|
||||
EXPECT_EQ(EBADF, ec.get());
|
||||
}
|
||||
|
||||
TEST(FileTest, Pipe) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
EXPECT_NE(-1, read_end.descriptor());
|
||||
EXPECT_NE(-1, write_end.descriptor());
|
||||
write(write_end, "test");
|
||||
EXPECT_READ(read_end, "test");
|
||||
}
|
||||
|
||||
TEST(FileTest, Fdopen) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
int read_fd = read_end.descriptor();
|
||||
EXPECT_EQ(read_fd, FMT_POSIX(fileno(read_end.fdopen("r").get())));
|
||||
}
|
||||
|
||||
TEST(FileTest, FdopenError) {
|
||||
File f;
|
||||
EXPECT_SYSTEM_ERROR_NOASSERT(
|
||||
f.fdopen("r"), EBADF, "cannot associate stream with file descriptor");
|
||||
}
|
||||
|
||||
TEST(OutputRedirectTest, ScopedRedirect) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
{
|
||||
BufferedFile file(write_end.fdopen("w"));
|
||||
std::fprintf(file.get(), "[[[");
|
||||
{
|
||||
OutputRedirect redir(file.get());
|
||||
std::fprintf(file.get(), "censored");
|
||||
}
|
||||
std::fprintf(file.get(), "]]]");
|
||||
}
|
||||
EXPECT_READ(read_end, "[[[]]]");
|
||||
}
|
||||
|
||||
// Test that OutputRedirect handles errors in flush correctly.
|
||||
TEST(OutputRedirectTest, FlushErrorInCtor) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
int write_fd = write_end.descriptor();
|
||||
File write_copy = write_end.dup(write_fd);
|
||||
BufferedFile f = write_end.fdopen("w");
|
||||
// Put a character in a file buffer.
|
||||
EXPECT_EQ('x', fputc('x', f.get()));
|
||||
FMT_POSIX(close(write_fd));
|
||||
OutputRedirect *redir = 0;
|
||||
EXPECT_SYSTEM_ERROR_NOASSERT(redir = new OutputRedirect(f.get()),
|
||||
EBADF, "cannot flush stream");
|
||||
delete redir;
|
||||
write_copy.dup2(write_fd); // "undo" close or dtor will fail
|
||||
}
|
||||
|
||||
TEST(OutputRedirectTest, DupErrorInCtor) {
|
||||
BufferedFile f = open_buffered_file();
|
||||
int fd = f.fileno();
|
||||
File copy = File::dup(fd);
|
||||
FMT_POSIX(close(fd));
|
||||
OutputRedirect *redir = 0;
|
||||
EXPECT_SYSTEM_ERROR_NOASSERT(redir = new OutputRedirect(f.get()),
|
||||
EBADF, fmt::format("cannot duplicate file descriptor {}", fd));
|
||||
copy.dup2(fd); // "undo" close or dtor will fail
|
||||
delete redir;
|
||||
}
|
||||
|
||||
TEST(OutputRedirectTest, RestoreAndRead) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
BufferedFile file(write_end.fdopen("w"));
|
||||
std::fprintf(file.get(), "[[[");
|
||||
OutputRedirect redir(file.get());
|
||||
std::fprintf(file.get(), "censored");
|
||||
EXPECT_EQ("censored", redir.restore_and_read());
|
||||
EXPECT_EQ("", redir.restore_and_read());
|
||||
std::fprintf(file.get(), "]]]");
|
||||
file = BufferedFile();
|
||||
EXPECT_READ(read_end, "[[[]]]");
|
||||
}
|
||||
|
||||
// Test that OutputRedirect handles errors in flush correctly.
|
||||
TEST(OutputRedirectTest, FlushErrorInRestoreAndRead) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
int write_fd = write_end.descriptor();
|
||||
File write_copy = write_end.dup(write_fd);
|
||||
BufferedFile f = write_end.fdopen("w");
|
||||
OutputRedirect redir(f.get());
|
||||
// Put a character in a file buffer.
|
||||
EXPECT_EQ('x', fputc('x', f.get()));
|
||||
FMT_POSIX(close(write_fd));
|
||||
EXPECT_SYSTEM_ERROR_NOASSERT(redir.restore_and_read(),
|
||||
EBADF, "cannot flush stream");
|
||||
write_copy.dup2(write_fd); // "undo" close or dtor will fail
|
||||
}
|
||||
|
||||
TEST(OutputRedirectTest, ErrorInDtor) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
int write_fd = write_end.descriptor();
|
||||
File write_copy = write_end.dup(write_fd);
|
||||
BufferedFile f = write_end.fdopen("w");
|
||||
OutputRedirect *redir = new OutputRedirect(f.get());
|
||||
// Put a character in a file buffer.
|
||||
EXPECT_EQ('x', fputc('x', f.get()));
|
||||
EXPECT_WRITE(stderr, {
|
||||
// The close function must be called inside EXPECT_WRITE, otherwise
|
||||
// the system may recycle closed file descriptor when redirecting the
|
||||
// output in EXPECT_STDERR and the second close will break output
|
||||
// redirection.
|
||||
FMT_POSIX(close(write_fd));
|
||||
SUPPRESS_ASSERT(delete redir);
|
||||
}, format_system_error(EBADF, "cannot flush stream"));
|
||||
write_copy.dup2(write_fd); // "undo" close or dtor of BufferedFile will fail
|
||||
}
|
||||
|
||||
#endif // FMT_USE_FILE_DESCRIPTORS
|
||||
|
||||
} // namespace
|
||||
98
test/gtest-extra.cc
Normal file
98
test/gtest-extra.cc
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
Custom Google Test assertions.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "gtest-extra.h"
|
||||
|
||||
#if FMT_USE_FILE_DESCRIPTORS
|
||||
|
||||
using fmt::File;
|
||||
|
||||
void OutputRedirect::flush() {
|
||||
#if EOF != -1
|
||||
# error "FMT_RETRY assumes return value of -1 indicating failure"
|
||||
#endif
|
||||
int result = 0;
|
||||
FMT_RETRY(result, fflush(file_));
|
||||
if (result != 0)
|
||||
throw fmt::SystemError(errno, "cannot flush stream");
|
||||
}
|
||||
|
||||
void OutputRedirect::restore() {
|
||||
if (original_.descriptor() == -1)
|
||||
return; // Already restored.
|
||||
flush();
|
||||
// Restore the original file.
|
||||
original_.dup2(FMT_POSIX(fileno(file_)));
|
||||
original_.close();
|
||||
}
|
||||
|
||||
OutputRedirect::OutputRedirect(FILE *file) : file_(file) {
|
||||
flush();
|
||||
int fd = FMT_POSIX(fileno(file));
|
||||
// Create a File object referring to the original file.
|
||||
original_ = File::dup(fd);
|
||||
// Create a pipe.
|
||||
File write_end;
|
||||
File::pipe(read_end_, write_end);
|
||||
// Connect the passed FILE object to the write end of the pipe.
|
||||
write_end.dup2(fd);
|
||||
}
|
||||
|
||||
OutputRedirect::~OutputRedirect() FMT_NOEXCEPT {
|
||||
try {
|
||||
restore();
|
||||
} catch (const std::exception &e) {
|
||||
std::fputs(e.what(), stderr);
|
||||
}
|
||||
}
|
||||
|
||||
std::string OutputRedirect::restore_and_read() {
|
||||
// Restore output.
|
||||
restore();
|
||||
|
||||
// Read everything from the pipe.
|
||||
std::string content;
|
||||
if (read_end_.descriptor() == -1)
|
||||
return content; // Already read.
|
||||
enum { BUFFER_SIZE = 4096 };
|
||||
char buffer[BUFFER_SIZE];
|
||||
std::streamsize count = 0;
|
||||
do {
|
||||
count = read_end_.read(buffer, BUFFER_SIZE);
|
||||
content.append(buffer, static_cast<std::size_t>(count));
|
||||
} while (count != 0);
|
||||
read_end_.close();
|
||||
return content;
|
||||
}
|
||||
|
||||
#endif // FMT_USE_FILE_DESCRIPTORS
|
||||
|
||||
std::string format_system_error(int error_code, fmt::StringRef message) {
|
||||
fmt::MemoryWriter out;
|
||||
fmt::internal::format_system_error(out, error_code, message);
|
||||
return out.str();
|
||||
}
|
||||
138
test/gtest-extra.h
Normal file
138
test/gtest-extra.h
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
Custom Google Test assertions.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef FMT_GTEST_EXTRA_H_
|
||||
#define FMT_GTEST_EXTRA_H_
|
||||
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "format.h"
|
||||
|
||||
#ifndef FMT_USE_FILE_DESCRIPTORS
|
||||
# define FMT_USE_FILE_DESCRIPTORS 0
|
||||
#endif
|
||||
|
||||
#if FMT_USE_FILE_DESCRIPTORS
|
||||
# include "posix.h"
|
||||
#endif
|
||||
|
||||
#define FMT_TEST_THROW_(statement, expected_exception, expected_message, fail) \
|
||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||
if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
|
||||
std::string gtest_expected_message = expected_message; \
|
||||
bool gtest_caught_expected = false; \
|
||||
try { \
|
||||
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
|
||||
} \
|
||||
catch (expected_exception const& e) { \
|
||||
if (gtest_expected_message != e.what()) { \
|
||||
gtest_ar \
|
||||
<< #statement " throws an exception with a different message.\n" \
|
||||
<< "Expected: " << gtest_expected_message << "\n" \
|
||||
<< " Actual: " << e.what(); \
|
||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||
} \
|
||||
gtest_caught_expected = true; \
|
||||
} \
|
||||
catch (...) { \
|
||||
gtest_ar << \
|
||||
"Expected: " #statement " throws an exception of type " \
|
||||
#expected_exception ".\n Actual: it throws a different type."; \
|
||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||
} \
|
||||
if (!gtest_caught_expected) { \
|
||||
gtest_ar << \
|
||||
"Expected: " #statement " throws an exception of type " \
|
||||
#expected_exception ".\n Actual: it throws nothing."; \
|
||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||
} \
|
||||
} else \
|
||||
GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
|
||||
fail(gtest_ar.failure_message())
|
||||
|
||||
// Tests that the statement throws the expected exception and the exception's
|
||||
// what() method returns expected message.
|
||||
#define EXPECT_THROW_MSG(statement, expected_exception, expected_message) \
|
||||
FMT_TEST_THROW_(statement, expected_exception, \
|
||||
expected_message, GTEST_NONFATAL_FAILURE_)
|
||||
|
||||
std::string format_system_error(int error_code, fmt::StringRef message);
|
||||
|
||||
#define EXPECT_SYSTEM_ERROR(statement, error_code, message) \
|
||||
EXPECT_THROW_MSG(statement, fmt::SystemError, \
|
||||
format_system_error(error_code, message))
|
||||
|
||||
#if FMT_USE_FILE_DESCRIPTORS
|
||||
|
||||
// Captures file output by redirecting it to a pipe.
|
||||
// The output it can handle is limited by the pipe capacity.
|
||||
class OutputRedirect {
|
||||
private:
|
||||
FILE *file_;
|
||||
fmt::File original_; // Original file passed to redirector.
|
||||
fmt::File read_end_; // Read end of the pipe where the output is redirected.
|
||||
|
||||
GTEST_DISALLOW_COPY_AND_ASSIGN_(OutputRedirect);
|
||||
|
||||
void flush();
|
||||
void restore();
|
||||
|
||||
public:
|
||||
explicit OutputRedirect(FILE *file);
|
||||
~OutputRedirect() FMT_NOEXCEPT;
|
||||
|
||||
// Restores the original file, reads output from the pipe into a string
|
||||
// and returns it.
|
||||
std::string restore_and_read();
|
||||
};
|
||||
|
||||
#define FMT_TEST_WRITE_(statement, expected_output, file, fail) \
|
||||
GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
|
||||
if (::testing::AssertionResult gtest_ar = ::testing::AssertionSuccess()) { \
|
||||
std::string gtest_expected_output = expected_output; \
|
||||
OutputRedirect gtest_redir(file); \
|
||||
GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
|
||||
std::string gtest_output = gtest_redir.restore_and_read(); \
|
||||
if (gtest_output != gtest_expected_output) { \
|
||||
gtest_ar \
|
||||
<< #statement " produces different output.\n" \
|
||||
<< "Expected: " << gtest_expected_output << "\n" \
|
||||
<< " Actual: " << gtest_output; \
|
||||
goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
|
||||
} \
|
||||
} else \
|
||||
GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
|
||||
fail(gtest_ar.failure_message())
|
||||
|
||||
// Tests that the statement writes the expected output to file.
|
||||
#define EXPECT_WRITE(file, statement, expected_output) \
|
||||
FMT_TEST_WRITE_(statement, expected_output, file, GTEST_NONFATAL_FAILURE_)
|
||||
|
||||
#endif // FMT_USE_FILE_DESCRIPTORS
|
||||
|
||||
#endif // FMT_GTEST_EXTRA_H_
|
||||
28
test/header-only-test.cc
Normal file
28
test/header-only-test.cc
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Header-only configuration test
|
||||
|
||||
Copyright (c) 2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "format.h"
|
||||
28
test/header-only-test2.cc
Normal file
28
test/header-only-test2.cc
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Additional translation unit for the header-only configuration test
|
||||
|
||||
Copyright (c) 2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "format.h"
|
||||
113
test/macro-test.cc
Normal file
113
test/macro-test.cc
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
Tests of variadic function emulation macros.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <utility>
|
||||
#include <gtest/gtest.h>
|
||||
#include "format.h"
|
||||
|
||||
#define IDENTITY(x) x
|
||||
|
||||
TEST(UtilTest, Gen) {
|
||||
int values[] = {FMT_GEN(10, IDENTITY)};
|
||||
for (int i = 0; i < 10; ++i)
|
||||
EXPECT_EQ(i, values[i]);
|
||||
}
|
||||
|
||||
#define MAKE_PAIR(x, y) std::make_pair(x, y)
|
||||
|
||||
TEST(UtilTest, ForEach) {
|
||||
std::pair<char, int> values[] = {
|
||||
FMT_FOR_EACH10(MAKE_PAIR, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
|
||||
};
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
EXPECT_EQ('a' + i, values[i].first);
|
||||
EXPECT_EQ(i, values[i].second);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UtilTest, NArg) {
|
||||
EXPECT_EQ(1, FMT_NARG(a));
|
||||
EXPECT_EQ(2, FMT_NARG(a, b));
|
||||
EXPECT_EQ(3, FMT_NARG(a, b, c));
|
||||
EXPECT_EQ(4, FMT_NARG(a, b, c, d));
|
||||
EXPECT_EQ(5, FMT_NARG(a, b, c, d, e));
|
||||
EXPECT_EQ(6, FMT_NARG(a, b, c, d, e, f));
|
||||
EXPECT_EQ(7, FMT_NARG(a, b, c, d, e, f, g));
|
||||
EXPECT_EQ(8, FMT_NARG(a, b, c, d, e, f, g, h));
|
||||
EXPECT_EQ(9, FMT_NARG(a, b, c, d, e, f, g, h, i));
|
||||
EXPECT_EQ(10, FMT_NARG(a, b, c, d, e, f, g, h, i, j));
|
||||
}
|
||||
|
||||
int result;
|
||||
|
||||
#define MAKE_TEST(func) \
|
||||
void func(const char *format, const fmt::ArgList &args) { \
|
||||
result = 0; \
|
||||
for (unsigned i = 0; args[i].type; ++i) \
|
||||
result += args[i].int_value; \
|
||||
}
|
||||
|
||||
MAKE_TEST(test_func)
|
||||
|
||||
typedef char Char;
|
||||
FMT_WRAP1(test_func, const char *, 1)
|
||||
|
||||
TEST(UtilTest, Wrap1) {
|
||||
result = 0;
|
||||
test_func("", 42);
|
||||
EXPECT_EQ(42, result);
|
||||
}
|
||||
|
||||
MAKE_TEST(test_variadic_void)
|
||||
FMT_VARIADIC_VOID(test_variadic_void, const char *)
|
||||
|
||||
TEST(UtilTest, VariadicVoid) {
|
||||
result = 0;
|
||||
test_variadic_void("", 10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
|
||||
EXPECT_EQ(550, result);
|
||||
}
|
||||
|
||||
template <int>
|
||||
struct S {};
|
||||
|
||||
#define GET_TYPE(n) S<n>
|
||||
|
||||
int test_variadic(FMT_GEN(10, GET_TYPE), const fmt::ArgList &args) { \
|
||||
int result = 0; \
|
||||
for (std::size_t i = 0; args[i].type; ++i) \
|
||||
result += args[i].int_value; \
|
||||
return result;
|
||||
}
|
||||
FMT_VARIADIC(int, test_variadic,
|
||||
S<0>, S<1>, S<2>, S<3>, S<4>, S<5>, S<6>, S<7>, S<8>, S<9>)
|
||||
|
||||
#define MAKE_ARG(n) S<n>()
|
||||
|
||||
TEST(UtilTest, Variadic) {
|
||||
EXPECT_EQ(550, test_variadic(FMT_GEN(10, MAKE_ARG),
|
||||
10, 20, 30, 40, 50, 60, 70, 80, 90, 100));
|
||||
}
|
||||
85
test/mock-allocator.h
Normal file
85
test/mock-allocator.h
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
Mock allocator.
|
||||
|
||||
Copyright (c) 2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef FMT_MOCK_ALLOCATOR_H_
|
||||
#define FMT_MOCK_ALLOCATOR_H_
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
template <typename T>
|
||||
class MockAllocator {
|
||||
public:
|
||||
MockAllocator() {}
|
||||
MockAllocator(const MockAllocator &) {}
|
||||
typedef T value_type;
|
||||
MOCK_METHOD1_T(allocate, T* (std::size_t n));
|
||||
MOCK_METHOD2_T(deallocate, void (T* p, std::size_t n));
|
||||
};
|
||||
|
||||
template <typename Allocator>
|
||||
class AllocatorRef {
|
||||
private:
|
||||
Allocator *alloc_;
|
||||
|
||||
public:
|
||||
typedef typename Allocator::value_type value_type;
|
||||
|
||||
explicit AllocatorRef(Allocator *alloc = 0) : alloc_(alloc) {}
|
||||
|
||||
AllocatorRef(const AllocatorRef &other) : alloc_(other.alloc_) {}
|
||||
|
||||
AllocatorRef& operator=(const AllocatorRef &other) {
|
||||
alloc_ = other.alloc_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if FMT_USE_RVALUE_REFERENCES
|
||||
private:
|
||||
void move(AllocatorRef &other) {
|
||||
alloc_ = other.alloc_;
|
||||
other.alloc_ = 0;
|
||||
}
|
||||
|
||||
public:
|
||||
AllocatorRef(AllocatorRef &&other) {
|
||||
move(other);
|
||||
}
|
||||
|
||||
AllocatorRef& operator=(AllocatorRef &&other) {
|
||||
assert(this != &other);
|
||||
move(other);
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
Allocator *get() const { return alloc_; }
|
||||
|
||||
value_type* allocate(std::size_t n) { return alloc_->allocate(n); }
|
||||
void deallocate(value_type* p, std::size_t n) { alloc_->deallocate(p, n); }
|
||||
};
|
||||
|
||||
#endif // FMT_MOCK_ALLOCATOR_H_
|
||||
437
test/posix-test.cc
Normal file
437
test/posix-test.cc
Normal file
@@ -0,0 +1,437 @@
|
||||
/*
|
||||
Test wrappers around POSIX functions.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
// Disable bogus MSVC warnings.
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include "posix-test.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <climits>
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <io.h>
|
||||
# undef max
|
||||
# undef ERROR
|
||||
#endif
|
||||
|
||||
#include "gtest-extra.h"
|
||||
|
||||
using fmt::BufferedFile;
|
||||
using fmt::ErrorCode;
|
||||
using fmt::File;
|
||||
|
||||
namespace {
|
||||
int open_count;
|
||||
int close_count;
|
||||
int dup_count;
|
||||
int dup2_count;
|
||||
int fdopen_count;
|
||||
int read_count;
|
||||
int write_count;
|
||||
int pipe_count;
|
||||
int fopen_count;
|
||||
int fclose_count;
|
||||
int fileno_count;
|
||||
std::size_t read_nbyte;
|
||||
std::size_t write_nbyte;
|
||||
bool sysconf_error;
|
||||
|
||||
enum FStatSimulation { NONE, MAX_SIZE, ERROR } fstat_sim;
|
||||
}
|
||||
|
||||
#define EMULATE_EINTR(func, error_result) \
|
||||
if (func##_count != 0) { \
|
||||
if (func##_count++ != 3) { \
|
||||
errno = EINTR; \
|
||||
return error_result; \
|
||||
} \
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
int test::open(const char *path, int oflag, int mode) {
|
||||
EMULATE_EINTR(open, -1);
|
||||
return ::open(path, oflag, mode);
|
||||
}
|
||||
|
||||
static off_t max_file_size() { return std::numeric_limits<off_t>::max(); }
|
||||
|
||||
int test::fstat(int fd, struct stat *buf) {
|
||||
int result = ::fstat(fd, buf);
|
||||
if (fstat_sim == MAX_SIZE)
|
||||
buf->st_size = max_file_size();
|
||||
return result;
|
||||
}
|
||||
|
||||
long test::sysconf(int name) {
|
||||
long result = ::sysconf(name);
|
||||
if (!sysconf_error)
|
||||
return result;
|
||||
// Simulate an error.
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
errno_t test::sopen_s(
|
||||
int* pfh, const char *filename, int oflag, int shflag, int pmode) {
|
||||
EMULATE_EINTR(open, EINTR);
|
||||
return _sopen_s(pfh, filename, oflag, shflag, pmode);
|
||||
}
|
||||
|
||||
static LONGLONG max_file_size() { return std::numeric_limits<LONGLONG>::max(); }
|
||||
|
||||
BOOL test::GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize) {
|
||||
if (fstat_sim == ERROR) {
|
||||
SetLastError(ERROR_ACCESS_DENIED);
|
||||
return FALSE;
|
||||
}
|
||||
BOOL result = ::GetFileSizeEx(hFile, lpFileSize);
|
||||
if (fstat_sim == MAX_SIZE)
|
||||
lpFileSize->QuadPart = max_file_size();
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
int test::close(int fildes) {
|
||||
// Close the file first because close shouldn't be retried.
|
||||
int result = ::FMT_POSIX(close(fildes));
|
||||
EMULATE_EINTR(close, -1);
|
||||
return result;
|
||||
}
|
||||
|
||||
int test::dup(int fildes) {
|
||||
EMULATE_EINTR(dup, -1);
|
||||
return ::FMT_POSIX(dup(fildes));
|
||||
}
|
||||
|
||||
int test::dup2(int fildes, int fildes2) {
|
||||
EMULATE_EINTR(dup2, -1);
|
||||
return ::FMT_POSIX(dup2(fildes, fildes2));
|
||||
}
|
||||
|
||||
FILE *test::fdopen(int fildes, const char *mode) {
|
||||
EMULATE_EINTR(fdopen, 0);
|
||||
return ::FMT_POSIX(fdopen(fildes, mode));
|
||||
}
|
||||
|
||||
test::ssize_t test::read(int fildes, void *buf, test::size_t nbyte) {
|
||||
read_nbyte = nbyte;
|
||||
EMULATE_EINTR(read, -1);
|
||||
return ::FMT_POSIX(read(fildes, buf, nbyte));
|
||||
}
|
||||
|
||||
test::ssize_t test::write(int fildes, const void *buf, test::size_t nbyte) {
|
||||
write_nbyte = nbyte;
|
||||
EMULATE_EINTR(write, -1);
|
||||
return ::FMT_POSIX(write(fildes, buf, nbyte));
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
int test::pipe(int fildes[2]) {
|
||||
EMULATE_EINTR(pipe, -1);
|
||||
return ::pipe(fildes);
|
||||
}
|
||||
#else
|
||||
int test::pipe(int *pfds, unsigned psize, int textmode) {
|
||||
EMULATE_EINTR(pipe, -1);
|
||||
return _pipe(pfds, psize, textmode);
|
||||
}
|
||||
#endif
|
||||
|
||||
FILE *test::fopen(const char *filename, const char *mode) {
|
||||
EMULATE_EINTR(fopen, 0);
|
||||
return ::fopen(filename, mode);
|
||||
}
|
||||
|
||||
int test::fclose(FILE *stream) {
|
||||
EMULATE_EINTR(fclose, EOF);
|
||||
return ::fclose(stream);
|
||||
}
|
||||
|
||||
int test::fileno(FILE *stream) {
|
||||
EMULATE_EINTR(fileno, -1);
|
||||
return ::FMT_POSIX(fileno(stream));
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
# define EXPECT_RETRY(statement, func, message) \
|
||||
func##_count = 1; \
|
||||
statement; \
|
||||
EXPECT_EQ(4, func##_count); \
|
||||
func##_count = 0;
|
||||
# define EXPECT_EQ_POSIX(expected, actual) EXPECT_EQ(expected, actual)
|
||||
#else
|
||||
# define EXPECT_RETRY(statement, func, message) \
|
||||
func##_count = 1; \
|
||||
EXPECT_SYSTEM_ERROR(statement, EINTR, message); \
|
||||
func##_count = 0;
|
||||
# define EXPECT_EQ_POSIX(expected, actual)
|
||||
#endif
|
||||
|
||||
void write_file(fmt::StringRef filename, fmt::StringRef content) {
|
||||
fmt::BufferedFile f(filename, "w");
|
||||
f.print("{}", content);
|
||||
}
|
||||
|
||||
TEST(UtilTest, StaticAssert) {
|
||||
FMT_STATIC_ASSERT(true, "success");
|
||||
// Static assertion failure is tested in compile-test because it causes
|
||||
// a compile-time error.
|
||||
}
|
||||
|
||||
TEST(UtilTest, GetPageSize) {
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO si = {};
|
||||
GetSystemInfo(&si);
|
||||
EXPECT_EQ(si.dwPageSize, fmt::getpagesize());
|
||||
#else
|
||||
EXPECT_EQ(sysconf(_SC_PAGESIZE), fmt::getpagesize());
|
||||
sysconf_error = true;
|
||||
EXPECT_SYSTEM_ERROR(
|
||||
fmt::getpagesize(), EINVAL, "cannot get memory page size");
|
||||
sysconf_error = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(FileTest, OpenRetry) {
|
||||
write_file("test", "there must be something here");
|
||||
File *f = 0;
|
||||
EXPECT_RETRY(f = new File("test", File::RDONLY),
|
||||
open, "cannot open file test");
|
||||
#ifndef _WIN32
|
||||
char c = 0;
|
||||
f->read(&c, 1);
|
||||
#endif
|
||||
delete f;
|
||||
}
|
||||
|
||||
TEST(FileTest, CloseNoRetryInDtor) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
File *f = new File(std::move(read_end));
|
||||
int saved_close_count = 0;
|
||||
EXPECT_WRITE(stderr, {
|
||||
close_count = 1;
|
||||
delete f;
|
||||
saved_close_count = close_count;
|
||||
close_count = 0;
|
||||
}, format_system_error(EINTR, "cannot close file") + "\n");
|
||||
EXPECT_EQ(2, saved_close_count);
|
||||
}
|
||||
|
||||
TEST(FileTest, CloseNoRetry) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
close_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(read_end.close(), EINTR, "cannot close file");
|
||||
EXPECT_EQ(2, close_count);
|
||||
close_count = 0;
|
||||
}
|
||||
|
||||
TEST(FileTest, Size) {
|
||||
std::string content = "top secret, destroy before reading";
|
||||
write_file("test", content);
|
||||
File f("test", File::RDONLY);
|
||||
EXPECT_GE(f.size(), 0);
|
||||
fmt::ULongLong file_size = f.size();
|
||||
EXPECT_EQ(content.size(), file_size);
|
||||
#ifdef _WIN32
|
||||
fmt::MemoryWriter message;
|
||||
fmt::internal::format_windows_error(
|
||||
message, ERROR_ACCESS_DENIED, "cannot get file size");
|
||||
fstat_sim = ERROR;
|
||||
EXPECT_THROW_MSG(f.size(), fmt::WindowsError, message.str());
|
||||
fstat_sim = NONE;
|
||||
#else
|
||||
f.close();
|
||||
EXPECT_SYSTEM_ERROR(f.size(), EBADF, "cannot get file attributes");
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(FileTest, MaxSize) {
|
||||
write_file("test", "");
|
||||
File f("test", File::RDONLY);
|
||||
fstat_sim = MAX_SIZE;
|
||||
EXPECT_GE(f.size(), 0);
|
||||
EXPECT_EQ(max_file_size(), f.size());
|
||||
fstat_sim = NONE;
|
||||
}
|
||||
|
||||
TEST(FileTest, ReadRetry) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
enum { SIZE = 4 };
|
||||
write_end.write("test", SIZE);
|
||||
write_end.close();
|
||||
char buffer[SIZE];
|
||||
std::streamsize count = 0;
|
||||
EXPECT_RETRY(count = read_end.read(buffer, SIZE),
|
||||
read, "cannot read from file");
|
||||
EXPECT_EQ_POSIX(static_cast<std::streamsize>(SIZE), count);
|
||||
}
|
||||
|
||||
TEST(FileTest, WriteRetry) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
enum { SIZE = 4 };
|
||||
std::streamsize count = 0;
|
||||
EXPECT_RETRY(count = write_end.write("test", SIZE),
|
||||
write, "cannot write to file");
|
||||
write_end.close();
|
||||
#ifndef _WIN32
|
||||
EXPECT_EQ(static_cast<std::streamsize>(SIZE), count);
|
||||
char buffer[SIZE + 1];
|
||||
read_end.read(buffer, SIZE);
|
||||
buffer[SIZE] = '\0';
|
||||
EXPECT_STREQ("test", buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
TEST(FileTest, ConvertReadCount) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
char c;
|
||||
std::size_t size = UINT_MAX;
|
||||
if (sizeof(unsigned) != sizeof(std::size_t))
|
||||
++size;
|
||||
read_count = 1;
|
||||
read_nbyte = 0;
|
||||
EXPECT_THROW(read_end.read(&c, size), fmt::SystemError);
|
||||
read_count = 0;
|
||||
EXPECT_EQ(UINT_MAX, read_nbyte);
|
||||
}
|
||||
|
||||
TEST(FileTest, ConvertWriteCount) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
char c;
|
||||
std::size_t size = UINT_MAX;
|
||||
if (sizeof(unsigned) != sizeof(std::size_t))
|
||||
++size;
|
||||
write_count = 1;
|
||||
write_nbyte = 0;
|
||||
EXPECT_THROW(write_end.write(&c, size), fmt::SystemError);
|
||||
write_count = 0;
|
||||
EXPECT_EQ(UINT_MAX, write_nbyte);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(FileTest, DupNoRetry) {
|
||||
int stdout_fd = FMT_POSIX(fileno(stdout));
|
||||
dup_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(File::dup(stdout_fd), EINTR,
|
||||
fmt::format("cannot duplicate file descriptor {}", stdout_fd));
|
||||
dup_count = 0;
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup2Retry) {
|
||||
int stdout_fd = FMT_POSIX(fileno(stdout));
|
||||
File f1 = File::dup(stdout_fd), f2 = File::dup(stdout_fd);
|
||||
EXPECT_RETRY(f1.dup2(f2.descriptor()), dup2,
|
||||
fmt::format("cannot duplicate file descriptor {} to {}",
|
||||
f1.descriptor(), f2.descriptor()));
|
||||
}
|
||||
|
||||
TEST(FileTest, Dup2NoExceptRetry) {
|
||||
int stdout_fd = FMT_POSIX(fileno(stdout));
|
||||
File f1 = File::dup(stdout_fd), f2 = File::dup(stdout_fd);
|
||||
ErrorCode ec;
|
||||
dup2_count = 1;
|
||||
f1.dup2(f2.descriptor(), ec);
|
||||
#ifndef _WIN32
|
||||
EXPECT_EQ(4, dup2_count);
|
||||
#else
|
||||
EXPECT_EQ(EINTR, ec.get());
|
||||
#endif
|
||||
dup2_count = 0;
|
||||
}
|
||||
|
||||
TEST(FileTest, PipeNoRetry) {
|
||||
File read_end, write_end;
|
||||
pipe_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(
|
||||
File::pipe(read_end, write_end), EINTR, "cannot create pipe");
|
||||
pipe_count = 0;
|
||||
}
|
||||
|
||||
TEST(FileTest, FdopenNoRetry) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
fdopen_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(read_end.fdopen("r"),
|
||||
EINTR, "cannot associate stream with file descriptor");
|
||||
fdopen_count = 0;
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, OpenRetry) {
|
||||
write_file("test", "there must be something here");
|
||||
BufferedFile *f = 0;
|
||||
EXPECT_RETRY(f = new BufferedFile("test", "r"),
|
||||
fopen, "cannot open file test");
|
||||
#ifndef _WIN32
|
||||
char c = 0;
|
||||
if (fread(&c, 1, 1, f->get()) < 1)
|
||||
throw fmt::SystemError(errno, "fread failed");
|
||||
#endif
|
||||
delete f;
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, CloseNoRetryInDtor) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
BufferedFile *f = new BufferedFile(read_end.fdopen("r"));
|
||||
int saved_fclose_count = 0;
|
||||
EXPECT_WRITE(stderr, {
|
||||
fclose_count = 1;
|
||||
delete f;
|
||||
saved_fclose_count = fclose_count;
|
||||
fclose_count = 0;
|
||||
}, format_system_error(EINTR, "cannot close file") + "\n");
|
||||
EXPECT_EQ(2, saved_fclose_count);
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, CloseNoRetry) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
BufferedFile f = read_end.fdopen("r");
|
||||
fclose_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file");
|
||||
EXPECT_EQ(2, fclose_count);
|
||||
fclose_count = 0;
|
||||
}
|
||||
|
||||
TEST(BufferedFileTest, FilenoNoRetry) {
|
||||
File read_end, write_end;
|
||||
File::pipe(read_end, write_end);
|
||||
BufferedFile f = read_end.fdopen("r");
|
||||
fileno_count = 1;
|
||||
EXPECT_SYSTEM_ERROR(f.fileno(), EINTR, "cannot get file descriptor");
|
||||
EXPECT_EQ(2, fileno_count);
|
||||
fileno_count = 0;
|
||||
}
|
||||
81
test/posix-test.h
Normal file
81
test/posix-test.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
Test wrappers around POSIX functions.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef FMT_POSIX_TEST_H
|
||||
#define FMT_POSIX_TEST_H
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
struct stat;
|
||||
#else
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace test {
|
||||
|
||||
#ifndef _WIN32
|
||||
// Size type for read and write.
|
||||
typedef size_t size_t;
|
||||
typedef ssize_t ssize_t;
|
||||
int open(const char *path, int oflag, int mode);
|
||||
int fstat(int fd, struct stat *buf);
|
||||
long sysconf(int name);
|
||||
#else
|
||||
typedef unsigned size_t;
|
||||
typedef int ssize_t;
|
||||
errno_t sopen_s(
|
||||
int* pfh, const char *filename, int oflag, int shflag, int pmode);
|
||||
BOOL GetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize);
|
||||
#endif
|
||||
|
||||
int close(int fildes);
|
||||
|
||||
int dup(int fildes);
|
||||
int dup2(int fildes, int fildes2);
|
||||
|
||||
FILE *fdopen(int fildes, const char *mode);
|
||||
|
||||
ssize_t read(int fildes, void *buf, size_t nbyte);
|
||||
ssize_t write(int fildes, const void *buf, size_t nbyte);
|
||||
|
||||
#ifndef _WIN32
|
||||
int pipe(int fildes[2]);
|
||||
#else
|
||||
int pipe(int *pfds, unsigned psize, int textmode);
|
||||
#endif
|
||||
|
||||
FILE *fopen(const char *filename, const char *mode);
|
||||
int fclose(FILE *stream);
|
||||
int fileno(FILE *stream);
|
||||
|
||||
} // namespace test
|
||||
|
||||
#define FMT_SYSTEM(call) test::call
|
||||
|
||||
#endif // FMT_POSIX_TEST_H
|
||||
448
test/printf-test.cc
Normal file
448
test/printf-test.cc
Normal file
@@ -0,0 +1,448 @@
|
||||
/*
|
||||
printf tests.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <cctype>
|
||||
#include <climits>
|
||||
#include <cstring>
|
||||
|
||||
#include "format.h"
|
||||
#include "gtest-extra.h"
|
||||
#include "util.h"
|
||||
|
||||
using fmt::format;
|
||||
using fmt::FormatError;
|
||||
|
||||
const unsigned BIG_NUM = INT_MAX + 1u;
|
||||
|
||||
// Makes format string argument positional.
|
||||
std::string make_positional(fmt::StringRef format) {
|
||||
std::string s(format);
|
||||
s.replace(s.find('%'), 1, "%1$");
|
||||
return s;
|
||||
}
|
||||
|
||||
#define EXPECT_PRINTF(expected_output, format, arg) \
|
||||
EXPECT_EQ(expected_output, fmt::sprintf(format, arg)) \
|
||||
<< "format: " << format; \
|
||||
EXPECT_EQ(expected_output, fmt::sprintf(make_positional(format), arg))
|
||||
|
||||
TEST(PrintfTest, NoArgs) {
|
||||
EXPECT_EQ("test", fmt::sprintf("test"));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Escape) {
|
||||
EXPECT_EQ("%", fmt::sprintf("%%"));
|
||||
EXPECT_EQ("before %", fmt::sprintf("before %%"));
|
||||
EXPECT_EQ("% after", fmt::sprintf("%% after"));
|
||||
EXPECT_EQ("before % after", fmt::sprintf("before %% after"));
|
||||
EXPECT_EQ("%s", fmt::sprintf("%%s"));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, PositionalArgs) {
|
||||
EXPECT_EQ("42", fmt::sprintf("%1$d", 42));
|
||||
EXPECT_EQ("before 42", fmt::sprintf("before %1$d", 42));
|
||||
EXPECT_EQ("42 after", fmt::sprintf("%1$d after",42));
|
||||
EXPECT_EQ("before 42 after", fmt::sprintf("before %1$d after", 42));
|
||||
EXPECT_EQ("answer = 42", fmt::sprintf("%1$s = %2$d", "answer", 42));
|
||||
EXPECT_EQ("42 is the answer",
|
||||
fmt::sprintf("%2$d is the %1$s", "answer", 42));
|
||||
EXPECT_EQ("abracadabra", fmt::sprintf("%1$s%2$s%1$s", "abra", "cad"));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, AutomaticArgIndexing) {
|
||||
EXPECT_EQ("abc", fmt::sprintf("%c%c%c", 'a', 'b', 'c'));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, NumberIsTooBigInArgIndex) {
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%{}$", BIG_NUM)),
|
||||
FormatError, "number is too big");
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%{}$d", BIG_NUM)),
|
||||
FormatError, "number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, SwitchArgIndexing) {
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%1$d%", 1, 2),
|
||||
FormatError, "invalid format string");
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
|
||||
FormatError, "number is too big");
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%1$d%d", 1, 2),
|
||||
FormatError, "cannot switch from manual to automatic argument indexing");
|
||||
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%d%1$", 1, 2),
|
||||
FormatError, "invalid format string");
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%d%{}$d", BIG_NUM), 1, 2),
|
||||
FormatError, "number is too big");
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%d%1$d", 1, 2),
|
||||
FormatError, "cannot switch from automatic to manual argument indexing");
|
||||
|
||||
// Indexing errors override width errors.
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%d%1${}d", BIG_NUM), 1, 2),
|
||||
FormatError, "number is too big");
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%1$d%{}d", BIG_NUM), 1, 2),
|
||||
FormatError, "number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, InvalidArgIndex) {
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%0$d", 42), FormatError,
|
||||
"argument index out of range");
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%2$d", 42), FormatError,
|
||||
"argument index out of range");
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%{}$d", INT_MAX), 42),
|
||||
FormatError, "argument index out of range");
|
||||
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%2$", 42),
|
||||
FormatError, "invalid format string");
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%{}$d", BIG_NUM), 42),
|
||||
FormatError, "number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, DefaultAlignRight) {
|
||||
EXPECT_PRINTF(" 42", "%5d", 42);
|
||||
EXPECT_PRINTF(" abc", "%5s", "abc");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, ZeroFlag) {
|
||||
EXPECT_PRINTF("00042", "%05d", 42);
|
||||
EXPECT_PRINTF("-0042", "%05d", -42);
|
||||
|
||||
EXPECT_PRINTF("00042", "%05d", 42);
|
||||
EXPECT_PRINTF("-0042", "%05d", -42);
|
||||
EXPECT_PRINTF("-004.2", "%06g", -4.2);
|
||||
|
||||
EXPECT_PRINTF("+00042", "%00+6d", 42);
|
||||
|
||||
// '0' flag is ignored for non-numeric types.
|
||||
EXPECT_PRINTF(" x", "%05c", 'x');
|
||||
}
|
||||
|
||||
TEST(PrintfTest, PlusFlag) {
|
||||
EXPECT_PRINTF("+42", "%+d", 42);
|
||||
EXPECT_PRINTF("-42", "%+d", -42);
|
||||
EXPECT_PRINTF("+0042", "%+05d", 42);
|
||||
EXPECT_PRINTF("+0042", "%0++5d", 42);
|
||||
|
||||
// '+' flag is ignored for non-numeric types.
|
||||
EXPECT_PRINTF("x", "%+c", 'x');
|
||||
}
|
||||
|
||||
TEST(PrintfTest, MinusFlag) {
|
||||
EXPECT_PRINTF("abc ", "%-5s", "abc");
|
||||
EXPECT_PRINTF("abc ", "%0--5s", "abc");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, SpaceFlag) {
|
||||
EXPECT_PRINTF(" 42", "% d", 42);
|
||||
EXPECT_PRINTF("-42", "% d", -42);
|
||||
EXPECT_PRINTF(" 0042", "% 05d", 42);
|
||||
EXPECT_PRINTF(" 0042", "%0 5d", 42);
|
||||
|
||||
// ' ' flag is ignored for non-numeric types.
|
||||
EXPECT_PRINTF("x", "% c", 'x');
|
||||
}
|
||||
|
||||
TEST(PrintfTest, HashFlag) {
|
||||
EXPECT_PRINTF("042", "%#o", 042);
|
||||
EXPECT_PRINTF(fmt::format("0{:o}", static_cast<unsigned>(-042)), "%#o", -042);
|
||||
EXPECT_PRINTF("0", "%#o", 0);
|
||||
|
||||
EXPECT_PRINTF("0x42", "%#x", 0x42);
|
||||
EXPECT_PRINTF("0X42", "%#X", 0x42);
|
||||
EXPECT_PRINTF(
|
||||
fmt::format("0x{:x}", static_cast<unsigned>(-0x42)), "%#x", -0x42);
|
||||
EXPECT_PRINTF("0", "%#x", 0);
|
||||
|
||||
EXPECT_PRINTF("0x0042", "%#06x", 0x42);
|
||||
EXPECT_PRINTF("0x0042", "%0##6x", 0x42);
|
||||
|
||||
EXPECT_PRINTF("-42.000000", "%#f", -42.0);
|
||||
EXPECT_PRINTF("-42.000000", "%#F", -42.0);
|
||||
|
||||
char buffer[BUFFER_SIZE];
|
||||
safe_sprintf(buffer, "%#e", -42.0);
|
||||
EXPECT_PRINTF(buffer, "%#e", -42.0);
|
||||
safe_sprintf(buffer, "%#E", -42.0);
|
||||
EXPECT_PRINTF(buffer, "%#E", -42.0);
|
||||
|
||||
EXPECT_PRINTF("-42.0000", "%#g", -42.0);
|
||||
EXPECT_PRINTF("-42.0000", "%#G", -42.0);
|
||||
|
||||
safe_sprintf(buffer, "%#a", 16.0);
|
||||
EXPECT_PRINTF(buffer, "%#a", 16.0);
|
||||
safe_sprintf(buffer, "%#A", 16.0);
|
||||
EXPECT_PRINTF(buffer, "%#A", 16.0);
|
||||
|
||||
// '#' flag is ignored for non-numeric types.
|
||||
EXPECT_PRINTF("x", "%#c", 'x');
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Width) {
|
||||
EXPECT_PRINTF(" abc", "%5s", "abc");
|
||||
|
||||
// Width cannot be specified twice.
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%5-5d", 42), FormatError,
|
||||
"unknown format code '-' for integer");
|
||||
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%{}d", BIG_NUM), 42),
|
||||
FormatError, "number is too big");
|
||||
EXPECT_THROW_MSG(fmt::sprintf(format("%1${}d", BIG_NUM), 42),
|
||||
FormatError, "number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, DynamicWidth) {
|
||||
EXPECT_EQ(" 42", fmt::sprintf("%*d", 5, 42));
|
||||
EXPECT_EQ("42 ", fmt::sprintf("%*d", -5, 42));
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%*d", 5.0, 42), FormatError,
|
||||
"width is not integer");
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%*d"), FormatError,
|
||||
"argument index out of range");
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%*d", BIG_NUM, 42), FormatError,
|
||||
"number is too big");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, IntPrecision) {
|
||||
EXPECT_PRINTF("00042", "%.5d", 42);
|
||||
EXPECT_PRINTF("-00042", "%.5d", -42);
|
||||
EXPECT_PRINTF("00042", "%.5x", 0x42);
|
||||
EXPECT_PRINTF("0x00042", "%#.5x", 0x42);
|
||||
EXPECT_PRINTF("00042", "%.5o", 042);
|
||||
EXPECT_PRINTF("00042", "%#.5o", 042);
|
||||
|
||||
EXPECT_PRINTF(" 00042", "%7.5d", 42);
|
||||
EXPECT_PRINTF(" 00042", "%7.5x", 0x42);
|
||||
EXPECT_PRINTF(" 0x00042", "%#10.5x", 0x42);
|
||||
EXPECT_PRINTF(" 00042", "%7.5o", 042);
|
||||
EXPECT_PRINTF(" 00042", "%#10.5o", 042);
|
||||
|
||||
EXPECT_PRINTF("00042 ", "%-7.5d", 42);
|
||||
EXPECT_PRINTF("00042 ", "%-7.5x", 0x42);
|
||||
EXPECT_PRINTF("0x00042 ", "%-#10.5x", 0x42);
|
||||
EXPECT_PRINTF("00042 ", "%-7.5o", 042);
|
||||
EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, FloatPrecision) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
safe_sprintf(buffer, "%.3e", 1234.5678);
|
||||
EXPECT_PRINTF(buffer, "%.3e", 1234.5678);
|
||||
EXPECT_PRINTF("1234.568", "%.3f", 1234.5678);
|
||||
safe_sprintf(buffer, "%.3g", 1234.5678);
|
||||
EXPECT_PRINTF(buffer, "%.3g", 1234.5678);
|
||||
safe_sprintf(buffer, "%.3a", 1234.5678);
|
||||
EXPECT_PRINTF(buffer, "%.3a", 1234.5678);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, IgnorePrecisionForNonNumericArg) {
|
||||
EXPECT_PRINTF("abc", "%.5s", "abc");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, DynamicPrecision) {
|
||||
EXPECT_EQ("00042", fmt::sprintf("%.*d", 5, 42));
|
||||
EXPECT_EQ("42", fmt::sprintf("%.*d", -5, 42));
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%.*d", 5.0, 42), FormatError,
|
||||
"precision is not integer");
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%.*d"), FormatError,
|
||||
"argument index out of range");
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%.*d", BIG_NUM, 42), FormatError,
|
||||
"number is too big");
|
||||
if (sizeof(fmt::LongLong) != sizeof(int)) {
|
||||
fmt::LongLong prec = static_cast<fmt::LongLong>(INT_MIN) - 1;
|
||||
EXPECT_THROW_MSG(fmt::sprintf("%.*d", prec, 42), FormatError,
|
||||
"number is too big");
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct MakeSigned { typedef T Type; };
|
||||
|
||||
#define SPECIALIZE_MAKE_SIGNED(T, S) \
|
||||
template <> \
|
||||
struct MakeSigned<T> { typedef S Type; }
|
||||
|
||||
SPECIALIZE_MAKE_SIGNED(char, signed char);
|
||||
SPECIALIZE_MAKE_SIGNED(unsigned char, signed char);
|
||||
SPECIALIZE_MAKE_SIGNED(unsigned short, short);
|
||||
SPECIALIZE_MAKE_SIGNED(unsigned, int);
|
||||
SPECIALIZE_MAKE_SIGNED(unsigned long, long);
|
||||
SPECIALIZE_MAKE_SIGNED(fmt::ULongLong, fmt::LongLong);
|
||||
|
||||
template <typename T, typename U>
|
||||
void TestLength(const char *length_spec, U value) {
|
||||
fmt::LongLong signed_value = value;
|
||||
fmt::ULongLong unsigned_value = value;
|
||||
// Apply integer promotion to the argument.
|
||||
fmt::ULongLong max = std::numeric_limits<U>::max();
|
||||
if (max <= static_cast<unsigned>(std::numeric_limits<int>::max())) {
|
||||
signed_value = static_cast<int>(value);
|
||||
unsigned_value = static_cast<int>(value);
|
||||
} else if (max <= std::numeric_limits<unsigned>::max()) {
|
||||
signed_value = static_cast<unsigned>(value);
|
||||
unsigned_value = static_cast<unsigned>(value);
|
||||
}
|
||||
using fmt::internal::MakeUnsigned;
|
||||
if (sizeof(U) <= sizeof(int) && sizeof(int) < sizeof(T)) {
|
||||
signed_value = unsigned_value =
|
||||
static_cast<typename MakeUnsigned<unsigned>::Type>(value);
|
||||
} else {
|
||||
signed_value = static_cast<typename MakeSigned<T>::Type>(value);
|
||||
unsigned_value = static_cast<typename MakeUnsigned<T>::Type>(value);
|
||||
}
|
||||
std::ostringstream os;
|
||||
os << signed_value;
|
||||
EXPECT_PRINTF(os.str(), fmt::format("%{}d", length_spec), value);
|
||||
EXPECT_PRINTF(os.str(), fmt::format("%{}i", length_spec), value);
|
||||
os.str("");
|
||||
os << unsigned_value;
|
||||
EXPECT_PRINTF(os.str(), fmt::format("%{}u", length_spec), value);
|
||||
os.str("");
|
||||
os << std::oct << unsigned_value;
|
||||
EXPECT_PRINTF(os.str(), fmt::format("%{}o", length_spec), value);
|
||||
os.str("");
|
||||
os << std::hex << unsigned_value;
|
||||
EXPECT_PRINTF(os.str(), fmt::format("%{}x", length_spec), value);
|
||||
os.str("");
|
||||
os << std::hex << std::uppercase << unsigned_value;
|
||||
EXPECT_PRINTF(os.str(), fmt::format("%{}X", length_spec), value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void TestLength(const char *length_spec) {
|
||||
T min = std::numeric_limits<T>::min(), max = std::numeric_limits<T>::max();
|
||||
TestLength<T>(length_spec, 42);
|
||||
TestLength<T>(length_spec, -42);
|
||||
TestLength<T>(length_spec, min);
|
||||
TestLength<T>(length_spec, max);
|
||||
if (min >= 0 || static_cast<fmt::LongLong>(min) >
|
||||
std::numeric_limits<fmt::LongLong>::min()) {
|
||||
TestLength<T>(length_spec, fmt::LongLong(min) - 1);
|
||||
}
|
||||
fmt::ULongLong long_long_max = std::numeric_limits<fmt::LongLong>::max();
|
||||
if (max < 0 || static_cast<fmt::ULongLong>(max) < long_long_max)
|
||||
TestLength<T>(length_spec, fmt::LongLong(max) + 1);
|
||||
TestLength<T>(length_spec, std::numeric_limits<short>::min());
|
||||
TestLength<T>(length_spec, std::numeric_limits<unsigned short>::max());
|
||||
TestLength<T>(length_spec, std::numeric_limits<int>::min());
|
||||
TestLength<T>(length_spec, std::numeric_limits<int>::max());
|
||||
TestLength<T>(length_spec, std::numeric_limits<unsigned>::min());
|
||||
TestLength<T>(length_spec, std::numeric_limits<unsigned>::max());
|
||||
TestLength<T>(length_spec, std::numeric_limits<fmt::LongLong>::min());
|
||||
TestLength<T>(length_spec, std::numeric_limits<fmt::LongLong>::max());
|
||||
TestLength<T>(length_spec, std::numeric_limits<fmt::ULongLong>::min());
|
||||
TestLength<T>(length_spec, std::numeric_limits<fmt::ULongLong>::max());
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Length) {
|
||||
TestLength<char>("hh");
|
||||
TestLength<signed char>("hh");
|
||||
TestLength<unsigned char>("hh");
|
||||
TestLength<short>("h");
|
||||
TestLength<unsigned short>("h");
|
||||
TestLength<long>("l");
|
||||
TestLength<unsigned long>("l");
|
||||
TestLength<fmt::LongLong>("ll");
|
||||
TestLength<fmt::ULongLong>("ll");
|
||||
TestLength<intmax_t>("j");
|
||||
TestLength<std::size_t>("z");
|
||||
TestLength<std::ptrdiff_t>("t");
|
||||
long double max = std::numeric_limits<long double>::max();
|
||||
EXPECT_PRINTF(fmt::format("{}", max), "%g", max);
|
||||
EXPECT_PRINTF(fmt::format("{}", max), "%Lg", max);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Int) {
|
||||
EXPECT_PRINTF("-42", "%d", -42);
|
||||
EXPECT_PRINTF("-42", "%i", -42);
|
||||
unsigned u = -42;
|
||||
EXPECT_PRINTF(fmt::format("{}", u), "%u", -42);
|
||||
EXPECT_PRINTF(fmt::format("{:o}", u), "%o", -42);
|
||||
EXPECT_PRINTF(fmt::format("{:x}", u), "%x", -42);
|
||||
EXPECT_PRINTF(fmt::format("{:X}", u), "%X", -42);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Float) {
|
||||
EXPECT_PRINTF("392.650000", "%f", 392.65);
|
||||
EXPECT_PRINTF("392.650000", "%F", 392.65);
|
||||
char buffer[BUFFER_SIZE];
|
||||
safe_sprintf(buffer, "%e", 392.65);
|
||||
EXPECT_PRINTF(buffer, "%e", 392.65);
|
||||
safe_sprintf(buffer, "%E", 392.65);
|
||||
EXPECT_PRINTF(buffer, "%E", 392.65);
|
||||
EXPECT_PRINTF("392.65", "%g", 392.65);
|
||||
EXPECT_PRINTF("392.65", "%G", 392.65);
|
||||
safe_sprintf(buffer, "%a", -392.65);
|
||||
EXPECT_EQ(buffer, format("{:a}", -392.65));
|
||||
safe_sprintf(buffer, "%A", -392.65);
|
||||
EXPECT_EQ(buffer, format("{:A}", -392.65));
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Inf) {
|
||||
double inf = std::numeric_limits<double>::infinity();
|
||||
for (const char* type = "fega"; *type; ++type) {
|
||||
EXPECT_PRINTF("inf", fmt::format("%{}", *type), inf);
|
||||
char upper = std::toupper(*type);
|
||||
EXPECT_PRINTF("INF", fmt::format("%{}", upper), inf);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Char) {
|
||||
EXPECT_PRINTF("x", "%c", 'x');
|
||||
int max = std::numeric_limits<int>::max();
|
||||
EXPECT_PRINTF(fmt::format("{}", static_cast<char>(max)), "%c", max);
|
||||
//EXPECT_PRINTF("x", "%lc", L'x');
|
||||
// TODO: test wchar_t
|
||||
}
|
||||
|
||||
TEST(PrintfTest, String) {
|
||||
EXPECT_PRINTF("abc", "%s", "abc");
|
||||
// TODO: wide string
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Pointer) {
|
||||
int n;
|
||||
void *p = &n;
|
||||
EXPECT_PRINTF(fmt::format("{}", p), "%p", p);
|
||||
}
|
||||
|
||||
TEST(PrintfTest, Location) {
|
||||
// TODO: test %n
|
||||
}
|
||||
|
||||
#if FMT_USE_FILE_DESCRIPTORS
|
||||
TEST(PrintfTest, Examples) {
|
||||
const char *weekday = "Thursday";
|
||||
const char *month = "August";
|
||||
int day = 21;
|
||||
EXPECT_WRITE(stdout, fmt::printf("%1$s, %3$d %2$s", weekday, month, day),
|
||||
"Thursday, 21 August");
|
||||
}
|
||||
|
||||
TEST(PrintfTest, PrintfError) {
|
||||
fmt::File read_end, write_end;
|
||||
fmt::File::pipe(read_end, write_end);
|
||||
int result = fmt::fprintf(read_end.fdopen("r").get(), "test");
|
||||
EXPECT_LT(result, 0);
|
||||
}
|
||||
#endif
|
||||
50
test/test-main.cc
Normal file
50
test/test-main.cc
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
Tests of additional gtest macros.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
# include <crtdbg.h>
|
||||
#endif
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
#ifdef _WIN32
|
||||
// Don't display any error dialogs. This also suppresses message boxes
|
||||
// on assertion failures in MinGW where _set_error_mode/CrtSetReportMode
|
||||
// doesn't help.
|
||||
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
|
||||
SEM_NOOPENFILEERRORBOX);
|
||||
// Disable message boxes on assertion failures.
|
||||
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
|
||||
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
|
||||
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
|
||||
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
|
||||
#endif
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
834
test/util-test.cc
Normal file
834
test/util-test.cc
Normal file
@@ -0,0 +1,834 @@
|
||||
/*
|
||||
Utility tests.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <cfloat>
|
||||
#include <climits>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
|
||||
#if FMT_USE_TYPE_TRAITS
|
||||
# include <type_traits>
|
||||
#endif
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest-extra.h"
|
||||
#include "mock-allocator.h"
|
||||
#include "util.h"
|
||||
|
||||
// Check if format.h compiles with windows.h included.
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "format.h"
|
||||
|
||||
#undef max
|
||||
|
||||
using fmt::StringRef;
|
||||
using fmt::internal::Arg;
|
||||
using fmt::internal::Value;
|
||||
using fmt::internal::Buffer;
|
||||
using fmt::internal::MemoryBuffer;
|
||||
|
||||
using testing::Return;
|
||||
using testing::StrictMock;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Test {};
|
||||
template <typename Char>
|
||||
std::basic_ostream<Char> &operator<<(std::basic_ostream<Char> &os, Test) {
|
||||
return os << "test";
|
||||
}
|
||||
|
||||
template <typename Char, typename T>
|
||||
Arg make_arg(const T &value) {
|
||||
Arg arg = Arg();
|
||||
Value &arg_value = arg;
|
||||
arg_value = fmt::internal::MakeValue<Char>(value);
|
||||
arg.type = static_cast<Arg::Type>(
|
||||
fmt::internal::MakeValue<Char>::type(value));
|
||||
return arg;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void CheckForwarding(
|
||||
MockAllocator<int> &alloc, AllocatorRef< MockAllocator<int> > &ref) {
|
||||
int mem;
|
||||
// Check if value_type is properly defined.
|
||||
AllocatorRef< MockAllocator<int> >::value_type *ptr = &mem;
|
||||
// Check forwarding.
|
||||
EXPECT_CALL(alloc, allocate(42)).WillOnce(Return(ptr));
|
||||
ref.allocate(42);
|
||||
EXPECT_CALL(alloc, deallocate(ptr, 42));
|
||||
ref.deallocate(ptr, 42);
|
||||
}
|
||||
|
||||
TEST(AllocatorTest, AllocatorRef) {
|
||||
StrictMock< MockAllocator<int> > alloc;
|
||||
typedef AllocatorRef< MockAllocator<int> > TestAllocatorRef;
|
||||
TestAllocatorRef ref(&alloc);
|
||||
// Check if AllocatorRef forwards to the underlying allocator.
|
||||
CheckForwarding(alloc, ref);
|
||||
TestAllocatorRef ref2(ref);
|
||||
CheckForwarding(alloc, ref2);
|
||||
TestAllocatorRef ref3;
|
||||
EXPECT_EQ(0, ref3.get());
|
||||
ref3 = ref;
|
||||
CheckForwarding(alloc, ref3);
|
||||
}
|
||||
|
||||
#if FMT_USE_TYPE_TRAITS
|
||||
TEST(BufferTest, Noncopyable) {
|
||||
EXPECT_FALSE(std::is_copy_constructible<Buffer<char> >::value);
|
||||
EXPECT_FALSE(std::is_copy_assignable<Buffer<char> >::value);
|
||||
}
|
||||
|
||||
TEST(BufferTest, Nonmoveable) {
|
||||
EXPECT_FALSE(std::is_move_constructible<Buffer<char> >::value);
|
||||
EXPECT_FALSE(std::is_move_assignable<Buffer<char> >::value);
|
||||
}
|
||||
#endif
|
||||
|
||||
// A test buffer with a dummy grow method.
|
||||
template <typename T>
|
||||
struct TestBuffer : Buffer<T> {
|
||||
void grow(std::size_t size) { this->capacity_ = size; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct MockBuffer : Buffer<T> {
|
||||
MOCK_METHOD1(do_grow, void (std::size_t size));
|
||||
|
||||
void grow(std::size_t size) {
|
||||
this->capacity_ = size;
|
||||
do_grow(size);
|
||||
}
|
||||
|
||||
MockBuffer() {}
|
||||
MockBuffer(T *ptr) : Buffer<T>(ptr) {}
|
||||
MockBuffer(T *ptr, std::size_t capacity) : Buffer<T>(ptr, capacity) {}
|
||||
};
|
||||
|
||||
TEST(BufferTest, Ctor) {
|
||||
{
|
||||
MockBuffer<int> buffer;
|
||||
EXPECT_EQ(0, &buffer[0]);
|
||||
EXPECT_EQ(0u, buffer.size());
|
||||
EXPECT_EQ(0u, buffer.capacity());
|
||||
}
|
||||
{
|
||||
int dummy;
|
||||
MockBuffer<int> buffer(&dummy);
|
||||
EXPECT_EQ(&dummy, &buffer[0]);
|
||||
EXPECT_EQ(0u, buffer.size());
|
||||
EXPECT_EQ(0u, buffer.capacity());
|
||||
}
|
||||
{
|
||||
int dummy;
|
||||
std::size_t capacity = std::numeric_limits<std::size_t>::max();
|
||||
MockBuffer<int> buffer(&dummy, capacity);
|
||||
EXPECT_EQ(&dummy, &buffer[0]);
|
||||
EXPECT_EQ(0u, buffer.size());
|
||||
EXPECT_EQ(capacity, buffer.capacity());
|
||||
}
|
||||
}
|
||||
|
||||
struct DyingBuffer : TestBuffer<int> {
|
||||
MOCK_METHOD0(die, void());
|
||||
~DyingBuffer() { die(); }
|
||||
};
|
||||
|
||||
TEST(BufferTest, VirtualDtor) {
|
||||
typedef StrictMock<DyingBuffer> StictMockBuffer;
|
||||
StictMockBuffer *mock_buffer = new StictMockBuffer();
|
||||
EXPECT_CALL(*mock_buffer, die());
|
||||
Buffer<int> *buffer = mock_buffer;
|
||||
delete buffer;
|
||||
}
|
||||
|
||||
TEST(BufferTest, Access) {
|
||||
char data[10];
|
||||
MockBuffer<char> buffer(data, sizeof(data));
|
||||
buffer[0] = 11;
|
||||
EXPECT_EQ(11, buffer[0]);
|
||||
buffer[3] = 42;
|
||||
EXPECT_EQ(42, *(&buffer[0] + 3));
|
||||
const Buffer<char> &const_buffer = buffer;
|
||||
EXPECT_EQ(42, const_buffer[3]);
|
||||
}
|
||||
|
||||
TEST(BufferTest, Resize) {
|
||||
char data[123];
|
||||
MockBuffer<char> buffer(data, sizeof(data));
|
||||
buffer[10] = 42;
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
buffer.resize(20);
|
||||
EXPECT_EQ(20u, buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
buffer.resize(5);
|
||||
EXPECT_EQ(5u, buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
EXPECT_EQ(42, buffer[10]);
|
||||
// Check if resize calls grow.
|
||||
EXPECT_CALL(buffer, do_grow(124));
|
||||
buffer.resize(124);
|
||||
EXPECT_CALL(buffer, do_grow(200));
|
||||
buffer.resize(200);
|
||||
}
|
||||
|
||||
TEST(BufferTest, Clear) {
|
||||
TestBuffer<char> buffer;
|
||||
buffer.resize(20);
|
||||
buffer.clear();
|
||||
EXPECT_EQ(0u, buffer.size());
|
||||
EXPECT_EQ(20u, buffer.capacity());
|
||||
}
|
||||
|
||||
TEST(BufferTest, PushBack) {
|
||||
int data[15];
|
||||
MockBuffer<int> buffer(data, 10);
|
||||
buffer.push_back(11);
|
||||
EXPECT_EQ(11, buffer[0]);
|
||||
EXPECT_EQ(1u, buffer.size());
|
||||
buffer.resize(10);
|
||||
EXPECT_CALL(buffer, do_grow(11));
|
||||
buffer.push_back(22);
|
||||
EXPECT_EQ(22, buffer[10]);
|
||||
EXPECT_EQ(11u, buffer.size());
|
||||
}
|
||||
|
||||
TEST(BufferTest, Append) {
|
||||
char data[15];
|
||||
MockBuffer<char> buffer(data, 10);
|
||||
const char *test = "test";
|
||||
buffer.append(test, test + 5);
|
||||
EXPECT_STREQ(test, &buffer[0]);
|
||||
EXPECT_EQ(5u, buffer.size());
|
||||
buffer.resize(10);
|
||||
EXPECT_CALL(buffer, do_grow(12));
|
||||
buffer.append(test, test + 2);
|
||||
EXPECT_EQ('t', buffer[10]);
|
||||
EXPECT_EQ('e', buffer[11]);
|
||||
EXPECT_EQ(12u, buffer.size());
|
||||
}
|
||||
|
||||
TEST(BufferTest, AppendAllocatesEnoughStorage) {
|
||||
char data[19];
|
||||
MockBuffer<char> buffer(data, 10);
|
||||
const char *test = "abcdefgh";
|
||||
buffer.resize(10);
|
||||
EXPECT_CALL(buffer, do_grow(19));
|
||||
buffer.append(test, test + 9);
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, Ctor) {
|
||||
MemoryBuffer<char, 123> buffer;
|
||||
EXPECT_EQ(0u, buffer.size());
|
||||
EXPECT_EQ(123u, buffer.capacity());
|
||||
}
|
||||
|
||||
#if FMT_USE_RVALUE_REFERENCES
|
||||
|
||||
typedef AllocatorRef< std::allocator<char> > TestAllocator;
|
||||
|
||||
void check_move_buffer(const char *str,
|
||||
MemoryBuffer<char, 5, TestAllocator> &buffer) {
|
||||
std::allocator<char> *alloc = buffer.get_allocator().get();
|
||||
MemoryBuffer<char, 5, TestAllocator> buffer2(std::move(buffer));
|
||||
// Move shouldn't destroy the inline content of the first buffer.
|
||||
EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
|
||||
EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_EQ(5u, buffer2.capacity());
|
||||
// Move should transfer allocator.
|
||||
EXPECT_EQ(0, buffer.get_allocator().get());
|
||||
EXPECT_EQ(alloc, buffer2.get_allocator().get());
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, MoveCtor) {
|
||||
std::allocator<char> alloc;
|
||||
MemoryBuffer<char, 5, TestAllocator> buffer((TestAllocator(&alloc)));
|
||||
const char test[] = "test";
|
||||
buffer.append(test, test + 4);
|
||||
check_move_buffer("test", buffer);
|
||||
// Adding one more character fills the inline buffer, but doesn't cause
|
||||
// dynamic allocation.
|
||||
buffer.push_back('a');
|
||||
check_move_buffer("testa", buffer);
|
||||
const char *inline_buffer_ptr = &buffer[0];
|
||||
// Adding one more character causes the content to move from the inline to
|
||||
// a dynamically allocated buffer.
|
||||
buffer.push_back('b');
|
||||
MemoryBuffer<char, 5, TestAllocator> buffer2(std::move(buffer));
|
||||
// Move should rip the guts of the first buffer.
|
||||
EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
|
||||
EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_GT(buffer2.capacity(), 5u);
|
||||
}
|
||||
|
||||
void check_move_assign_buffer(const char *str, MemoryBuffer<char, 5> &buffer) {
|
||||
MemoryBuffer<char, 5> buffer2;
|
||||
buffer2 = std::move(buffer);
|
||||
// Move shouldn't destroy the inline content of the first buffer.
|
||||
EXPECT_EQ(str, std::string(&buffer[0], buffer.size()));
|
||||
EXPECT_EQ(str, std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_EQ(5u, buffer2.capacity());
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, MoveAssignment) {
|
||||
MemoryBuffer<char, 5> buffer;
|
||||
const char test[] = "test";
|
||||
buffer.append(test, test + 4);
|
||||
check_move_assign_buffer("test", buffer);
|
||||
// Adding one more character fills the inline buffer, but doesn't cause
|
||||
// dynamic allocation.
|
||||
buffer.push_back('a');
|
||||
check_move_assign_buffer("testa", buffer);
|
||||
const char *inline_buffer_ptr = &buffer[0];
|
||||
// Adding one more character causes the content to move from the inline to
|
||||
// a dynamically allocated buffer.
|
||||
buffer.push_back('b');
|
||||
MemoryBuffer<char, 5> buffer2;
|
||||
buffer2 = std::move(buffer);
|
||||
// Move should rip the guts of the first buffer.
|
||||
EXPECT_EQ(inline_buffer_ptr, &buffer[0]);
|
||||
EXPECT_EQ("testab", std::string(&buffer2[0], buffer2.size()));
|
||||
EXPECT_GT(buffer2.capacity(), 5u);
|
||||
}
|
||||
|
||||
#endif // FMT_USE_RVALUE_REFERENCES
|
||||
|
||||
TEST(MemoryBufferTest, Grow) {
|
||||
typedef AllocatorRef< MockAllocator<int> > Allocator;
|
||||
typedef MemoryBuffer<int, 10, Allocator> Base;
|
||||
MockAllocator<int> alloc;
|
||||
struct TestMemoryBuffer : Base {
|
||||
TestMemoryBuffer(Allocator alloc) : Base(alloc) {}
|
||||
void grow(std::size_t size) { Base::grow(size); }
|
||||
} buffer((Allocator(&alloc)));
|
||||
buffer.resize(7);
|
||||
for (int i = 0; i < 7; ++i)
|
||||
buffer[i] = i * i;
|
||||
EXPECT_EQ(10u, buffer.capacity());
|
||||
int mem[20];
|
||||
mem[7] = 0xdead;
|
||||
EXPECT_CALL(alloc, allocate(20)).WillOnce(Return(mem));
|
||||
buffer.grow(20);
|
||||
EXPECT_EQ(20u, buffer.capacity());
|
||||
// Check if size elements have been copied
|
||||
for (int i = 0; i < 7; ++i)
|
||||
EXPECT_EQ(i * i, buffer[i]);
|
||||
// and no more than that.
|
||||
EXPECT_EQ(0xdead, buffer[7]);
|
||||
EXPECT_CALL(alloc, deallocate(mem, 20));
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, Allocator) {
|
||||
typedef AllocatorRef< MockAllocator<char> > TestAllocator;
|
||||
MemoryBuffer<char, 10, TestAllocator> buffer;
|
||||
EXPECT_EQ(0, buffer.get_allocator().get());
|
||||
StrictMock< MockAllocator<char> > alloc;
|
||||
char mem;
|
||||
{
|
||||
MemoryBuffer<char, 10, TestAllocator> buffer2((TestAllocator(&alloc)));
|
||||
EXPECT_EQ(&alloc, buffer2.get_allocator().get());
|
||||
std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE;
|
||||
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem));
|
||||
buffer2.reserve(size);
|
||||
EXPECT_CALL(alloc, deallocate(&mem, size));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MemoryBufferTest, ExceptionInDeallocate) {
|
||||
typedef AllocatorRef< MockAllocator<char> > TestAllocator;
|
||||
StrictMock< MockAllocator<char> > alloc;
|
||||
MemoryBuffer<char, 10, TestAllocator> buffer((TestAllocator(&alloc)));
|
||||
std::size_t size = 2 * fmt::internal::INLINE_BUFFER_SIZE;
|
||||
std::vector<char> mem(size);
|
||||
{
|
||||
EXPECT_CALL(alloc, allocate(size)).WillOnce(Return(&mem[0]));
|
||||
buffer.resize(size);
|
||||
std::fill(&buffer[0], &buffer[0] + size, 'x');
|
||||
}
|
||||
std::vector<char> mem2(2 * size);
|
||||
{
|
||||
EXPECT_CALL(alloc, allocate(2 * size)).WillOnce(Return(&mem2[0]));
|
||||
std::exception e;
|
||||
EXPECT_CALL(alloc, deallocate(&mem[0], size)).WillOnce(testing::Throw(e));
|
||||
EXPECT_THROW(buffer.reserve(2 * size), std::exception);
|
||||
EXPECT_EQ(&mem2[0], &buffer[0]);
|
||||
// Check that the data has been copied.
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
EXPECT_EQ('x', buffer[i]);
|
||||
}
|
||||
EXPECT_CALL(alloc, deallocate(&mem2[0], 2 * size));
|
||||
}
|
||||
|
||||
TEST(UtilTest, Increment) {
|
||||
char s[10] = "123";
|
||||
increment(s);
|
||||
EXPECT_STREQ("124", s);
|
||||
s[2] = '8';
|
||||
increment(s);
|
||||
EXPECT_STREQ("129", s);
|
||||
increment(s);
|
||||
EXPECT_STREQ("130", s);
|
||||
s[1] = s[2] = '9';
|
||||
increment(s);
|
||||
EXPECT_STREQ("200", s);
|
||||
}
|
||||
|
||||
template <Arg::Type>
|
||||
struct ArgInfo;
|
||||
|
||||
#define ARG_INFO(type_code, Type, field) \
|
||||
template <> \
|
||||
struct ArgInfo<Arg::type_code> { \
|
||||
static Type get(const Value &value) { return value.field; } \
|
||||
};
|
||||
|
||||
ARG_INFO(INT, int, int_value);
|
||||
ARG_INFO(UINT, unsigned, uint_value);
|
||||
ARG_INFO(LONG_LONG, fmt::LongLong, long_long_value);
|
||||
ARG_INFO(ULONG_LONG, fmt::ULongLong, ulong_long_value);
|
||||
ARG_INFO(DOUBLE, double, double_value);
|
||||
ARG_INFO(LONG_DOUBLE, long double, long_double_value);
|
||||
ARG_INFO(CHAR, int, int_value);
|
||||
ARG_INFO(CSTRING, const char *, string.value);
|
||||
ARG_INFO(STRING, const char *, string.value);
|
||||
ARG_INFO(WSTRING, const wchar_t *, wstring.value);
|
||||
ARG_INFO(POINTER, const void *, pointer);
|
||||
ARG_INFO(CUSTOM, Arg::CustomValue, custom);
|
||||
|
||||
#define CHECK_ARG_INFO(Type, field, value) { \
|
||||
Value arg_value = {}; \
|
||||
arg_value.field = value; \
|
||||
EXPECT_EQ(value, ArgInfo<Arg::Type>::get(arg_value)); \
|
||||
}
|
||||
|
||||
TEST(ArgTest, ArgInfo) {
|
||||
CHECK_ARG_INFO(INT, int_value, 42);
|
||||
CHECK_ARG_INFO(UINT, uint_value, 42u);
|
||||
CHECK_ARG_INFO(LONG_LONG, long_long_value, 42);
|
||||
CHECK_ARG_INFO(ULONG_LONG, ulong_long_value, 42u);
|
||||
CHECK_ARG_INFO(DOUBLE, double_value, 4.2);
|
||||
CHECK_ARG_INFO(LONG_DOUBLE, long_double_value, 4.2);
|
||||
CHECK_ARG_INFO(CHAR, int_value, 'x');
|
||||
const char STR[] = "abc";
|
||||
CHECK_ARG_INFO(CSTRING, string.value, STR);
|
||||
const wchar_t WSTR[] = L"abc";
|
||||
CHECK_ARG_INFO(WSTRING, wstring.value, WSTR);
|
||||
int p = 0;
|
||||
CHECK_ARG_INFO(POINTER, pointer, &p);
|
||||
Value value = {};
|
||||
value.custom.value = &p;
|
||||
EXPECT_EQ(&p, ArgInfo<Arg::CUSTOM>::get(value).value);
|
||||
}
|
||||
|
||||
#define EXPECT_ARG_(Char, type_code, MakeArgType, ExpectedType, value) { \
|
||||
MakeArgType input = static_cast<MakeArgType>(value); \
|
||||
Arg arg = make_arg<Char>(input); \
|
||||
EXPECT_EQ(Arg::type_code, arg.type); \
|
||||
ExpectedType expected_value = static_cast<ExpectedType>(value); \
|
||||
EXPECT_EQ(expected_value, ArgInfo<Arg::type_code>::get(arg)); \
|
||||
}
|
||||
|
||||
#define EXPECT_ARG(type_code, Type, value) \
|
||||
EXPECT_ARG_(char, type_code, Type, Type, value)
|
||||
|
||||
#define EXPECT_ARGW(type_code, Type, value) \
|
||||
EXPECT_ARG_(wchar_t, type_code, Type, Type, value)
|
||||
|
||||
TEST(ArgTest, MakeArg) {
|
||||
// Test bool.
|
||||
EXPECT_ARG_(char, INT, bool, int, true);
|
||||
EXPECT_ARG_(wchar_t, INT, bool, int, true);
|
||||
|
||||
// Test char.
|
||||
EXPECT_ARG(CHAR, signed char, 'a');
|
||||
EXPECT_ARG(CHAR, signed char, SCHAR_MIN);
|
||||
EXPECT_ARG(CHAR, signed char, SCHAR_MAX);
|
||||
EXPECT_ARG(CHAR, unsigned char, 'a');
|
||||
EXPECT_ARG(CHAR, unsigned char, UCHAR_MAX );
|
||||
EXPECT_ARG(CHAR, char, 'a');
|
||||
EXPECT_ARG(CHAR, char, CHAR_MIN);
|
||||
EXPECT_ARG(CHAR, char, CHAR_MAX);
|
||||
|
||||
// Test wchar_t.
|
||||
EXPECT_ARGW(CHAR, wchar_t, L'a');
|
||||
EXPECT_ARGW(CHAR, wchar_t, WCHAR_MIN);
|
||||
EXPECT_ARGW(CHAR, wchar_t, WCHAR_MAX);
|
||||
|
||||
// Test short.
|
||||
EXPECT_ARG(INT, short, 42);
|
||||
EXPECT_ARG(INT, short, SHRT_MIN);
|
||||
EXPECT_ARG(INT, short, SHRT_MAX);
|
||||
EXPECT_ARG(UINT, unsigned short, 42);
|
||||
EXPECT_ARG(UINT, unsigned short, USHRT_MAX);
|
||||
|
||||
// Test int.
|
||||
EXPECT_ARG(INT, int, 42);
|
||||
EXPECT_ARG(INT, int, INT_MIN);
|
||||
EXPECT_ARG(INT, int, INT_MAX);
|
||||
EXPECT_ARG(UINT, unsigned, 42);
|
||||
EXPECT_ARG(UINT, unsigned, UINT_MAX);
|
||||
|
||||
// Test long.
|
||||
#if LONG_MAX == INT_MAX
|
||||
# define LONG INT
|
||||
# define ULONG UINT
|
||||
# define long_value int_value
|
||||
# define ulong_value uint_value
|
||||
#else
|
||||
# define LONG LONG_LONG
|
||||
# define ULONG ULONG_LONG
|
||||
# define long_value long_long_value
|
||||
# define ulong_value ulong_long_value
|
||||
#endif
|
||||
EXPECT_ARG(LONG, long, 42);
|
||||
EXPECT_ARG(LONG, long, LONG_MIN);
|
||||
EXPECT_ARG(LONG, long, LONG_MAX);
|
||||
EXPECT_ARG(ULONG, unsigned long, 42);
|
||||
EXPECT_ARG(ULONG, unsigned long, ULONG_MAX);
|
||||
|
||||
// Test long long.
|
||||
EXPECT_ARG(LONG_LONG, fmt::LongLong, 42);
|
||||
EXPECT_ARG(LONG_LONG, fmt::LongLong, LLONG_MIN);
|
||||
EXPECT_ARG(LONG_LONG, fmt::LongLong, LLONG_MAX);
|
||||
EXPECT_ARG(ULONG_LONG, fmt::ULongLong, 42);
|
||||
EXPECT_ARG(ULONG_LONG, fmt::ULongLong, ULLONG_MAX);
|
||||
|
||||
// Test float.
|
||||
EXPECT_ARG(DOUBLE, float, 4.2);
|
||||
EXPECT_ARG(DOUBLE, float, FLT_MIN);
|
||||
EXPECT_ARG(DOUBLE, float, FLT_MAX);
|
||||
|
||||
// Test double.
|
||||
EXPECT_ARG(DOUBLE, double, 4.2);
|
||||
EXPECT_ARG(DOUBLE, double, DBL_MIN);
|
||||
EXPECT_ARG(DOUBLE, double, DBL_MAX);
|
||||
|
||||
// Test long double.
|
||||
EXPECT_ARG(LONG_DOUBLE, long double, 4.2);
|
||||
EXPECT_ARG(LONG_DOUBLE, long double, LDBL_MIN);
|
||||
EXPECT_ARG(LONG_DOUBLE, long double, LDBL_MAX);
|
||||
|
||||
// Test string.
|
||||
char STR[] = "test";
|
||||
EXPECT_ARG(CSTRING, char*, STR);
|
||||
EXPECT_ARG(CSTRING, const char*, STR);
|
||||
EXPECT_ARG(STRING, std::string, STR);
|
||||
EXPECT_ARG(STRING, fmt::StringRef, STR);
|
||||
|
||||
// Test wide string.
|
||||
wchar_t WSTR[] = L"test";
|
||||
EXPECT_ARGW(WSTRING, wchar_t*, WSTR);
|
||||
EXPECT_ARGW(WSTRING, const wchar_t*, WSTR);
|
||||
EXPECT_ARGW(WSTRING, std::wstring, WSTR);
|
||||
EXPECT_ARGW(WSTRING, fmt::WStringRef, WSTR);
|
||||
|
||||
int n = 42;
|
||||
EXPECT_ARG(POINTER, void*, &n);
|
||||
EXPECT_ARG(POINTER, const void*, &n);
|
||||
|
||||
::Test t;
|
||||
Arg arg = make_arg<char>(t);
|
||||
EXPECT_EQ(fmt::internal::Arg::CUSTOM, arg.type);
|
||||
EXPECT_EQ(&t, arg.custom.value);
|
||||
fmt::MemoryWriter w;
|
||||
fmt::BasicFormatter<char> formatter(w);
|
||||
const char *s = "}";
|
||||
arg.custom.format(&formatter, &t, &s);
|
||||
EXPECT_EQ("test", w.str());
|
||||
}
|
||||
|
||||
TEST(UtilTest, ArgList) {
|
||||
fmt::ArgList args;
|
||||
EXPECT_EQ(Arg::NONE, args[fmt::ArgList::MAX_ARGS].type);
|
||||
}
|
||||
|
||||
struct Result {
|
||||
Arg arg;
|
||||
|
||||
Result() : arg(make_arg<char>(0xdeadbeef)) {}
|
||||
|
||||
template <typename T>
|
||||
Result(const T& value) : arg(make_arg<char>(value)) {}
|
||||
Result(const wchar_t *s) : arg(make_arg<wchar_t>(s)) {}
|
||||
};
|
||||
|
||||
struct TestVisitor : fmt::internal::ArgVisitor<TestVisitor, Result> {
|
||||
Result visit_int(int value) { return value; }
|
||||
Result visit_uint(unsigned value) { return value; }
|
||||
Result visit_long_long(fmt::LongLong value) { return value; }
|
||||
Result visit_ulong_long(fmt::ULongLong value) { return value; }
|
||||
Result visit_double(double value) { return value; }
|
||||
Result visit_long_double(long double value) { return value; }
|
||||
Result visit_char(int value) { return static_cast<char>(value); }
|
||||
Result visit_string(Arg::StringValue<char> s) { return s.value; }
|
||||
Result visit_wstring(Arg::StringValue<wchar_t> s) { return s.value; }
|
||||
Result visit_pointer(const void *p) { return p; }
|
||||
Result visit_custom(Arg::CustomValue c) {
|
||||
return *static_cast<const ::Test*>(c.value);
|
||||
}
|
||||
};
|
||||
|
||||
#define EXPECT_RESULT_(Char, type_code, value) { \
|
||||
Arg arg = make_arg<Char>(value); \
|
||||
Result result = TestVisitor().visit(arg); \
|
||||
EXPECT_EQ(Arg::type_code, result.arg.type); \
|
||||
EXPECT_EQ(value, ArgInfo<Arg::type_code>::get(result.arg)); \
|
||||
}
|
||||
|
||||
#define EXPECT_RESULT(type_code, value) \
|
||||
EXPECT_RESULT_(char, type_code, value)
|
||||
#define EXPECT_RESULTW(type_code, value) \
|
||||
EXPECT_RESULT_(wchar_t, type_code, value)
|
||||
|
||||
TEST(ArgVisitorTest, VisitAll) {
|
||||
EXPECT_RESULT(INT, 42);
|
||||
EXPECT_RESULT(UINT, 42u);
|
||||
EXPECT_RESULT(LONG_LONG, 42ll);
|
||||
EXPECT_RESULT(ULONG_LONG, 42ull);
|
||||
EXPECT_RESULT(DOUBLE, 4.2);
|
||||
EXPECT_RESULT(LONG_DOUBLE, 4.2l);
|
||||
EXPECT_RESULT(CHAR, 'x');
|
||||
const char STR[] = "abc";
|
||||
EXPECT_RESULT(CSTRING, STR);
|
||||
const wchar_t WSTR[] = L"abc";
|
||||
EXPECT_RESULTW(WSTRING, WSTR);
|
||||
const void *p = STR;
|
||||
EXPECT_RESULT(POINTER, p);
|
||||
::Test t;
|
||||
Result result = TestVisitor().visit(make_arg<char>(t));
|
||||
EXPECT_EQ(Arg::CUSTOM, result.arg.type);
|
||||
EXPECT_EQ(&t, result.arg.custom.value);
|
||||
}
|
||||
|
||||
struct TestAnyVisitor : fmt::internal::ArgVisitor<TestAnyVisitor, Result> {
|
||||
template <typename T>
|
||||
Result visit_any_int(T value) { return value; }
|
||||
|
||||
template <typename T>
|
||||
Result visit_any_double(T value) { return value; }
|
||||
};
|
||||
|
||||
#undef EXPECT_RESULT
|
||||
#define EXPECT_RESULT(type_code, value) { \
|
||||
Result result = TestAnyVisitor().visit(make_arg<char>(value)); \
|
||||
EXPECT_EQ(Arg::type_code, result.arg.type); \
|
||||
EXPECT_EQ(value, ArgInfo<Arg::type_code>::get(result.arg)); \
|
||||
}
|
||||
|
||||
TEST(ArgVisitorTest, VisitAny) {
|
||||
EXPECT_RESULT(INT, 42);
|
||||
EXPECT_RESULT(UINT, 42u);
|
||||
EXPECT_RESULT(LONG_LONG, 42ll);
|
||||
EXPECT_RESULT(ULONG_LONG, 42ull);
|
||||
EXPECT_RESULT(DOUBLE, 4.2);
|
||||
EXPECT_RESULT(LONG_DOUBLE, 4.2l);
|
||||
}
|
||||
|
||||
struct TestUnhandledVisitor :
|
||||
fmt::internal::ArgVisitor<TestUnhandledVisitor, const char *> {
|
||||
const char *visit_unhandled_arg() { return "test"; }
|
||||
};
|
||||
|
||||
#define EXPECT_UNHANDLED(value) \
|
||||
EXPECT_STREQ("test", TestUnhandledVisitor().visit(make_arg<wchar_t>(value)));
|
||||
|
||||
TEST(ArgVisitorTest, VisitUnhandledArg) {
|
||||
EXPECT_UNHANDLED(42);
|
||||
EXPECT_UNHANDLED(42u);
|
||||
EXPECT_UNHANDLED(42ll);
|
||||
EXPECT_UNHANDLED(42ull);
|
||||
EXPECT_UNHANDLED(4.2);
|
||||
EXPECT_UNHANDLED(4.2l);
|
||||
EXPECT_UNHANDLED('x');
|
||||
const char STR[] = "abc";
|
||||
EXPECT_UNHANDLED(STR);
|
||||
const wchar_t WSTR[] = L"abc";
|
||||
EXPECT_UNHANDLED(WSTR);
|
||||
const void *p = STR;
|
||||
EXPECT_UNHANDLED(p);
|
||||
EXPECT_UNHANDLED(::Test());
|
||||
}
|
||||
|
||||
TEST(ArgVisitorTest, VisitInvalidArg) {
|
||||
Arg arg = Arg();
|
||||
arg.type = static_cast<Arg::Type>(Arg::CUSTOM + 1);
|
||||
EXPECT_DEBUG_DEATH(TestVisitor().visit(arg), "Assertion");
|
||||
}
|
||||
|
||||
// Tests fmt::internal::count_digits for integer type Int.
|
||||
template <typename Int>
|
||||
void test_count_digits() {
|
||||
for (Int i = 0; i < 10; ++i)
|
||||
EXPECT_EQ(1u, fmt::internal::count_digits(i));
|
||||
for (Int i = 1, n = 1,
|
||||
end = std::numeric_limits<Int>::max() / 10; n <= end; ++i) {
|
||||
n *= 10;
|
||||
EXPECT_EQ(i, fmt::internal::count_digits(n - 1));
|
||||
EXPECT_EQ(i + 1, fmt::internal::count_digits(n));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UtilTest, StringRef) {
|
||||
// Test that StringRef::size() returns string length, not buffer size.
|
||||
char str[100] = "some string";
|
||||
EXPECT_EQ(std::strlen(str), StringRef(str).size());
|
||||
EXPECT_LT(std::strlen(str), sizeof(str));
|
||||
}
|
||||
|
||||
TEST(UtilTest, CountDigits) {
|
||||
test_count_digits<uint32_t>();
|
||||
test_count_digits<uint64_t>();
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
TEST(UtilTest, UTF16ToUTF8) {
|
||||
std::string s = "ёжик";
|
||||
fmt::internal::UTF16ToUTF8 u(L"\x0451\x0436\x0438\x043A");
|
||||
EXPECT_EQ(s, u.str());
|
||||
EXPECT_EQ(s.size(), u.size());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF8ToUTF16) {
|
||||
std::string s = "лошадка";
|
||||
fmt::internal::UTF8ToUTF16 u(s.c_str());
|
||||
EXPECT_EQ(L"\x043B\x043E\x0448\x0430\x0434\x043A\x0430", u.str());
|
||||
EXPECT_EQ(7, u.size());
|
||||
}
|
||||
|
||||
template <typename Converter, typename Char>
|
||||
void check_utf_conversion_error(const char *message) {
|
||||
fmt::MemoryWriter out;
|
||||
fmt::internal::format_windows_error(out, ERROR_INVALID_PARAMETER, message);
|
||||
fmt::SystemError error(0, "");
|
||||
try {
|
||||
Converter(fmt::BasicStringRef<Char>(0, 0));
|
||||
} catch (const fmt::SystemError &e) {
|
||||
error = e;
|
||||
}
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, error.error_code());
|
||||
EXPECT_EQ(out.str(), error.what());
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8Error) {
|
||||
check_utf_conversion_error<fmt::internal::UTF16ToUTF8, wchar_t>(
|
||||
"cannot convert string from UTF-16 to UTF-8");
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF8ToUTF16Error) {
|
||||
check_utf_conversion_error<fmt::internal::UTF8ToUTF16, char>(
|
||||
"cannot convert string from UTF-8 to UTF-16");
|
||||
}
|
||||
|
||||
TEST(UtilTest, UTF16ToUTF8Convert) {
|
||||
fmt::internal::UTF16ToUTF8 u;
|
||||
EXPECT_EQ(ERROR_INVALID_PARAMETER, u.convert(fmt::WStringRef(0, 0)));
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
||||
typedef void (*FormatErrorMessage)(
|
||||
fmt::Writer &out, int error_code, StringRef message);
|
||||
|
||||
template <typename Error>
|
||||
void check_throw_error(int error_code, FormatErrorMessage format) {
|
||||
fmt::SystemError error(0, "");
|
||||
try {
|
||||
throw Error(error_code, "test {}", "error");
|
||||
} catch (const fmt::SystemError &e) {
|
||||
error = e;
|
||||
}
|
||||
fmt::MemoryWriter message;
|
||||
format(message, error_code, "test error");
|
||||
EXPECT_EQ(message.str(), error.what());
|
||||
EXPECT_EQ(error_code, error.error_code());
|
||||
}
|
||||
|
||||
TEST(UtilTest, FormatSystemError) {
|
||||
fmt::MemoryWriter message;
|
||||
fmt::internal::format_system_error(message, EDOM, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), message.str());
|
||||
message.clear();
|
||||
fmt::internal::format_system_error(
|
||||
message, EDOM, fmt::StringRef("x", std::numeric_limits<size_t>::max()));
|
||||
EXPECT_EQ(fmt::format("error {}", EDOM), message.str());
|
||||
}
|
||||
|
||||
TEST(UtilTest, SystemError) {
|
||||
fmt::SystemError e(EDOM, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), e.what());
|
||||
EXPECT_EQ(EDOM, e.error_code());
|
||||
check_throw_error<fmt::SystemError>(EDOM, fmt::internal::format_system_error);
|
||||
}
|
||||
|
||||
TEST(UtilTest, ReportSystemError) {
|
||||
fmt::MemoryWriter out;
|
||||
fmt::internal::format_system_error(out, EDOM, "test error");
|
||||
out << '\n';
|
||||
EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"), out.str());
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
TEST(UtilTest, FormatWindowsError) {
|
||||
LPWSTR message = 0;
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
||||
ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
reinterpret_cast<LPWSTR>(&message), 0, 0);
|
||||
fmt::internal::UTF16ToUTF8 utf8_message(message);
|
||||
LocalFree(message);
|
||||
fmt::MemoryWriter actual_message;
|
||||
fmt::internal::format_windows_error(
|
||||
actual_message, ERROR_FILE_EXISTS, "test");
|
||||
EXPECT_EQ(fmt::format("test: {}", utf8_message.str()),
|
||||
actual_message.str());
|
||||
actual_message.clear();
|
||||
fmt::internal::format_windows_error(
|
||||
actual_message, ERROR_FILE_EXISTS,
|
||||
fmt::StringRef("x", std::numeric_limits<size_t>::max()));
|
||||
EXPECT_EQ(fmt::format("error {}", ERROR_FILE_EXISTS), actual_message.str());
|
||||
}
|
||||
|
||||
TEST(UtilTest, WindowsError) {
|
||||
check_throw_error<fmt::WindowsError>(
|
||||
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
|
||||
}
|
||||
|
||||
TEST(UtilTest, ReportWindowsError) {
|
||||
fmt::MemoryWriter out;
|
||||
fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
|
||||
out << '\n';
|
||||
EXPECT_WRITE(stderr,
|
||||
fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"), out.str());
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
||||
51
test/util.cc
Normal file
51
test/util.cc
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Test utilities.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "util.h"
|
||||
#include <cstring>
|
||||
|
||||
void increment(char *s) {
|
||||
for (int i = static_cast<int>(std::strlen(s)) - 1; i >= 0; --i) {
|
||||
if (s[i] != '9') {
|
||||
++s[i];
|
||||
break;
|
||||
}
|
||||
s[i] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_system_error(int error_code) {
|
||||
#if defined(__MINGW32__) || !defined(_WIN32)
|
||||
return strerror(error_code);
|
||||
#else
|
||||
enum { BUFFER_SIZE = 200 };
|
||||
char buffer[BUFFER_SIZE];
|
||||
if (strerror_s(buffer, BUFFER_SIZE, error_code))
|
||||
throw std::exception("strerror_s failed");
|
||||
return buffer;
|
||||
#endif
|
||||
}
|
||||
51
test/util.h
Normal file
51
test/util.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Test utilities.
|
||||
|
||||
Copyright (c) 2012-2014, Victor Zverovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
enum {BUFFER_SIZE = 256};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define FMT_VSNPRINTF vsprintf_s
|
||||
#else
|
||||
# define FMT_VSNPRINTF vsnprintf
|
||||
#endif
|
||||
|
||||
template <std::size_t SIZE>
|
||||
void safe_sprintf(char (&buffer)[SIZE], const char *format, ...) {
|
||||
std::va_list args;
|
||||
va_start(args, format);
|
||||
FMT_VSNPRINTF(buffer, SIZE, format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
// Increment a number in a string.
|
||||
void increment(char *s);
|
||||
|
||||
std::string get_system_error(int error_code);
|
||||
Reference in New Issue
Block a user