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 // This tests that swapping filebufs works correctly even when the small buffer
10 // optimization is in use (https://github.com/llvm/llvm-project/issues/49282).
11 
12 // <fstream>
13 
14 // template <class charT, class traits = char_traits<charT> >
15 // class basic_filebuf
16 
17 // template <class charT, class traits>
18 // void
19 // swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
20 
21 #include <fstream>
22 #include <cassert>
23 #include "test_macros.h"
24 #include "platform_support.h"
25 
main(int,char **)26 int main(int, char**)
27 {
28     std::string tmpA = get_temp_file_name();
29     std::string tmpB = get_temp_file_name();
30 
31     {
32         std::ofstream sa(tmpA), sb(tmpB);
33         sa << "AAAA";
34         sb << "BBBB";
35     }
36 
37     std::filebuf f1;
38     assert(f1.open(tmpA, std::ios_base::in) != 0);
39     assert(f1.is_open());
40     f1.pubsetbuf(0, 0);
41 
42     std::filebuf f2;
43     assert(f2.open(tmpB, std::ios_base::in) != 0);
44     assert(f2.is_open());
45     f2.pubsetbuf(0, 0);
46 
47     assert(f1.sgetc() == 'A');
48     assert(f2.sgetc() == 'B');
49 
50     swap(f1, f2);
51 
52     assert(f1.is_open());
53     assert(f2.is_open());
54 
55     assert(f1.sgetc() == 'B');
56     assert(f2.sgetc() == 'A');
57 
58     std::remove(tmpA.c_str());
59     std::remove(tmpB.c_str());
60 
61     return 0;
62 }
63