Don't access ivars in tests Add some helper methods for tests.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/bdf9ac40 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/bdf9ac40 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/bdf9ac40 Branch: refs/heads/master Commit: bdf9ac404628369185b87767a421a3c8eb745661 Parents: 572d356 Author: Nick Wellnhofer <[email protected]> Authored: Fri Jul 8 17:51:12 2016 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Fri Jul 8 17:53:40 2016 +0200 ---------------------------------------------------------------------- core/Lucy/Store/FSFileHandle.c | 8 +++ core/Lucy/Store/FSFileHandle.cfh | 5 ++ core/Lucy/Store/InStream.c | 15 +++++ core/Lucy/Store/InStream.cfh | 15 +++++ core/Lucy/Util/MemoryPool.c | 5 ++ core/Lucy/Util/MemoryPool.cfh | 5 ++ test/Lucy/Test/Store/TestFSFileHandle.c | 19 ++++--- test/Lucy/Test/Store/TestIOChunks.c | 13 ++--- test/Lucy/Test/Store/TestInStream.c | 81 +++++++++++++-------------- test/Lucy/Test/Store/TestRAMFileHandle.c | 13 +++-- test/Lucy/Test/Util/TestMemoryPool.c | 6 +- 11 files changed, 121 insertions(+), 64 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/core/Lucy/Store/FSFileHandle.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Store/FSFileHandle.c b/core/Lucy/Store/FSFileHandle.c index d2431fa..235d8f1 100644 --- a/core/Lucy/Store/FSFileHandle.c +++ b/core/Lucy/Store/FSFileHandle.c @@ -245,6 +245,14 @@ FSFH_Window_IMP(FSFileHandle *self, FileWindow *window, int64_t offset, } } +int +FSFH_Set_FD_IMP(FSFileHandle *self, int fd) { + FSFileHandleIVARS *const ivars = FSFH_IVARS(self); + int old_fd = ivars->fd; + ivars->fd = fd; + return old_fd; +} + /********************************* 64-bit *********************************/ #if IS_64_BIT http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/core/Lucy/Store/FSFileHandle.cfh ---------------------------------------------------------------------- diff --git a/core/Lucy/Store/FSFileHandle.cfh b/core/Lucy/Store/FSFileHandle.cfh index c36e5b5..679745e 100644 --- a/core/Lucy/Store/FSFileHandle.cfh +++ b/core/Lucy/Store/FSFileHandle.cfh @@ -57,6 +57,11 @@ class Lucy::Store::FSFileHandle nickname FSFH bool Close(FSFileHandle *self); + + /** Only for testing. + */ + int + Set_FD(FSFileHandle *self, int fd); } http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/core/Lucy/Store/InStream.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Store/InStream.c b/core/Lucy/Store/InStream.c index 273a79e..7d1010c 100644 --- a/core/Lucy/Store/InStream.c +++ b/core/Lucy/Store/InStream.c @@ -535,4 +535,19 @@ InStream_Read_Raw_C64_IMP(InStream *self, char *buf) { return (int)(dest - (uint8_t*)buf); } +FileWindow* +InStream_Get_Window_IMP(InStream *self) { + return InStream_IVARS(self)->window; +} + +FileHandle* +InStream_Get_Handle_IMP(InStream *self) { + return InStream_IVARS(self)->file_handle; +} + +int64_t +InStream_Bytes_In_Buf_IMP(InStream *self) { + InStreamIVARS *const ivars = InStream_IVARS(self); + return (int64_t)(ivars->limit - ivars->buf); +} http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/core/Lucy/Store/InStream.cfh ---------------------------------------------------------------------- diff --git a/core/Lucy/Store/InStream.cfh b/core/Lucy/Store/InStream.cfh index 3d96860..4a38b7d 100644 --- a/core/Lucy/Store/InStream.cfh +++ b/core/Lucy/Store/InStream.cfh @@ -204,6 +204,21 @@ class Lucy::Store::InStream inherits Clownfish::Obj { */ String* Get_Filename(InStream *self); + + /** Only for testing. + */ + FileWindow* + Get_Window(InStream *self); + + /** Only for testing. + */ + FileHandle* + Get_Handle(InStream *self); + + /** Only for testing. + */ + int64_t + Bytes_In_Buf(InStream *self); } http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/core/Lucy/Util/MemoryPool.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Util/MemoryPool.c b/core/Lucy/Util/MemoryPool.c index ac89873..5755bf0 100644 --- a/core/Lucy/Util/MemoryPool.c +++ b/core/Lucy/Util/MemoryPool.c @@ -157,3 +157,8 @@ MemPool_Release_All_IMP(MemoryPool *self) { ivars->consumed = 0; } +char* +MemPool_Get_Buf_IMP(MemoryPool *self) { + return MemPool_IVARS(self)->buf; +} + http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/core/Lucy/Util/MemoryPool.cfh ---------------------------------------------------------------------- diff --git a/core/Lucy/Util/MemoryPool.cfh b/core/Lucy/Util/MemoryPool.cfh index 30216e4..5cc8ada 100644 --- a/core/Lucy/Util/MemoryPool.cfh +++ b/core/Lucy/Util/MemoryPool.cfh @@ -67,6 +67,11 @@ class Lucy::Util::MemoryPool nickname MemPool public void Destroy(MemoryPool *self); + + /** Only for testing. + */ + char* + Get_Buf(MemoryPool *self); } http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/test/Lucy/Test/Store/TestFSFileHandle.c ---------------------------------------------------------------------- diff --git a/test/Lucy/Test/Store/TestFSFileHandle.c b/test/Lucy/Test/Store/TestFSFileHandle.c index 7f9f2f1..518b71c 100644 --- a/test/Lucy/Test/Store/TestFSFileHandle.c +++ b/test/Lucy/Test/Store/TestFSFileHandle.c @@ -187,14 +187,13 @@ test_Close(TestBatchRunner *runner) { #ifdef _MSC_VER SKIP(runner, 2, "LUCY-155"); #else - int saved_fd = FSFH_IVARS(fh)->fd; - FSFH_IVARS(fh)->fd = -1; + int saved_fd = FSFH_Set_FD(fh, -1); Err_set_error(NULL); bool result = FSFH_Close(fh); TEST_FALSE(runner, result, "Failed Close() returns false"); TEST_TRUE(runner, Err_get_error() != NULL, "Failed Close() sets global error"); - FSFH_IVARS(fh)->fd = saved_fd; + FSFH_Set_FD(fh, saved_fd); #endif /* _MSC_VER */ DECREF(fh); @@ -210,7 +209,6 @@ test_Window(TestBatchRunner *runner) { String *test_filename = SSTR_WRAP_C("_fstest"); FSFileHandle *fh; FileWindow *window = FileWindow_new(); - FileWindowIVARS *const window_ivars = FileWindow_IVARS(window); uint32_t i; S_remove(test_filename); @@ -240,15 +238,20 @@ test_Window(TestBatchRunner *runner) { TEST_TRUE(runner, FSFH_Window(fh, window, 1021, 2), "Window() returns true"); + const char *buf = FileWindow_Get_Buf(window); + int64_t offset = FileWindow_Get_Offset(window); TEST_TRUE(runner, - strncmp(window_ivars->buf - window_ivars->offset + 1021, "oo", 2) == 0, + strncmp(buf - offset + 1021, "oo", 2) == 0, "Window()"); TEST_TRUE(runner, FSFH_Release_Window(fh, window), "Release_Window() returns true"); - TEST_TRUE(runner, window_ivars->buf == NULL, "Release_Window() resets buf"); - TEST_TRUE(runner, window_ivars->offset == 0, "Release_Window() resets offset"); - TEST_TRUE(runner, window_ivars->len == 0, "Release_Window() resets len"); + TEST_TRUE(runner, FileWindow_Get_Buf(window) == NULL, + "Release_Window() resets buf"); + TEST_INT_EQ(runner, FileWindow_Get_Offset(window), 0, + "Release_Window() resets offset"); + TEST_INT_EQ(runner, FileWindow_Get_Len(window), 0, + "Release_Window() resets len"); DECREF(window); DECREF(fh); http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/test/Lucy/Test/Store/TestIOChunks.c ---------------------------------------------------------------------- diff --git a/test/Lucy/Test/Store/TestIOChunks.c b/test/Lucy/Test/Store/TestIOChunks.c index bf35d0b..9a1f2eb 100644 --- a/test/Lucy/Test/Store/TestIOChunks.c +++ b/test/Lucy/Test/Store/TestIOChunks.c @@ -87,25 +87,24 @@ test_Buf(TestBatchRunner *runner) { OutStream_Close(outstream); instream = InStream_open((Obj*)file); - InStreamIVARS *const ivars = InStream_IVARS(instream); buf = InStream_Buf(instream, 5); - TEST_INT_EQ(runner, ivars->limit - buf, IO_STREAM_BUF_SIZE, + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), IO_STREAM_BUF_SIZE, "Small request bumped up"); buf += IO_STREAM_BUF_SIZE - 10; // 10 bytes left in buffer. InStream_Advance_Buf(instream, buf); buf = InStream_Buf(instream, 10); - TEST_INT_EQ(runner, ivars->limit - buf, 10, + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), 10, "Exact request doesn't trigger refill"); buf = InStream_Buf(instream, 11); - TEST_INT_EQ(runner, ivars->limit - buf, IO_STREAM_BUF_SIZE, + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), IO_STREAM_BUF_SIZE, "Requesting over limit triggers refill"); - int64_t expected = InStream_Length(instream) - InStream_Tell(instream); - const char *buff = InStream_Buf(instream, 100000); - int64_t got = CHY_PTR_TO_I64(ivars->limit) - CHY_PTR_TO_I64(buff); + int64_t expected = InStream_Length(instream) - InStream_Tell(instream); + InStream_Buf(instream, 100000); + int64_t got = InStream_Bytes_In_Buf(instream); TEST_TRUE(runner, got == expected, "Requests greater than file size get pared down"); http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/test/Lucy/Test/Store/TestInStream.c ---------------------------------------------------------------------- diff --git a/test/Lucy/Test/Store/TestInStream.c b/test/Lucy/Test/Store/TestInStream.c index c32f985..286dffd 100644 --- a/test/Lucy/Test/Store/TestInStream.c +++ b/test/Lucy/Test/Store/TestInStream.c @@ -41,8 +41,8 @@ test_refill(TestBatchRunner *runner) { RAMFile *file = RAMFile_new(NULL, false); OutStream *outstream = OutStream_open((Obj*)file); InStream *instream; + FileWindow *window; char scratch[5]; - InStreamIVARS *ivars; for (int32_t i = 0; i < 1023; i++) { OutStream_Write_U8(outstream, 'x'); @@ -52,41 +52,38 @@ test_refill(TestBatchRunner *runner) { OutStream_Close(outstream); instream = InStream_open((Obj*)file); - ivars = InStream_IVARS(instream); InStream_Refill(instream); - TEST_INT_EQ(runner, ivars->limit - ivars->buf, IO_STREAM_BUF_SIZE, + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), IO_STREAM_BUF_SIZE, "Refill"); TEST_INT_EQ(runner, (long)InStream_Tell(instream), 0, "Correct file pos after standing-start Refill()"); DECREF(instream); instream = InStream_open((Obj*)file); - ivars = InStream_IVARS(instream); InStream_Fill(instream, 30); - TEST_INT_EQ(runner, ivars->limit - ivars->buf, 30, "Fill()"); + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), 30, "Fill()"); TEST_INT_EQ(runner, (long)InStream_Tell(instream), 0, "Correct file pos after standing-start Fill()"); DECREF(instream); instream = InStream_open((Obj*)file); - ivars = InStream_IVARS(instream); InStream_Read_Bytes(instream, scratch, 5); - TEST_INT_EQ(runner, ivars->limit - ivars->buf, + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), IO_STREAM_BUF_SIZE - 5, "small read triggers refill"); DECREF(instream); instream = InStream_open((Obj*)file); - ivars = InStream_IVARS(instream); TEST_INT_EQ(runner, InStream_Read_U8(instream), 'x', "Read_U8"); InStream_Seek(instream, 1023); - TEST_INT_EQ(runner, (long)FileWindow_IVARS(ivars->window)->offset, 0, + window = InStream_Get_Window(instream); + TEST_INT_EQ(runner, FileWindow_Get_Offset(window), 0, "no unnecessary refill on Seek"); TEST_INT_EQ(runner, (long)InStream_Tell(instream), 1023, "Seek/Tell"); TEST_INT_EQ(runner, InStream_Read_U8(instream), 'y', "correct data after in-buffer Seek()"); TEST_INT_EQ(runner, InStream_Read_U8(instream), 'z', "automatic Refill"); - TEST_TRUE(runner, (FileWindow_IVARS(ivars->window)->offset != 0), - "refilled"); + window = InStream_Get_Window(instream); + TEST_TRUE(runner, FileWindow_Get_Offset(window) != 0, "refilled"); DECREF(instream); DECREF(outstream); @@ -149,7 +146,7 @@ test_Close(TestBatchRunner *runner) { RAMFile *file = RAMFile_new(NULL, false); InStream *instream = InStream_open((Obj*)file); InStream_Close(instream); - TEST_TRUE(runner, InStream_IVARS(instream)->file_handle == NULL, + TEST_TRUE(runner, InStream_Get_Handle(instream) == NULL, "Close decrements FileHandle's refcount"); DECREF(instream); DECREF(file); @@ -163,51 +160,53 @@ test_Seek_and_Tell(TestBatchRunner *runner) { int64_t gb12 = gb1 * 12; FileHandle *fh = (FileHandle*)MockFileHandle_new(NULL, gb12); InStream *instream = InStream_open((Obj*)fh); - InStreamIVARS *const ivars = InStream_IVARS(instream); + FileWindow *window; InStream_Buf(instream, 10000); - TEST_TRUE(runner, ivars->limit == ((char*)NULL) + 10000, - "InStream_Buf sets limit"); + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), 10000, + "InStream_Buf sets limit"); InStream_Seek(instream, gb6); - TEST_TRUE(runner, InStream_Tell(instream) == gb6, - "Tell after seek forwards outside buffer"); - TEST_TRUE(runner, ivars->buf == NULL, + TEST_INT_EQ(runner, InStream_Tell(instream), gb6, + "Tell after seek forwards outside buffer"); + TEST_TRUE(runner, InStream_Buf(instream, 0) == NULL, "Seek forwards outside buffer sets buf to NULL"); - TEST_TRUE(runner, ivars->limit == NULL, - "Seek forwards outside buffer sets limit to NULL"); - TEST_TRUE(runner, FileWindow_IVARS(ivars->window)->offset == gb6, - "Seek forwards outside buffer tracks pos in window offset"); + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), 0, + "Seek forwards outside buffer sets limit to NULL"); + window = InStream_Get_Window(instream); + TEST_INT_EQ(runner, FileWindow_Get_Offset(window), gb6, + "Seek forwards outside buffer tracks pos in window offset"); InStream_Buf(instream, (size_t)gb1); - TEST_TRUE(runner, ivars->limit == ((char*)NULL) + gb1, - "InStream_Buf sets limit"); + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), gb1, + "InStream_Buf sets limit"); InStream_Seek(instream, gb6 + 10); - TEST_TRUE(runner, InStream_Tell(instream) == gb6 + 10, - "Tell after seek forwards within buffer"); - TEST_TRUE(runner, ivars->buf == ((char*)NULL) + 10, + TEST_INT_EQ(runner, InStream_Tell(instream), gb6 + 10, + "Tell after seek forwards within buffer"); + TEST_TRUE(runner, InStream_Buf(instream, 0) == ((char*)NULL) + 10, "Seek within buffer sets buf"); - TEST_TRUE(runner, ivars->limit == ((char*)NULL) + gb1, - "Seek within buffer leaves limit alone"); + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), gb1 - 10, + "Seek within buffer leaves limit alone"); InStream_Seek(instream, gb6 + 1); - TEST_TRUE(runner, InStream_Tell(instream) == gb6 + 1, - "Tell after seek backwards within buffer"); - TEST_TRUE(runner, ivars->buf == ((char*)NULL) + 1, + TEST_INT_EQ(runner, InStream_Tell(instream), gb6 + 1, + "Tell after seek backwards within buffer"); + TEST_TRUE(runner, InStream_Buf(instream, 0) == ((char*)NULL) + 1, "Seek backwards within buffer sets buf"); - TEST_TRUE(runner, ivars->limit == ((char*)NULL) + gb1, - "Seek backwards within buffer leaves limit alone"); + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), gb1 - 1, + "Seek backwards within buffer leaves limit alone"); InStream_Seek(instream, gb3); - TEST_TRUE(runner, InStream_Tell(instream) == gb3, - "Tell after seek backwards outside buffer"); - TEST_TRUE(runner, ivars->buf == NULL, + TEST_INT_EQ(runner, InStream_Tell(instream), gb3, + "Tell after seek backwards outside buffer"); + TEST_TRUE(runner, InStream_Buf(instream, 0) == NULL, "Seek backwards outside buffer sets buf to NULL"); - TEST_TRUE(runner, ivars->limit == NULL, - "Seek backwards outside buffer sets limit to NULL"); - TEST_TRUE(runner, FileWindow_IVARS(ivars->window)->offset == gb3, - "Seek backwards outside buffer tracks pos in window offset"); + TEST_INT_EQ(runner, InStream_Bytes_In_Buf(instream), 0, + "Seek backwards outside buffer sets limit to NULL"); + window = InStream_Get_Window(instream); + TEST_INT_EQ(runner, FileWindow_Get_Offset(window), gb3, + "Seek backwards outside buffer tracks pos in window offset"); DECREF(instream); DECREF(fh); http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/test/Lucy/Test/Store/TestRAMFileHandle.c ---------------------------------------------------------------------- diff --git a/test/Lucy/Test/Store/TestRAMFileHandle.c b/test/Lucy/Test/Store/TestRAMFileHandle.c index e208784..f8d0f80 100644 --- a/test/Lucy/Test/Store/TestRAMFileHandle.c +++ b/test/Lucy/Test/Store/TestRAMFileHandle.c @@ -126,7 +126,6 @@ test_Window(TestBatchRunner *runner) { RAMFile *file = RAMFile_new(NULL, false); RAMFileHandle *fh = RAMFH_open(NULL, FH_WRITE_ONLY, file); FileWindow *window = FileWindow_new(); - FileWindowIVARS *const window_ivars = FileWindow_IVARS(window); for (uint32_t i = 0; i < 1024; i++) { RAMFH_Write(fh, "foo ", 4); @@ -151,13 +150,17 @@ test_Window(TestBatchRunner *runner) { TEST_TRUE(runner, RAMFH_Window(fh, window, 1021, 2), "Window() returns true"); - TEST_TRUE(runner, strncmp(window_ivars->buf, "oo", 2) == 0, "Window()"); + TEST_TRUE(runner, strncmp(FileWindow_Get_Buf(window), "oo", 2) == 0, + "Window()"); TEST_TRUE(runner, RAMFH_Release_Window(fh, window), "Release_Window() returns true"); - TEST_TRUE(runner, window_ivars->buf == NULL, "Release_Window() resets buf"); - TEST_TRUE(runner, window_ivars->offset == 0, "Release_Window() resets offset"); - TEST_TRUE(runner, window_ivars->len == 0, "Release_Window() resets len"); + TEST_TRUE(runner, FileWindow_Get_Buf(window) == NULL, + "Release_Window() resets buf"); + TEST_TRUE(runner, FileWindow_Get_Offset(window) == 0, + "Release_Window() resets offset"); + TEST_TRUE(runner, FileWindow_Get_Len(window) == 0, + "Release_Window() resets len"); DECREF(window); DECREF(fh); http://git-wip-us.apache.org/repos/asf/lucy/blob/bdf9ac40/test/Lucy/Test/Util/TestMemoryPool.c ---------------------------------------------------------------------- diff --git a/test/Lucy/Test/Util/TestMemoryPool.c b/test/Lucy/Test/Util/TestMemoryPool.c index 7e960ab..f21cf44 100644 --- a/test/Lucy/Test/Util/TestMemoryPool.c +++ b/test/Lucy/Test/Util/TestMemoryPool.c @@ -34,7 +34,6 @@ TestMemPool_Run_IMP(TestMemoryPool *self, TestBatchRunner *runner) { TestBatchRunner_Plan(runner, (TestBatch*)self, 5); MemoryPool *mem_pool = MemPool_new(0); - MemoryPoolIVARS *const ivars = MemPool_IVARS(mem_pool); char *ptr_a, *ptr_b; ptr_a = (char*)MemPool_Grab(mem_pool, 10); @@ -45,9 +44,10 @@ TestMemPool_Run_IMP(TestMemoryPool *self, TestBatchRunner *runner) { TEST_UINT_EQ(runner, MemPool_Get_Consumed(mem_pool), expected * 2, "Accumulate consumed."); - ptr_a = ivars->buf; + ptr_a = MemPool_Get_Buf(mem_pool); MemPool_Resize(mem_pool, ptr_b, 6); - TEST_TRUE(runner, ivars->buf < ptr_a, "Resize adjusts next allocation"); + TEST_TRUE(runner, MemPool_Get_Buf(mem_pool) < ptr_a, + "Resize adjusts next allocation"); TEST_TRUE(runner, MemPool_Get_Consumed(mem_pool) < expected * 2, "Resize() adjusts `consumed`");
