1 //===- MemoryAreaTest.cpp -------------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #include <mcld/Support/FileHandle.h> 10 #include <mcld/Support/FileSystem.h> 11 #include <mcld/Support/MemoryArea.h> 12 #include <mcld/Support/MemoryRegion.h> 13 #include <mcld/Support/MemoryAreaFactory.h> 14 #include <mcld/Support/Path.h> 15 16 #include "MemoryAreaTest.h" 17 #include <fcntl.h> 18 #include <cstdio> 19 20 using namespace mcld; 21 using namespace mcld::sys::fs; 22 using namespace mcldtest; 23 24 25 // Constructor can do set-up work for all test here. 26 MemoryAreaTest::MemoryAreaTest() 27 { 28 } 29 30 // Destructor can do clean-up work that doesn't throw exceptions here. 31 MemoryAreaTest::~MemoryAreaTest() 32 { 33 } 34 35 // SetUp() will be called immediately before each test. 36 void MemoryAreaTest::SetUp() 37 { 38 } 39 40 // TearDown() will be called immediately after each test. 41 void MemoryAreaTest::TearDown() 42 { 43 } 44 45 //==========================================================================// 46 // Testcases 47 // 48 TEST_F( MemoryAreaTest, read_by_malloc ) 49 { 50 Path path(TOPDIR); 51 path.append("unittests/test3.txt"); 52 53 MemoryAreaFactory *AreaFactory = new MemoryAreaFactory(1); 54 MemoryArea* area = AreaFactory->produce(path, FileHandle::ReadOnly); 55 MemoryRegion* region = area->request(3, 2); 56 ASSERT_EQ('L', region->getBuffer()[0]); 57 ASSERT_EQ('O', region->getBuffer()[1]); 58 area->release(region); 59 AreaFactory->destruct(area); 60 //delete AreaFactory;; 61 } 62 63 TEST_F( MemoryAreaTest, write_by_malloc ) 64 { 65 Path path(TOPDIR); 66 path.append("unittests/test2.txt"); 67 MemoryAreaFactory *AreaFactory = new MemoryAreaFactory(1); 68 MemoryArea* area = AreaFactory->produce(path, FileHandle::ReadOnly); 69 ASSERT_TRUE(area->handler()->isOpened()); 70 ASSERT_TRUE(area->handler()->isGood()); 71 MemoryRegion* region = area->request(3, 4); 72 region->getBuffer()[0] = 'L'; 73 region->getBuffer()[1] = 'i'; 74 region->getBuffer()[2] = 'n'; 75 region->getBuffer()[3] = 'k'; 76 area->release(region); 77 area->clear(); 78 area->handler()->close(); 79 80 area->handler()->open(path, FileHandle::ReadOnly); 81 ASSERT_TRUE(area->handler()->isOpened()); 82 ASSERT_TRUE(area->handler()->isGood()); 83 region = area->request(5, 2); 84 ASSERT_EQ('n', region->getBuffer()[0]); 85 ASSERT_EQ('k', region->getBuffer()[1]); 86 area->release(region); 87 AreaFactory->destruct(area); 88 //delete AreaFactory;; 89 } 90 91 TEST_F( MemoryAreaTest, read_one_page ) 92 { 93 Path path(TOPDIR) ; 94 path.append("unittests/test3.txt") ; 95 MemoryAreaFactory *AreaFactory = new MemoryAreaFactory(1) ; 96 MemoryArea* area = AreaFactory->produce(path, FileHandle::ReadOnly) ; 97 ASSERT_TRUE(area->handler()->isOpened()) ; 98 ASSERT_TRUE(area->handler()->isGood()) ; 99 MemoryRegion* region = area->request(0, 4096); 100 ASSERT_EQ('H', region->getBuffer()[0]); 101 ASSERT_EQ('E', region->getBuffer()[1]); 102 area->release(region); 103 AreaFactory->destruct(area); 104 //delete AreaFactory; ; 105 } 106 107 TEST_F( MemoryAreaTest, write_one_page ) 108 { 109 Path path(TOPDIR) ; 110 path.append("unittests/test2.txt") ; 111 MemoryAreaFactory *AreaFactory = new MemoryAreaFactory(1) ; 112 MemoryArea* area = AreaFactory->produce(path, FileHandle::ReadWrite); 113 ASSERT_TRUE(area->handler()->isOpened()) ; 114 ASSERT_TRUE(area->handler()->isGood()) ; 115 MemoryRegion* region = area->request(0, 4096) ; 116 region->getBuffer()[4000] = 'K' ; 117 region->getBuffer()[4001] = 'R' ; 118 ASSERT_EQ('K', region->getBuffer()[4000]); 119 ASSERT_EQ('R', region->getBuffer()[4001]); 120 area->release(region); 121 area->clear() ; 122 area->handler()->close(); 123 124 area->handler()->open(path, FileHandle::ReadOnly); 125 region = area->request(4000, 4); 126 ASSERT_EQ('K', region->getBuffer()[0]); 127 ASSERT_EQ('R', region->getBuffer()[1]); 128 region->getBuffer()[0] = 'O' ; 129 region->getBuffer()[1] = 'H' ; 130 area->clear() ; 131 AreaFactory->destruct(area); 132 //delete AreaFactory; ; 133 } 134 135 TEST_F( MemoryAreaTest, write_sync ) 136 { 137 Path path(TOPDIR) ; 138 path.append("unittests/test2.txt") ; 139 MemoryAreaFactory *AreaFactory = new MemoryAreaFactory(1) ; 140 MemoryArea* area = AreaFactory->produce(path, FileHandle::ReadWrite) ; 141 ASSERT_TRUE(area->handler()->isOpened()) ; 142 ASSERT_TRUE(area->handler()->isGood()) ; 143 MemoryRegion* region1 = area->request(0, 4096) ; 144 MemoryRegion* region2 = area->request(512, 1024) ; 145 region1->getBuffer()[1000] = 'L' ; 146 region1->getBuffer()[1001] = 'L' ; 147 region2->getBuffer()[488] = 'V' ; 148 region2->getBuffer()[489] = 'M' ; 149 area->release(region1); 150 area->release(region2); 151 area->clear(); 152 area->handler()->close(); 153 area->handler()->open(path, FileHandle::ReadWrite); 154 region1 = area->request(0, 1024) ; 155 EXPECT_EQ('V', region1->getBuffer()[1000]) ; 156 EXPECT_EQ('M', region1->getBuffer()[1001]) ; 157 region1->getBuffer()[1000] = '@' ; 158 region1->getBuffer()[1001] = '@' ; 159 area->release(region1); 160 area->clear(); 161 AreaFactory->destruct(area); 162 //delete AreaFactory; ; 163 } 164 165 166