brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 287716f Raw
99 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <fstream>10 11// template <class charT, class traits = char_traits<charT> >12// class basic_fstream13 14// template <class charT, class traits>15//   void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);16 17#include <fstream>18#include <utility>19#include <cassert>20#include "test_macros.h"21#include "platform_support.h"22 23 24std::pair<std::string, std::string> get_temp_file_names() {25  std::pair<std::string, std::string> names;26  names.first = get_temp_file_name();27 28  // Create the file so the next call to `get_temp_file_name()` doesn't29  // return the same file.30  std::FILE *fd1 = std::fopen(names.first.c_str(), "w");31 32  names.second = get_temp_file_name();33  assert(names.first != names.second);34 35  std::fclose(fd1);36  std::remove(names.first.c_str());37 38  return names;39}40 41int main(int, char**)42{43    std::pair<std::string, std::string> temp_files = get_temp_file_names();44    std::string& temp1 = temp_files.first;45    std::string& temp2 = temp_files.second;46    assert(temp1 != temp2);47    {48        std::fstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out49                                                  | std::ios_base::trunc);50        std::fstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out51                                                  | std::ios_base::trunc);52        fs1 << 1 << ' ' << 2;53        fs2 << 2 << ' ' << 1;54        fs1.seekg(0);55        swap(fs1, fs2);56        fs1.seekg(0);57        int i;58        fs1 >> i;59        assert(i == 2);60        fs1 >> i;61        assert(i == 1);62        i = 0;63        fs2 >> i;64        assert(i == 1);65        fs2 >> i;66        assert(i == 2);67    }68    std::remove(temp1.c_str());69    std::remove(temp2.c_str());70 71#ifndef TEST_HAS_NO_WIDE_CHARACTERS72    {73        std::wfstream fs1(temp1.c_str(), std::ios_base::in | std::ios_base::out74                                                   | std::ios_base::trunc);75        std::wfstream fs2(temp2.c_str(), std::ios_base::in | std::ios_base::out76                                                   | std::ios_base::trunc);77        fs1 << 1 << ' ' << 2;78        fs2 << 2 << ' ' << 1;79        fs1.seekg(0);80        swap(fs1, fs2);81        fs1.seekg(0);82        int i;83        fs1 >> i;84        assert(i == 2);85        fs1 >> i;86        assert(i == 1);87        i = 0;88        fs2 >> i;89        assert(i == 1);90        fs2 >> i;91        assert(i == 2);92    }93    std::remove(temp1.c_str());94    std::remove(temp2.c_str());95#endif96 97  return 0;98}99