xref: /llvm-project/libcxx/test/std/input.output/file.streams/fstreams/ofstream.assign/member_swap.pass.cpp (revision f4c1258d5633fcf06385ff3fd1f4bf57ab971964)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <fstream>
10 
11 // template <class charT, class traits = char_traits<charT> >
12 // class basic_ofstream
13 
14 // void swap(basic_ofstream& rhs);
15 
16 #include <fstream>
17 #include <utility>
18 #include <cassert>
19 #include "test_macros.h"
20 #include "platform_support.h"
21 
get_temp_file_names()22 std::pair<std::string, std::string> get_temp_file_names() {
23   std::pair<std::string, std::string> names;
24   names.first = get_temp_file_name();
25 
26   // Create the file so the next call to `get_temp_file_name()` doesn't
27   // return the same file.
28   std::FILE *fd1 = std::fopen(names.first.c_str(), "w");
29 
30   names.second = get_temp_file_name();
31   assert(names.first != names.second);
32 
33   std::fclose(fd1);
34   std::remove(names.first.c_str());
35 
36   return names;
37 }
38 
main(int,char **)39 int main(int, char**)
40 {
41     std::pair<std::string, std::string> temp_files = get_temp_file_names();
42     std::string& temp1 = temp_files.first;
43     std::string& temp2 = temp_files.second;
44     assert(temp1 != temp2);
45     {
46         std::ofstream fs1(temp1.c_str());
47         std::ofstream fs2(temp2.c_str());
48         fs1 << 3.25;
49         fs2 << 4.5;
50         fs1.swap(fs2);
51         fs1 << ' ' << 3.25;
52         fs2 << ' ' << 4.5;
53     }
54     {
55         std::ifstream fs(temp1.c_str());
56         double x = 0;
57         fs >> x;
58         assert(x == 3.25);
59         fs >> x;
60         assert(x == 4.5);
61     }
62     std::remove(temp1.c_str());
63     {
64         std::ifstream fs(temp2.c_str());
65         double x = 0;
66         fs >> x;
67         assert(x == 4.5);
68         fs >> x;
69         assert(x == 3.25);
70     }
71     std::remove(temp2.c_str());
72 
73 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
74     {
75         std::wofstream fs1(temp1.c_str());
76         std::wofstream fs2(temp2.c_str());
77         fs1 << 3.25;
78         fs2 << 4.5;
79         fs1.swap(fs2);
80         fs1 << ' ' << 3.25;
81         fs2 << ' ' << 4.5;
82     }
83     {
84         std::wifstream fs(temp1.c_str());
85         double x = 0;
86         fs >> x;
87         assert(x == 3.25);
88         fs >> x;
89         assert(x == 4.5);
90     }
91     std::remove(temp1.c_str());
92     {
93         std::wifstream fs(temp2.c_str());
94         double x = 0;
95         fs >> x;
96         assert(x == 4.5);
97         fs >> x;
98         assert(x == 3.25);
99     }
100     std::remove(temp2.c_str());
101 #endif
102 
103   return 0;
104 }
105