From 3675b33a2a77a16160e42783ec1199444cca0bf5 Mon Sep 17 00:00:00 2001 From: miloyip Date: Tue, 14 Apr 2015 10:35:45 +0800 Subject: [PATCH] Search more paths for different build situations --- test/unittest/documenttest.cpp | 19 ++++++++----- test/unittest/encodedstreamtest.cpp | 19 ++++++++----- test/unittest/filestreamtest.cpp | 18 ++++++++++--- test/unittest/jsoncheckertest.cpp | 41 ++++++++++++++++------------- 4 files changed, 64 insertions(+), 33 deletions(-) diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index bf6f3a2..dd4fb13 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -101,14 +101,21 @@ TEST(Document, Parse) { } static FILE* OpenEncodedFile(const char* filename) { + const char *paths[] = { + "encodings/%s", + "bin/encodings/%s", + "../bin/encodings/%s", + "../../bin/encodings/%s", + "../../../bin/encodings/%s" + }; char buffer[1024]; - sprintf(buffer, "encodings/%s", filename); - FILE *fp = fopen(buffer, "rb"); - if (!fp) { - sprintf(buffer, "../../bin/encodings/%s", filename); - fp = fopen(buffer, "rb"); + for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { + sprintf(buffer, paths[i], filename); + FILE *fp = fopen(buffer, "rb"); + if (fp) + return fp; } - return fp; + return 0; } TEST(Document, ParseStream_EncodedInputStream) { diff --git a/test/unittest/encodedstreamtest.cpp b/test/unittest/encodedstreamtest.cpp index 3580608..c2920a0 100644 --- a/test/unittest/encodedstreamtest.cpp +++ b/test/unittest/encodedstreamtest.cpp @@ -47,14 +47,21 @@ private: protected: static FILE* Open(const char* filename) { + const char *paths[] = { + "encodings/%s", + "bin/encodings/%s", + "../bin/encodings/%s", + "../../bin/encodings/%s", + "../../../bin/encodings/%s" + }; char buffer[1024]; - sprintf(buffer, "encodings/%s", filename); - FILE *fp = fopen(buffer, "rb"); - if (!fp) { - sprintf(buffer, "../../bin/encodings/%s", filename); - fp = fopen(buffer, "rb"); + for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { + sprintf(buffer, paths[i], filename); + FILE *fp = fopen(buffer, "rb"); + if (fp) + return fp; } - return fp; + return 0; } static char *ReadFile(const char* filename, bool appendPath, size_t* outLength) { diff --git a/test/unittest/filestreamtest.cpp b/test/unittest/filestreamtest.cpp index a176332..5b3df53 100644 --- a/test/unittest/filestreamtest.cpp +++ b/test/unittest/filestreamtest.cpp @@ -31,9 +31,21 @@ public: FileStreamTest() : filename_(), json_(), length_() {} virtual void SetUp() { - FILE *fp = fopen(filename_ = "data/sample.json", "rb"); - if (!fp) - fp = fopen(filename_ = "../../bin/data/sample.json", "rb"); + const char *paths[] = { + "data/sample.json", + "bin/data/sample.json", + "../bin/data/sample.json", + "../../bin/data/sample.json", + "../../../bin/data/sample.json" + }; + FILE* fp = 0; + for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { + fp = fopen(paths[i], "rb"); + if (fp) { + filename_ = paths[i]; + break; + } + } ASSERT_TRUE(fp != 0); fseek(fp, 0, SEEK_END); diff --git a/test/unittest/jsoncheckertest.cpp b/test/unittest/jsoncheckertest.cpp index caa1b8a..a89b8d2 100644 --- a/test/unittest/jsoncheckertest.cpp +++ b/test/unittest/jsoncheckertest.cpp @@ -25,9 +25,22 @@ using namespace rapidjson; static char* ReadFile(const char* filename, size_t& length) { - FILE *fp = fopen(filename, "rb"); - if (!fp) - fp = fopen(filename, "rb"); + const char *paths[] = { + "jsonchecker/%s", + "bin/jsonchecker/%s", + "../bin/jsonchecker/%s", + "../../bin/jsonchecker/%s", + "../../../bin/jsonchecker/%s" + }; + char buffer[1024]; + FILE *fp = 0; + for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { + sprintf(buffer, paths[i], filename); + fp = fopen(buffer, "rb"); + if (fp) + break; + } + if (!fp) return 0; @@ -51,17 +64,13 @@ TEST(JsonChecker, Reader) { if (i == 18) // fail18.json is valid in rapidjson, which has no limitation on depth of nesting. continue; - sprintf(filename, "jsonchecker/fail%d.json", i); + sprintf(filename, "fail%d.json", i); size_t length; char* json = ReadFile(filename, length); if (!json) { - sprintf(filename, "../../bin/jsonchecker/fail%d.json", i); - json = ReadFile(filename, length); - if (!json) { - printf("jsonchecker file %s not found", filename); - ADD_FAILURE(); - continue; - } + printf("jsonchecker file %s not found", filename); + ADD_FAILURE(); + continue; } GenericDocument, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) @@ -76,16 +85,12 @@ TEST(JsonChecker, Reader) { // passX.json for (int i = 1; i <= 3; i++) { - sprintf(filename, "jsonchecker/pass%d.json", i); + sprintf(filename, "pass%d.json", i); size_t length; char* json = ReadFile(filename, length); if (!json) { - sprintf(filename, "../../bin/jsonchecker/pass%d.json", i); - json = ReadFile(filename, length); - if (!json) { - printf("jsonchecker file %s not found", filename); - continue; - } + printf("jsonchecker file %s not found", filename); + continue; } GenericDocument, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)