blob: 236bfc64e6aeb5c8e8c1a13bc40c5720dfacfeba [file] [log] [blame]
Zane Shelley11b89942019-11-07 11:07:28 -06001#include "simulator.hpp"
2
Zane Shelley1be4c3c2020-04-17 15:55:07 -05003#include <fstream> // std::ifstream
4
5namespace libhei
6{
7
8//------------------------------------------------------------------------------
9
10// Paths are relative from the build/ directory
11const std::map<SimulatorData::ChipType, const char*>
12 SimulatorData::cv_chipPath = {
Zane Shelleyaadf3bf2020-04-30 21:25:29 -050013 {SAMPLE, "../test/simulator/sample_data/sample.cdb"},
Zane Shelley1be4c3c2020-04-17 15:55:07 -050014};
Zane Shelley11b89942019-11-07 11:07:28 -060015
16//------------------------------------------------------------------------------
17
18void SimulatorData::addChip(const Chip& i_chip)
19{
20 // First check if this entry already exists.
Zane Shelley1be4c3c2020-04-17 15:55:07 -050021 auto itr1 = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
22 ASSERT_EQ(iv_chipList.end(), itr1);
Zane Shelley11b89942019-11-07 11:07:28 -060023
24 // Add the new entry.
25 iv_chipList.push_back(i_chip);
26
Zane Shelley1be4c3c2020-04-17 15:55:07 -050027 // Look for the file path
28 auto itr2 = cv_chipPath.find(static_cast<ChipType>(i_chip.getType()));
29 ASSERT_NE(cv_chipPath.end(), itr2);
30 const char* path = itr2->second;
Zane Shelley11b89942019-11-07 11:07:28 -060031
Zane Shelley1be4c3c2020-04-17 15:55:07 -050032 // Open the Chip Data File
33 std::ifstream cdf{path, std::ifstream::binary};
34 ASSERT_TRUE(cdf.good());
35
36 // Get the length of file
37 cdf.seekg(0, cdf.end);
38 size_t sz_buffer = cdf.tellg();
39 cdf.seekg(0, cdf.beg);
40
41 // Allocate memory
42 char* buffer = new char[sz_buffer];
43
44 // Read data as a block
45 cdf.read(buffer, sz_buffer);
46
47 // Close the Chip Data File
48 cdf.close();
49
50 // Initilize the chip with this Chip Data File.
Zane Shelleydd109cc2020-04-30 21:41:41 -050051 initialize(buffer, sz_buffer);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050052
53 // Clean up the buffer
54 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060055}
56
57//------------------------------------------------------------------------------
58
59void SimulatorData::endIteration()
60{
61 // Start by calling libhei::isolate().
62 IsolationData isoData{};
63 ReturnCode rc = isolate(iv_chipList, isoData);
64
65 // It is possible that even in a failure scenario the information in the
66 // returned IsolationData would be useful.
67 // TODO: Figure out where to put the data.
68
69 // Verify if isolation completed successfully.
70 ASSERT_TRUE(RC_SUCCESS == rc);
71
72 // Get the list of signatures found in isolation.
73 std::vector<Signature> givenSigList = isoData.getSignatureList();
74
75 // Verify the expected list and given list are the same.
76 ASSERT_EQ(iv_expSigList.size(), givenSigList.size());
77
78 std::sort(iv_expSigList.begin(), iv_expSigList.end());
79 std::sort(givenSigList.begin(), givenSigList.end());
80
81 ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(),
82 iv_expSigList.begin()));
83
84 // The iteration is complete so we can flush the data.
85 flushIterationData();
86}
Zane Shelley1be4c3c2020-04-17 15:55:07 -050087
88} // end namespace libhei