15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <fstream>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // template <class charT, class traits = char_traits<charT> >
125a83710eSEric Fiselier // class basic_ofstream
135a83710eSEric Fiselier 
145a83710eSEric Fiselier // template <class charT, class traits>
155a83710eSEric Fiselier //   void swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
165a83710eSEric Fiselier 
175a83710eSEric Fiselier #include <fstream>
18ea9dc4aeSEric Fiselier #include <utility>
195a83710eSEric Fiselier #include <cassert>
207fc6a556SMarshall Clow #include "test_macros.h"
215a83710eSEric Fiselier #include "platform_support.h"
225a83710eSEric Fiselier 
get_temp_file_names()23ea9dc4aeSEric Fiselier std::pair<std::string, std::string> get_temp_file_names() {
24ea9dc4aeSEric Fiselier   std::pair<std::string, std::string> names;
25ea9dc4aeSEric Fiselier   names.first = get_temp_file_name();
26ea9dc4aeSEric Fiselier 
27ea9dc4aeSEric Fiselier   // Create the file so the next call to `get_temp_file_name()` doesn't
28ea9dc4aeSEric Fiselier   // return the same file.
29ea9dc4aeSEric Fiselier   std::FILE *fd1 = std::fopen(names.first.c_str(), "w");
30ea9dc4aeSEric Fiselier 
31ea9dc4aeSEric Fiselier   names.second = get_temp_file_name();
32ea9dc4aeSEric Fiselier   assert(names.first != names.second);
33ea9dc4aeSEric Fiselier 
34ea9dc4aeSEric Fiselier   std::fclose(fd1);
35ea9dc4aeSEric Fiselier   std::remove(names.first.c_str());
36ea9dc4aeSEric Fiselier 
37ea9dc4aeSEric Fiselier   return names;
38ea9dc4aeSEric Fiselier }
39ea9dc4aeSEric Fiselier 
main(int,char **)402df59c50SJF Bastien int main(int, char**)
415a83710eSEric Fiselier {
42ea9dc4aeSEric Fiselier     std::pair<std::string, std::string> temp_files = get_temp_file_names();
43ea9dc4aeSEric Fiselier     std::string& temp1 = temp_files.first;
44ea9dc4aeSEric Fiselier     std::string& temp2 = temp_files.second;
45ea9dc4aeSEric Fiselier     assert(temp1 != temp2);
465a83710eSEric Fiselier     {
475a83710eSEric Fiselier         std::ofstream fs1(temp1.c_str());
485a83710eSEric Fiselier         std::ofstream fs2(temp2.c_str());
495a83710eSEric Fiselier         fs1 << 3.25;
505a83710eSEric Fiselier         fs2 << 4.5;
515a83710eSEric Fiselier         swap(fs1, fs2);
525a83710eSEric Fiselier         fs1 << ' ' << 3.25;
535a83710eSEric Fiselier         fs2 << ' ' << 4.5;
545a83710eSEric Fiselier     }
555a83710eSEric Fiselier     {
565a83710eSEric Fiselier         std::ifstream fs(temp1.c_str());
575a83710eSEric Fiselier         double x = 0;
585a83710eSEric Fiselier         fs >> x;
595a83710eSEric Fiselier         assert(x == 3.25);
605a83710eSEric Fiselier         fs >> x;
615a83710eSEric Fiselier         assert(x == 4.5);
625a83710eSEric Fiselier     }
635a83710eSEric Fiselier     std::remove(temp1.c_str());
645a83710eSEric Fiselier     {
655a83710eSEric Fiselier         std::ifstream fs(temp2.c_str());
665a83710eSEric Fiselier         double x = 0;
675a83710eSEric Fiselier         fs >> x;
685a83710eSEric Fiselier         assert(x == 4.5);
695a83710eSEric Fiselier         fs >> x;
705a83710eSEric Fiselier         assert(x == 3.25);
715a83710eSEric Fiselier     }
725a83710eSEric Fiselier     std::remove(temp2.c_str());
73*f4c1258dSLouis Dionne 
74*f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
755a83710eSEric Fiselier     {
765a83710eSEric Fiselier         std::wofstream fs1(temp1.c_str());
775a83710eSEric Fiselier         std::wofstream fs2(temp2.c_str());
785a83710eSEric Fiselier         fs1 << 3.25;
795a83710eSEric Fiselier         fs2 << 4.5;
805a83710eSEric Fiselier         swap(fs1, fs2);
815a83710eSEric Fiselier         fs1 << ' ' << 3.25;
825a83710eSEric Fiselier         fs2 << ' ' << 4.5;
835a83710eSEric Fiselier     }
845a83710eSEric Fiselier     {
855a83710eSEric Fiselier         std::wifstream fs(temp1.c_str());
865a83710eSEric Fiselier         double x = 0;
875a83710eSEric Fiselier         fs >> x;
885a83710eSEric Fiselier         assert(x == 3.25);
895a83710eSEric Fiselier         fs >> x;
905a83710eSEric Fiselier         assert(x == 4.5);
915a83710eSEric Fiselier     }
925a83710eSEric Fiselier     std::remove(temp1.c_str());
935a83710eSEric Fiselier     {
945a83710eSEric Fiselier         std::wifstream fs(temp2.c_str());
955a83710eSEric Fiselier         double x = 0;
965a83710eSEric Fiselier         fs >> x;
975a83710eSEric Fiselier         assert(x == 4.5);
985a83710eSEric Fiselier         fs >> x;
995a83710eSEric Fiselier         assert(x == 3.25);
1005a83710eSEric Fiselier     }
1015a83710eSEric Fiselier     std::remove(temp2.c_str());
102*f4c1258dSLouis Dionne #endif
1032df59c50SJF Bastien 
1042df59c50SJF Bastien   return 0;
1055a83710eSEric Fiselier }
106