brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · f6378e7 Raw
70 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// pos_type seekoff(off_type off, ios_base::seekdir way,12//                  ios_base::openmode which = ios_base::in | ios_base::out);13// pos_type seekpos(pos_type sp,14//                  ios_base::openmode which = ios_base::in | ios_base::out);15 16#include <fstream>17#include <cassert>18 19#include "test_macros.h"20 21int main(int, char**)22{23    {24        char buf[10];25        typedef std::filebuf::pos_type pos_type;26        std::filebuf f;27        f.pubsetbuf(buf, sizeof(buf));28        assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out29                                                       | std::ios_base::trunc) != 0);30        assert(f.is_open());31        f.sputn("abcdefghijklmnopqrstuvwxyz", 26);32        pos_type p = f.pubseekoff(-15, std::ios_base::cur);33        assert(p == 11);34        assert(f.sgetc() == 'l');35        f.pubseekoff(0, std::ios_base::beg);36        assert(f.sgetc() == 'a');37        f.pubseekoff(-1, std::ios_base::end);38        assert(f.sgetc() == 'z');39        assert(f.pubseekpos(p) == p);40        assert(f.sgetc() == 'l');41    }42    std::remove("seekoff.dat");43 44#ifndef TEST_HAS_NO_WIDE_CHARACTERS45    {46        wchar_t buf[10];47        typedef std::filebuf::pos_type pos_type;48        std::wfilebuf f;49        f.pubsetbuf(buf, sizeof(buf)/sizeof(buf[0]));50        assert(f.open("seekoff.dat", std::ios_base::in | std::ios_base::out51                                                       | std::ios_base::trunc) != 0);52        assert(f.is_open());53        f.sputn(L"abcdefghijklmnopqrstuvwxyz", 26);54        LIBCPP_ASSERT(buf[0] == L'v');55        pos_type p = f.pubseekoff(-15, std::ios_base::cur);56        assert(p == 11);57        assert(f.sgetc() == L'l');58        f.pubseekoff(0, std::ios_base::beg);59        assert(f.sgetc() == L'a');60        f.pubseekoff(-1, std::ios_base::end);61        assert(f.sgetc() == L'z');62        assert(f.pubseekpos(p) == p);63        assert(f.sgetc() == L'l');64    }65    std::remove("seekoff.dat");66#endif67 68  return 0;69}70