11ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski//===- FactoriesTest.cpp --------------------------------------------------===// 21ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// 31ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// The MCLinker Project 41ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// 51ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// This file is distributed under the University of Illinois Open Source 61ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// License. See LICENSE.TXT for details. 71ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// 81ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski//===----------------------------------------------------------------------===// 91ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include <cstdlib> 101ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include "FactoriesTest.h" 111ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski#include <string> 121ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 131ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskiusing namespace mcld; 141ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskiusing namespace mcldtest; 151ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 161ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// Constructor can do set-up work for all test here. 171ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiFactoriesTest::FactoriesTest() { 181ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski m_pNodeAlloc = new NodeAlloc(); 191ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski m_pFileAlloc = new FileAlloc(); 201ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} 21467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski 22467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski// Destructor can do clean-up work that doesn't throw exceptions here. 231ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiFactoriesTest::~FactoriesTest() { 241ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski delete m_pNodeAlloc; 251ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski delete m_pFileAlloc; 261ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} 271ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 281ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// SetUp() will be called immediately before each test. 291ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskivoid FactoriesTest::SetUp() { 301ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} 311ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 321ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// TearDown() will be called immediately after each test. 331ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinskivoid FactoriesTest::TearDown() { 341ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} 351ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 361ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski//==========================================================================// 371ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// Testcases 381ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski// 391ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST_F(FactoriesTest, node_produce) { 401ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski NodeAlloc::NodeType* node = m_pNodeAlloc->produce(); 411ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski ASSERT_EQ(1, m_pNodeAlloc->size()); 421ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski ASSERT_FALSE(m_pNodeAlloc->empty()); 431ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski node = m_pNodeAlloc->produce(); 44467f171315f9c2037fcd3eb5edcfabc40671bf7bAdam Lesinski ASSERT_EQ(2, m_pNodeAlloc->size()); 451ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski ASSERT_FALSE(m_pNodeAlloc->empty()); 461ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski node = m_pNodeAlloc->produce(); 471ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski ASSERT_EQ(3, m_pNodeAlloc->size()); 481ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski ASSERT_FALSE(m_pNodeAlloc->empty()); 491ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski} 501ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski 511ab598f46c3ff520a67f9d80194847741f3467abAdam LesinskiTEST_F(FactoriesTest, node_iterate) { 521ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski NodeAlloc::NodeType* node = 0; 531ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski for (int i = 0; i < 100; ++i) { 541ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski node = m_pNodeAlloc->produce(); 551ab598f46c3ff520a67f9d80194847741f3467abAdam Lesinski node->data = (int*)malloc(sizeof(int)); 56 *(node->data) = i; 57 } 58 59 int counter = 0; 60 NodeAlloc::iterator data = m_pNodeAlloc->begin(); 61 NodeAlloc::iterator dEnd = m_pNodeAlloc->end(); 62 for (; data != dEnd; ++data) { 63 ASSERT_EQ(counter, *(*data).data); 64 free((*data).data); 65 (*data).data = 0; 66 ++counter; 67 } 68} 69 70TEST_F(FactoriesTest, node_delegate_empty) { 71 NodeAlloc::NodeType* node = 0; 72 for (int i = 0; i < 100; ++i) { 73 node = m_pNodeAlloc->produce(); 74 node->data = (int*)malloc(sizeof(int)); 75 *(node->data) = i; 76 } 77 NodeAlloc* delegatee = new NodeAlloc(); 78 m_pNodeAlloc->delegate(*delegatee); 79 ASSERT_EQ(100, m_pNodeAlloc->size()); 80 int counter = 0; 81 NodeAlloc::iterator data = m_pNodeAlloc->begin(); 82 NodeAlloc::iterator dEnd = m_pNodeAlloc->end(); 83 for (; data != dEnd; ++data) { 84 ASSERT_EQ(counter, *(*data).data); 85 free((*data).data); 86 (*data).data = 0; 87 ++counter; 88 } 89 delete delegatee; 90} 91 92TEST_F(FactoriesTest, node_empty_delegate) { 93 NodeAlloc::NodeType* node = 0; 94 NodeAlloc* delegatee = new NodeAlloc(); 95 for (int i = 0; i < 100; ++i) { 96 node = delegatee->produce(); 97 node->data = (int*)malloc(sizeof(int)); 98 *(node->data) = i; 99 } 100 m_pNodeAlloc->delegate(*delegatee); 101 ASSERT_EQ(100, m_pNodeAlloc->size()); 102 int counter = 0; 103 NodeAlloc::iterator data = m_pNodeAlloc->begin(); 104 NodeAlloc::iterator dEnd = m_pNodeAlloc->end(); 105 for (; data != dEnd; ++data) { 106 ASSERT_EQ(counter, *(*data).data); 107 free((*data).data); 108 (*data).data = 0; 109 ++counter; 110 } 111 ASSERT_EQ(0, delegatee->size()); 112 ASSERT_TRUE(delegatee->empty()); 113 delete delegatee; 114} 115 116TEST_F(FactoriesTest, node_delegate) { 117 NodeAlloc::NodeType* node = 0; 118 NodeAlloc* delegatee = new NodeAlloc(); 119 int counter = 0; 120 // produce agent 121 for (int i = 0; i < 100; ++i) { 122 node = m_pNodeAlloc->produce(); 123 node->data = (int*)malloc(sizeof(int)); 124 *(node->data) = counter; 125 ++counter; 126 } 127 128 // produce delegatee 129 for (int i = 0; i < 100; ++i) { 130 node = delegatee->produce(); 131 node->data = (int*)malloc(sizeof(int)); 132 *(node->data) = counter; 133 ++counter; 134 } 135 136 m_pNodeAlloc->delegate(*delegatee); 137 ASSERT_EQ(200, m_pNodeAlloc->size()); 138 ASSERT_FALSE(m_pNodeAlloc->empty()); 139 NodeAlloc::iterator data = m_pNodeAlloc->begin(); 140 NodeAlloc::iterator dEnd = m_pNodeAlloc->end(); 141 for (counter = 0; data != dEnd; ++data) { 142 ASSERT_EQ(counter, *(*data).data); 143 free((*data).data); 144 (*data).data = 0; 145 ++counter; 146 } 147 ASSERT_EQ(0, delegatee->size()); 148 ASSERT_TRUE(delegatee->empty()); 149 delete delegatee; 150} 151 152TEST_F(FactoriesTest, node_delegate_self) { 153 NodeAlloc::NodeType* node = 0; 154 for (int i = 0; i < 100; ++i) { 155 node = m_pNodeAlloc->produce(); 156 node->data = (int*)malloc(sizeof(int)); 157 *(node->data) = i; 158 } 159 ASSERT_EQ(100, m_pNodeAlloc->size()); 160 m_pNodeAlloc->delegate(*m_pNodeAlloc); 161 ASSERT_EQ(100, m_pNodeAlloc->size()); 162 ASSERT_FALSE(m_pNodeAlloc->empty()); 163} 164 165TEST_F(FactoriesTest, file_produce) { 166 int counter = 0; 167 for (counter = 1; counter < 1000; ++counter) { 168 MCLDFile* file = m_pFileAlloc->produce(); 169 ASSERT_EQ(counter, m_pFileAlloc->size()); 170 ASSERT_FALSE(m_pFileAlloc->empty()); 171 } 172} 173 174TEST_F(FactoriesTest, file_produce_by_params) { 175 int counter = 0; 176 for (counter = 1; counter < 1000; ++counter) { 177 char name[100]; 178 sprintf(name, "file %d", counter); 179 char path_name[100]; 180 sprintf(path_name, "/proj/mtk%d", counter); 181 MCLDFile* file = m_pFileAlloc->produce( 182 string(name), sys::fs::Path(string(path_name)), MCLDFile::Archive); 183 ASSERT_EQ(counter, m_pFileAlloc->size()); 184 ASSERT_FALSE(m_pFileAlloc->empty()); 185 ASSERT_TRUE(file->isRecognized()); 186 ASSERT_STREQ(name, file->name().data()); 187 } 188} 189 190TEST_F(FactoriesTest, file_iterate) { 191 int counter = 0; 192 for (counter = 1; counter < 1000; ++counter) { 193 char name[100]; 194 sprintf(name, "file %d", counter); 195 char path_name[100]; 196 sprintf(path_name, "/proj/mtk%d", counter); 197 MCLDFile* file = m_pFileAlloc->produce( 198 string(name), sys::fs::Path(string(path_name)), MCLDFile::Archive); 199 } 200 201 ASSERT_EQ(counter - 1, m_pFileAlloc->size()); 202 ASSERT_FALSE(m_pFileAlloc->empty()); 203 204 MCLDFileFactory::iterator file = m_pFileAlloc->begin(); 205 MCLDFileFactory::iterator fEnd = m_pFileAlloc->end(); 206 207 while (file != fEnd) { 208 ASSERT_TRUE((*file).isRecognized()); 209 ASSERT_FALSE((*file).name().empty()); 210 ++file; 211 } 212} 213