È necessario creare tutti i segnali necessari SystemC, Moduli SystemC e creare una connessione tra di essi prima di eseguire qualsiasi test in gtest. Ciò richiede di creare la propria implementazione gtest_main.cc. Naturalmente in SystemC è necessario inserire tutto nella funzione sc_main().
Per questo, vorrei utilizzare il modello di progettazione del Registro di sistema.
Per prima cosa creare la classe di registro (registro + factory + singleton). Questa classe sarà responsabile della memorizzazione dei costruttori registrati che utilizzano l'allocazione dinamica con un puntatore nuovo e intelligente nell'espressione lambda (vedere factory :: add class). Crea tutti gli oggetti usando il metodo factory :: create() prima di eseguire tutti i test. Quindi puoi ottenere oggetti usando il metodo factory :: get() nell'esecuzione del test.
factory.hpp
#ifndef FACTORY_HPP
#define FACTORY_HPP
#include <map>
#include <string>
#include <memory>
#include <functional>
class factory {
public:
static factory& get_instance();
template<typename T, typename ...Args>
class add {
public:
add(Args&&... args);
add(const std::string& name, Args&&... args);
};
template<typename T>
static T* get(const std::string& name = "");
void create();
void destroy();
private:
using destructor = std::function<void(void*)>;
using object = std::unique_ptr<void, destructor>;
using constructor = std::function<object(void)>;
factory();
factory(const factory& other) = delete;
factory& operator=(const factory& other) = delete;
void add_object(const std::string& name, constructor create);
void* get_object(const std::string& name);
std::map<std::string, constructor> m_constructors;
std::map<std::string, object> m_objects;
};
template<typename T, typename ...Args>
factory::add<T, Args...>::add(Args&&... args) {
add("", args...);
}
template<typename T, typename ...Args>
factory::add<T, Args...>::add(const std::string& name, Args&&... args) {
factory::get_instance().add_object(name,
[args...]() -> object {
return object{
new T(std::forward<Args>(args)...),
[] (void* obj) {
delete static_cast<T*>(obj);
}
};
}
);
}
template<typename T> auto
factory::get(const std::string& name) -> T* {
return static_cast<T*>(factory::get_instance().get_object(name));
}
#endif /* FACTORY_HPP */
factory.cpp
#include "factory.hpp"
#include <stdexcept>
auto factory::get_instance() -> factory& {
static factory instance{};
return instance;
}
factory::factory() :
m_constructors{},
m_objects{}
{ }
void factory::create() {
for (const auto& item : m_constructors) {
m_objects[item.first] = item.second();
}
}
void factory::destroy() {
m_objects.clear();
}
void factory::add_object(const std::string& name, constructor create) {
auto it = m_constructors.find(name);
if (it == m_constructors.cend()) {
m_constructors[name] = create;
}
else {
throw std::runtime_error("factory::add(): "
+ name + " object already exist in factory");
}
}
auto factory::get_object(const std::string& name) -> void* {
auto it = m_objects.find(name);
if (it == m_objects.cend()) {
throw std::runtime_error("factory::get(): "
+ name + " object doesn't exist in factory");
}
return it->second.get();
}
creare la propria versione di implementazione gtest_main.cc. Chiama il metodo factory :: create() per creare tutti i segnali SystemC e i moduli SystemC prima di eseguire qualsiasi test RUN_ALL_TESTS(). Poiché la classe factory è un modello di progettazione singleton, chiamare il metodo factory :: destroy() dopo aver completato tutti i test per distruggere tutti gli oggetti SystemC creati.
main.cpp
#include "factory.hpp"
#include <systemc>
#include <gtest/gtest.h>
int sc_main(int argc, char* argv[]) {
factory::get_instance().create();
testing::InitGoogleTest(&argc, argv);
int status = RUN_ALL_TESTS();
factory::get_instance().destroy();
return status;
}
Quindi definire dut classe nel test di creerà segnali SystemC e moduli SystemC. Nel costruttore fare la connessione tra i segnali e i moduli SystemC creati. Registro definito dut classe per registro oggetto utilizzando costruttore globale come questo factory :: add g. Dopo di che puoi ottenere il tuo oggetto dut usando il semplice metodo factory :: get().
test.cpp
#include "my_module.h"
#include "factory.hpp"
#include <gtest/gtest.h>
#include <systemc>
class dut {
public:
sc_core::sc_clock aclk{"aclk"};
sc_core::sc_signal<bool> areset_n{"areset_n"};
sc_core::sc_signal<bool> in{"in"};
sc_core::sc_signal<bool> out{"out"};
dut() {
m_dut.aclk(aclk);
m_dut.areset_n(areset_n);
m_dut.in(in);
m_dut.out(out);
}
private:
my_module m_dut{"my_module"};
};
static factory::add<dut> g;
TEST(my_module, simple) {
auto test = factory::get<dut>();
test->areset_n = 0;
test->in = 0;
sc_start(3, SC_NS);
test->areset_n = 1;
test->in = 1;
sc_start(3, SC_NS);
EXPECT_TRUE(test->out.read());
}
Per più ispirazione, è possibile controllare il mio logica biblioteca per la verifica SystemC: https://github.com/tymonx/logic
Grazie per la risposta! Potresti fornire un codice di sistema con il framework di Google Test? – Joe
Forse è possibile utilizzare i processi anziché i thread in modo che il simulatore di sistema non si lamenti. – Stephan
Mi sono imbattuto nello stesso problema (SystemC lamentava che sc_start() veniva chiamato più di una volta). Nel caso tu sia interessato [qui] (http://stackoverflow.com/questions/25706294/running-boost-unit-tests-on-different-processes) puoi trovare la mia domanda originale e la soluzione che ho trovato . – betabandido