diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-02-25 20:24:24 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-02-25 20:24:24 +0000 |
commit | 789c6f291ecfff925086015360da525d6de1c835 (patch) | |
tree | 4e0b2f28a8bd8624ae8ca1670548a9799cd37a68 /tests/StreamTest.cpp | |
parent | 6e4e65066a7c0dbc9bfbfe4b8f5d49c3d8a79b59 (diff) |
add --tmpDir foo option to tests, to allow unittests that want to write/read files
use tmpDir in SkStream tests
git-svn-id: http://skia.googlecode.com/svn/trunk@7851 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests/StreamTest.cpp')
-rw-r--r-- | tests/StreamTest.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/StreamTest.cpp b/tests/StreamTest.cpp index ed51aa7baf..317451112d 100644 --- a/tests/StreamTest.cpp +++ b/tests/StreamTest.cpp @@ -10,6 +10,11 @@ #include "SkStream.h" #include "SkData.h" +#ifndef SK_BUILD_FOR_WIN +#include <unistd.h> +#include <fcntl.h> +#endif + #define MAX_SIZE (256 * 1024) static void random_fill(SkRandom& rand, void* buffer, size_t size) { @@ -73,6 +78,58 @@ static void TestRStream(skiatest::Reporter* reporter) { test_buffer(reporter); } +static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream, + const void* src, size_t len, int repeat) { + SkAutoSMalloc<256> storage(len); + void* tmp = storage.get(); + + for (int i = 0; i < repeat; ++i) { + size_t bytes = stream->read(tmp, len); + REPORTER_ASSERT(reporter, bytes == len); + REPORTER_ASSERT(reporter, !memcmp(tmp, src, len)); + } + + // expect EOF + size_t bytes = stream->read(tmp, 1); + REPORTER_ASSERT(reporter, 0 == bytes); +} + +static void test_filestreams(skiatest::Reporter* reporter, const char* tmpDir) { + SkString path; + path.printf("%s%s", tmpDir, "wstream_test"); + + const char s[] = "abcdefghijklmnopqrstuvwxyz"; + + { + SkFILEWStream writer(path.c_str()); + if (!writer.isValid()) { + SkString msg; + msg.printf("Failed to create tmp file %s\n", path.c_str()); + reporter->reportFailed(msg.c_str()); + return; + } + + for (int i = 0; i < 100; ++i) { + writer.write(s, 26); + } + } + + { + SkFILEStream stream(path.c_str()); + REPORTER_ASSERT(reporter, stream.isValid()); + test_loop_stream(reporter, &stream, s, 26, 100); + } + +#ifndef SK_BUILD_FOR_WIN + { + int fd = ::open(path.c_str(), O_RDONLY); + SkFDStream stream(fd, true); + REPORTER_ASSERT(reporter, stream.isValid()); + test_loop_stream(reporter, &stream, s, 26, 100); + } +#endif +} + static void TestWStream(skiatest::Reporter* reporter) { SkDynamicMemoryWStream ds; const char s[] = "abcdefghijklmnopqrstuvwxyz"; @@ -97,6 +154,10 @@ static void TestWStream(skiatest::Reporter* reporter) { data->unref(); } delete[] dst; + + if (skiatest::Test::GetTmpDir()) { + test_filestreams(reporter, skiatest::Test::GetTmpDir()); + } } static void TestPackedUInt(skiatest::Reporter* reporter) { |