Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 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 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 16 | // Constructor can do set-up work for all test here. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 17 | FactoriesTest::FactoriesTest() { |
| 18 | m_pNodeAlloc = new NodeAlloc(); |
| 19 | m_pFileAlloc = new FileAlloc(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | // Destructor can do clean-up work that doesn't throw exceptions here. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 23 | FactoriesTest::~FactoriesTest() { |
| 24 | delete m_pNodeAlloc; |
| 25 | delete m_pFileAlloc; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // SetUp() will be called immediately before each test. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 29 | void FactoriesTest::SetUp() { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // TearDown() will be called immediately after each test. |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 33 | void FactoriesTest::TearDown() { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | //==========================================================================// |
| 37 | // Testcases |
| 38 | // |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 39 | TEST_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()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 51 | TEST_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 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 58 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 70 | TEST_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; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 92 | TEST_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; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 116 | TEST_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 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 127 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 135 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 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; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 152 | TEST_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()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 165 | TEST_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 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 174 | TEST_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 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 190 | TEST_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 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 200 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 201 | ASSERT_EQ(counter - 1, m_pFileAlloc->size()); |
| 202 | ASSERT_FALSE(m_pFileAlloc->empty()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 203 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 204 | MCLDFileFactory::iterator file = m_pFileAlloc->begin(); |
| 205 | MCLDFileFactory::iterator fEnd = m_pFileAlloc->end(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 206 | |
Stephen Hines | 37b74a3 | 2014-11-26 18:48:20 -0800 | [diff] [blame] | 207 | while (file != fEnd) { |
| 208 | ASSERT_TRUE((*file).isRecognized()); |
| 209 | ASSERT_FALSE((*file).name().empty()); |
| 210 | ++file; |
| 211 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 212 | } |