forked from microsoft/STL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly catch exceptions in {i|o}stream
Fixes microsoft#1858
- Loading branch information
Showing
5 changed files
with
146 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
RUNALL_INCLUDE ..\usual_matrix.lst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <cassert> | ||
#include <istream> | ||
#include <ostream> | ||
|
||
using namespace std; | ||
|
||
template <class CharT> | ||
class throwing_buffer : public basic_streambuf<CharT> { | ||
public: | ||
streampos seekoff(streamoff, ios::seekdir, ios_base::openmode = ios_base::in | ios_base::out) override { | ||
throw 42; | ||
} | ||
|
||
streampos seekpos(streampos, ios_base::openmode = ios_base::in | ios_base::out) override { | ||
throw 42; | ||
} | ||
|
||
int sync() override { | ||
throw 42; | ||
} | ||
|
||
basic_streambuf<CharT>* to_buf() { | ||
return this; | ||
} | ||
}; | ||
|
||
template <class CharT> | ||
void test_istream_exceptions() { | ||
throwing_buffer<CharT> buffer; | ||
|
||
{ // sync | ||
basic_istream<CharT> is(buffer.to_buf()); | ||
assert(!is.bad()); | ||
is.sync(); | ||
assert(is.bad()); | ||
} | ||
|
||
{ // seekg | ||
basic_istream<CharT> is(buffer.to_buf()); | ||
assert(!is.bad()); | ||
is.seekg(0); | ||
assert(is.bad()); | ||
} | ||
|
||
{ // seekg | ||
basic_istream<CharT> is(buffer.to_buf()); | ||
assert(!is.bad()); | ||
is.seekg(0, ios_base::beg); | ||
assert(is.bad()); | ||
} | ||
|
||
{ // tellg | ||
basic_istream<CharT> is(buffer.to_buf()); | ||
assert(!is.bad()); | ||
is.tellg(); | ||
assert(is.bad()); | ||
} | ||
} | ||
|
||
|
||
template <class CharT> | ||
void test_ostream_exceptions() { | ||
throwing_buffer<CharT> buffer; | ||
|
||
{ // flush | ||
basic_ostream<CharT> os(buffer.to_buf()); | ||
assert(!os.bad()); | ||
os.flush(); | ||
assert(os.bad()); | ||
} | ||
|
||
{ // seekp | ||
basic_ostream<CharT> os(buffer.to_buf()); | ||
assert(!os.bad()); | ||
os.seekp(0); | ||
assert(os.bad()); | ||
} | ||
|
||
{ // seekp | ||
basic_ostream<CharT> os(buffer.to_buf()); | ||
assert(!os.bad()); | ||
os.seekp(0, ios_base::beg); | ||
assert(os.bad()); | ||
} | ||
|
||
{ // tellp | ||
basic_ostream<CharT> os(buffer.to_buf()); | ||
assert(!os.bad()); | ||
os.tellp(); | ||
assert(os.bad()); | ||
} | ||
} | ||
|
||
int main() { | ||
test_istream_exceptions<char>(); | ||
test_istream_exceptions<wchar_t>(); | ||
|
||
test_ostream_exceptions<char>(); | ||
test_ostream_exceptions<wchar_t>(); | ||
} |