Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 1 | #include "simulator.hpp" |
| 2 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 3 | #include <fstream> // std::ifstream |
| 4 | |
| 5 | namespace libhei |
| 6 | { |
| 7 | |
| 8 | //------------------------------------------------------------------------------ |
| 9 | |
| 10 | // Paths are relative from the build/ directory |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 11 | const std::map<SimulatorData::SimChipType, const char*> |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 12 | SimulatorData::cv_chipPath = { |
Zane Shelley | aadf3bf | 2020-04-30 21:25:29 -0500 | [diff] [blame] | 13 | {SAMPLE, "../test/simulator/sample_data/sample.cdb"}, |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 14 | }; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 15 | |
| 16 | //------------------------------------------------------------------------------ |
| 17 | |
| 18 | void SimulatorData::addChip(const Chip& i_chip) |
| 19 | { |
| 20 | // First check if this entry already exists. |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 21 | auto chip_itr = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip); |
| 22 | ASSERT_EQ(iv_chipList.end(), chip_itr); |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 23 | |
| 24 | // Add the new entry. |
| 25 | iv_chipList.push_back(i_chip); |
| 26 | |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 27 | // Check if this chip type has been initialized. |
| 28 | ChipType_t chipType = i_chip.getType(); |
| 29 | auto type_itr = std::find(iv_typeList.begin(), iv_typeList.end(), chipType); |
| 30 | if (iv_typeList.end() != type_itr) |
| 31 | { |
| 32 | return; // No need to continue. |
| 33 | } |
| 34 | |
| 35 | // Add the new entry. |
| 36 | iv_typeList.push_back(chipType); |
| 37 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 38 | // Look for the file path |
Zane Shelley | 8c093d8 | 2020-05-04 22:06:52 -0500 | [diff] [blame] | 39 | auto itr2 = cv_chipPath.find(static_cast<SimChipType>(chipType)); |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 40 | ASSERT_NE(cv_chipPath.end(), itr2); |
| 41 | const char* path = itr2->second; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 42 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 43 | // Open the Chip Data File |
| 44 | std::ifstream cdf{path, std::ifstream::binary}; |
| 45 | ASSERT_TRUE(cdf.good()); |
| 46 | |
| 47 | // Get the length of file |
| 48 | cdf.seekg(0, cdf.end); |
| 49 | size_t sz_buffer = cdf.tellg(); |
| 50 | cdf.seekg(0, cdf.beg); |
| 51 | |
| 52 | // Allocate memory |
| 53 | char* buffer = new char[sz_buffer]; |
| 54 | |
| 55 | // Read data as a block |
| 56 | cdf.read(buffer, sz_buffer); |
| 57 | |
| 58 | // Close the Chip Data File |
| 59 | cdf.close(); |
| 60 | |
| 61 | // Initilize the chip with this Chip Data File. |
Zane Shelley | dd109cc | 2020-04-30 21:41:41 -0500 | [diff] [blame] | 62 | initialize(buffer, sz_buffer); |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 63 | |
| 64 | // Clean up the buffer |
| 65 | delete[] buffer; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | //------------------------------------------------------------------------------ |
| 69 | |
| 70 | void SimulatorData::endIteration() |
| 71 | { |
| 72 | // Start by calling libhei::isolate(). |
| 73 | IsolationData isoData{}; |
Zane Shelley | 229c155 | 2020-05-04 22:44:15 -0500 | [diff] [blame] | 74 | isolate(iv_chipList, isoData); |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 75 | |
| 76 | // Get the list of signatures found in isolation. |
| 77 | std::vector<Signature> givenSigList = isoData.getSignatureList(); |
| 78 | |
| 79 | // Verify the expected list and given list are the same. |
| 80 | ASSERT_EQ(iv_expSigList.size(), givenSigList.size()); |
| 81 | |
| 82 | std::sort(iv_expSigList.begin(), iv_expSigList.end()); |
| 83 | std::sort(givenSigList.begin(), givenSigList.end()); |
| 84 | |
| 85 | ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(), |
| 86 | iv_expSigList.begin())); |
| 87 | |
| 88 | // The iteration is complete so we can flush the data. |
| 89 | flushIterationData(); |
| 90 | } |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 91 | |
| 92 | } // end namespace libhei |