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 13using namespace mcld; 14using namespace mcldtest; 15 16// Constructor can do set-up work for all test here. 17FactoriesTest::FactoriesTest() { 18 m_pNodeAlloc = new NodeAlloc(); 19 m_pFileAlloc = new FileAlloc(); 20} 21 22// Destructor can do clean-up work that doesn't throw exceptions here. 23FactoriesTest::~FactoriesTest() { 24 delete m_pNodeAlloc; 25 delete m_pFileAlloc; 26} 27 28// SetUp() will be called immediately before each test. 29void FactoriesTest::SetUp() { 30} 31 32// TearDown() will be called immediately after each test. 33void FactoriesTest::TearDown() { 34} 35 36//==========================================================================// 37// Testcases 38// 39TEST_F(FactoriesTest, node_produce) { 40 NodeAlloc::NodeType* node = m_pNodeAlloc->produce(); 41 ASSERT_EQ(1, m_pNodeAlloc->size()); 42 ASSERT_FALSE(m_pNodeAlloc->empty()); 43 node = m_pNodeAlloc->produce(); 44 ASSERT_EQ(2, m_pNodeAlloc->size()); 45 ASSERT_FALSE(m_pNodeAlloc->empty()); 46 node = m_pNodeAlloc->produce(); 47 ASSERT_EQ(3, m_pNodeAlloc->size()); 48 ASSERT_FALSE(m_pNodeAlloc->empty()); 49} 50 51TEST_F(FactoriesTest, node_iterate) { 52 NodeAlloc::NodeType* node = 0; 53 for (int i = 0; i < 100; ++i) { 54 node = m_pNodeAlloc->produce(); 55 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