iostream-test.c++ (2862B)
1 // Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors 2 // Licensed under the MIT License: 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a copy 5 // of this software and associated documentation files (the "Software"), to deal 6 // in the Software without restriction, including without limitation the rights 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 // copies of the Software, and to permit persons to whom the Software is 9 // furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 // THE SOFTWARE. 21 22 #include "iostream.h" 23 #include <sstream> 24 #include <kj/compat/gtest.h> 25 26 namespace kj { 27 namespace std { 28 namespace { 29 30 TEST(StdIoStream, WriteVec) { 31 // Check that writing an array of arrays works even when some of the arrays 32 // are empty. (This used to not work in some cases.) 33 34 ::std::stringstream ss; 35 36 StdInputStream in(ss); 37 StdOutputStream out(ss); 38 39 ArrayPtr<const byte> pieces[5] = { 40 arrayPtr(implicitCast<const byte*>(nullptr), 0), 41 arrayPtr(reinterpret_cast<const byte*>("foo"), 3), 42 arrayPtr(implicitCast<const byte*>(nullptr), 0), 43 arrayPtr(reinterpret_cast<const byte*>("bar"), 3), 44 arrayPtr(implicitCast<const byte*>(nullptr), 0) 45 }; 46 47 out.write(pieces); 48 49 char buf[7]; 50 in.read(buf, 6); 51 buf[6] = '\0'; 52 53 EXPECT_STREQ("foobar", buf); 54 } 55 56 TEST(StdIoStream, TryReadToEndOfFile) { 57 // Check that tryRead works when eof is reached before minBytes. 58 59 ::std::stringstream ss; 60 61 StdInputStream in(ss); 62 StdOutputStream out(ss); 63 64 const void* bytes = "foobar"; 65 66 out.write(bytes, 6); 67 68 char buf[9]; 69 in.tryRead(buf, 8, 8); 70 buf[6] = '\0'; 71 72 EXPECT_STREQ("foobar", buf); 73 } 74 75 TEST(StdIoStream, ReadToEndOfFile) { 76 // Check that read throws an exception when eof is reached before specified 77 // bytes. 78 79 ::std::stringstream ss; 80 81 StdInputStream in(ss); 82 StdOutputStream out(ss); 83 84 const void* bytes = "foobar"; 85 86 out.write(bytes, 6); 87 88 char buf[9]; 89 90 Maybe<Exception> e = kj::runCatchingExceptions([&]() { 91 in.read(buf, 8, 8); 92 }); 93 buf[6] = '\0'; 94 95 ASSERT_FALSE(e == nullptr); 96 97 // Ensure that the value is still read up to the EOF. 98 EXPECT_STREQ("foobar", buf); 99 } 100 101 } // namespace 102 } // namespace std 103 } // namespace kj