1 //===- FactoriesTest.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 <cstdlib> 10 #include "FactoriesTest.h" 11 #include <string> 12 13 using namespace mcld; 14 using namespace mcldtest; 15 16 17 // Constructor can do set-up work for all test here. 18 FactoriesTest::FactoriesTest() 19 { 20 m_pNodeAlloc = new NodeAlloc(); 21 m_pFileAlloc = new FileAlloc(); 22 } 23 24 // Destructor can do clean-up work that doesn't throw exceptions here. 25 FactoriesTest::~FactoriesTest() 26 { 27 delete m_pNodeAlloc; 28 delete m_pFileAlloc; 29 } 30 31 // SetUp() will be called immediately before each test. 32 void FactoriesTest::SetUp() 33 { 34 } 35 36 // TearDown() will be called immediately after each test. 37 void FactoriesTest::TearDown() 38 { 39 } 40 41 //==========================================================================// 42 // Testcases 43 // 44 TEST_F( FactoriesTest, node_produce ) { 45 NodeAlloc::NodeType* node = m_pNodeAlloc->produce(); 46 ASSERT_EQ(1, m_pNodeAlloc->size()); 47 ASSERT_FALSE(m_pNodeAlloc->empty()); 48 node = m_pNodeAlloc->produce(); 49 ASSERT_EQ(2, m_pNodeAlloc->size()); 50 ASSERT_FALSE(m_pNodeAlloc->empty()); 51 node = m_pNodeAlloc->produce(); 52 ASSERT_EQ(3, m_pNodeAlloc->size()); 53 ASSERT_FALSE(m_pNodeAlloc->empty()); 54 } 55 56 TEST_F( FactoriesTest, node_iterate ) { 57 NodeAlloc::NodeType* node = 0; 58 for (int i=0 ; i<100; ++i) { 59 node = m_pNodeAlloc->produce(); 60 node->data = (int*)malloc(sizeof(int)); 61 *(node->data) = i; 62 } 63 64 int counter = 0; 65 NodeAlloc::iterator data = m_pNodeAlloc->begin(); 66 NodeAlloc::iterator dEnd = m_pNodeAlloc->end(); 67 for (; data!=dEnd; ++data) { 68 ASSERT_EQ(counter, *(*data).data ); 69 free((*data).data); 70 (*data).data = 0; 71 ++counter; 72 } 73 } 74 75 TEST_F( FactoriesTest, node_delegate_empty ) { 76 NodeAlloc::NodeType* node = 0; 77 for (int i=0 ; i<100; ++i) { 78 node = m_pNodeAlloc->produce(); 79 node->data = (int*)malloc(sizeof(int)); 80 *(node->data) = i; 81 } 82 NodeAlloc* delegatee = new NodeAlloc(); 83 m_pNodeAlloc->delegate(*delegatee); 84 ASSERT_EQ(100, m_pNodeAlloc->size()); 85 int counter = 0; 86 NodeAlloc::iterator data = m_pNodeAlloc->begin(); 87 NodeAlloc::iterator dEnd = m_pNodeAlloc->end(); 88 for (; data!=dEnd; ++data) { 89 ASSERT_EQ(counter, *(*data).data ); 90 free((*data).data); 91 (*data).data = 0; 92 ++counter; 93 } 94 delete delegatee; 95 } 96 97 TEST_F( FactoriesTest, node_empty_delegate ) { 98 NodeAlloc::NodeType* node = 0; 99 NodeAlloc* delegatee = new NodeAlloc(); 100 for (int i=0 ; i<100; ++i) { 101 node = delegatee->produce(); 102 node->data = (int*)malloc(sizeof(int)); 103 *(node->data) = i; 104 } 105 m_pNodeAlloc->delegate(*delegatee); 106 ASSERT_EQ(100, m_pNodeAlloc->size()); 107 int counter = 0; 108 NodeAlloc::iterator data = m_pNodeAlloc->begin(); 109 NodeAlloc::iterator dEnd = m_pNodeAlloc->end(); 110 for (; data!=dEnd; ++data) { 111 ASSERT_EQ(counter, *(*data).data ); 112 free((*data).data); 113 (*data).data = 0; 114 ++counter; 115 } 116 ASSERT_EQ(0, delegatee->size()); 117 ASSERT_TRUE(delegatee->empty()); 118 delete delegatee; 119 } 120 121 TEST_F( FactoriesTest, node_delegate ) { 122 NodeAlloc::NodeType* node = 0; 123 NodeAlloc* delegatee = new NodeAlloc(); 124 int counter = 0; 125 // produce agent 126 for (int i=0 ; i<100; ++i) { 127 node = m_pNodeAlloc->produce(); 128 node->data = (int*)malloc(sizeof(int)); 129 *(node->data) = counter; 130 ++counter; 131 } 132 133 // produce delegatee 134 for (int i=0 ; i<100; ++i) { 135 node = delegatee->produce(); 136 node->data = (int*)malloc(sizeof(int)); 137 *(node->data) = counter; 138 ++counter; 139 } 140 141 m_pNodeAlloc->delegate(*delegatee); 142 ASSERT_EQ(200, m_pNodeAlloc->size()); 143 ASSERT_FALSE(m_pNodeAlloc->empty()); 144 NodeAlloc::iterator data = m_pNodeAlloc->begin(); 145 NodeAlloc::iterator dEnd = m_pNodeAlloc->end(); 146 for ( counter = 0; data!=dEnd; ++data) { 147 ASSERT_EQ(counter, *(*data).data ); 148 free((*data).data); 149 (*data).data = 0; 150 ++counter; 151 } 152 ASSERT_EQ(0, delegatee->size()); 153 ASSERT_TRUE(delegatee->empty()); 154 delete delegatee; 155 } 156 157 TEST_F( FactoriesTest, node_delegate_self ) { 158 NodeAlloc::NodeType* node = 0; 159 for (int i=0 ; i<100; ++i) { 160 node = m_pNodeAlloc->produce(); 161 node->data = (int*)malloc(sizeof(int)); 162 *(node->data) = i; 163 } 164 ASSERT_EQ(100, m_pNodeAlloc->size()); 165 m_pNodeAlloc->delegate(*m_pNodeAlloc); 166 ASSERT_EQ(100, m_pNodeAlloc->size()); 167 ASSERT_FALSE(m_pNodeAlloc->empty()); 168 } 169 170 TEST_F( FactoriesTest, file_produce ) { 171 int counter = 0; 172 for (counter=1; counter<1000; ++counter) { 173 MCLDFile* file = m_pFileAlloc->produce(); 174 ASSERT_EQ(counter, m_pFileAlloc->size()); 175 ASSERT_FALSE(m_pFileAlloc->empty()); 176 } 177 } 178 179 TEST_F( FactoriesTest, file_produce_by_params ) { 180 int counter = 0; 181 for (counter=1; counter<1000; ++counter) { 182 char name[100]; 183 sprintf(name, "file %d", counter); 184 char path_name[100]; 185 sprintf(path_name, "/proj/mtk%d", counter); 186 MCLDFile* file = m_pFileAlloc->produce( string(name), 187 sys::fs::Path(string(path_name)), 188 MCLDFile::Archive); 189 ASSERT_EQ(counter, m_pFileAlloc->size()); 190 ASSERT_FALSE(m_pFileAlloc->empty()); 191 ASSERT_TRUE(file->isRecognized()); 192 ASSERT_STREQ(name, file->name().data()); 193 } 194 } 195 196 TEST_F( FactoriesTest, file_iterate ) { 197 int counter = 0; 198 for (counter=1; counter<1000; ++counter) { 199 char name[100]; 200 sprintf(name, "file %d", counter); 201 char path_name[100]; 202 sprintf(path_name, "/proj/mtk%d", counter); 203 MCLDFile* file = m_pFileAlloc->produce( string(name), 204 sys::fs::Path(string(path_name)), 205 MCLDFile::Archive); 206 } 207 208 ASSERT_EQ(counter-1, m_pFileAlloc->size()); 209 ASSERT_FALSE(m_pFileAlloc->empty()); 210 211 MCLDFileFactory::iterator file = m_pFileAlloc->begin(); 212 MCLDFileFactory::iterator fEnd = m_pFileAlloc->end(); 213 214 while (file!=fEnd) { 215 ASSERT_TRUE((*file).isRecognized()); 216 ASSERT_FALSE((*file).name().empty()); 217 ++file; 218 } 219 } 220 221