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
diff --git a/src/core/SkFDStream.cpp b/src/core/SkFDStream.cpp
index 913dbe9..9e96248 100644
--- a/src/core/SkFDStream.cpp
+++ b/src/core/SkFDStream.cpp
@@ -7,7 +7,16 @@
*/
#include "SkStream.h"
-#ifndef SK_BUILD_FOR_WIN
+#ifdef SK_BUILD_FOR_WIN
+
+// -1 means isValid() will return false
+SkFDStream::SkFDStream(int, bool) : fFD(-1), fCloseWhenDone(false) {}
+SkFDStream::~SkFDStream() {}
+bool SkFDStream::rewind() { return false; }
+size_t SkFDStream::read(void*, size_t) { return 0; }
+const char* SkFDStream::getFileName() { return NULL; }
+
+#else
#include <unistd.h>
diff --git a/tests/StreamTest.cpp b/tests/StreamTest.cpp
index ed51aa7..3174511 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 @@
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 @@
data->unref();
}
delete[] dst;
+
+ if (skiatest::Test::GetTmpDir()) {
+ test_filestreams(reporter, skiatest::Test::GetTmpDir());
+ }
}
static void TestPackedUInt(skiatest::Reporter* reporter) {
diff --git a/tests/Test.h b/tests/Test.h
index 2dca0e6..98ade93 100644
--- a/tests/Test.h
+++ b/tests/Test.h
@@ -88,6 +88,8 @@
const char* getName();
bool run(); // returns true on success
+ static const char* GetTmpDir();
+
protected:
virtual void onGetName(SkString*) = 0;
virtual void onRun(Reporter*) = 0;
diff --git a/tests/skia_test.cpp b/tests/skia_test.cpp
index 18c8c84..e3df2d7 100644
--- a/tests/skia_test.cpp
+++ b/tests/skia_test.cpp
@@ -7,6 +7,7 @@
*/
#include "SkGraphics.h"
#include "Test.h"
+#include "SkOSFile.h"
#if SK_SUPPORT_GPU
#include "GrContext.h"
@@ -82,6 +83,28 @@
int fIndex, fTotal;
};
+static const char* make_canonical_dir_path(const char* path, SkString* storage) {
+ if (path) {
+ // clean it up so it always has a trailing searator
+ size_t len = strlen(path);
+ if (0 == len) {
+ path = NULL;
+ } else if (SkPATH_SEPARATOR != path[len - 1]) {
+ // resize to len + 1, to make room for searator
+ storage->set(path, len + 1);
+ storage->writable_str()[len] = SkPATH_SEPARATOR;
+ path = storage->c_str();
+ }
+ }
+ return path;
+}
+
+static const char* gTmpDir;
+
+const char* Test::GetTmpDir() {
+ return gTmpDir;
+}
+
int tool_main(int argc, char** argv);
int tool_main(int argc, char** argv) {
#if SK_ENABLE_INST_COUNT
@@ -97,15 +120,32 @@
++argv;
if (argv < stop && **argv) {
matchStr = *argv;
+ } else {
+ SkDebugf("no following argument to --match\n");
+ return -1;
+ }
+ } else if (0 == strcmp(*argv, "--tmpDir")) {
+ ++argv;
+ if (argv < stop && **argv) {
+ gTmpDir = *argv;
+ } else {
+ SkDebugf("no following argument to --tmpDir\n");
+ return -1;
}
}
}
+ SkString tmpDirStorage;
+ gTmpDir = make_canonical_dir_path(gTmpDir, &tmpDirStorage);
+
{
SkString header("Skia UnitTests:");
if (matchStr) {
header.appendf(" --match %s", matchStr);
}
+ if (gTmpDir) {
+ header.appendf(" --tmpDir %s", gTmpDir);
+ }
#ifdef SK_DEBUG
header.append(" SK_DEBUG");
#else