|
| 1 | +#include"env/storage.h" |
| 2 | + |
| 3 | +// NOTE, Make sure all these includes are AFTER the system and header includes |
| 4 | +#include"CppUTest/CommandLineTestRunner.h" |
| 5 | +#include"CppUTest/MemoryLeakDetectorNewMacros.h" |
| 6 | +#include"CppUTest/TestHarness.h" |
| 7 | +#include"CppUTest/Utest.h" |
| 8 | +#include"CppUTestExt/MockSupport.h" |
| 9 | + |
| 10 | +// clang-format off |
| 11 | +TEST_GROUP(ScopedStorageTestGroup) |
| 12 | +{ |
| 13 | +}; |
| 14 | + |
| 15 | +TEST_GROUP(StorageTestGroup) |
| 16 | +{ |
| 17 | +voidsetup() { |
| 18 | +buildcc::Storage::Init(); |
| 19 | + } |
| 20 | +voidteardown() { |
| 21 | +buildcc::Storage::Deinit(); |
| 22 | + } |
| 23 | +}; |
| 24 | +// clang-format on |
| 25 | + |
| 26 | +classBigObj {}; |
| 27 | + |
| 28 | +classBigObjWithParameters { |
| 29 | +public: |
| 30 | +BigObjWithParameters(const std::string &name,int id,const BigObj &obj) |
| 31 | + : name_(name) { |
| 32 | + (void)id; |
| 33 | + (void)obj; |
| 34 | + } |
| 35 | + |
| 36 | +const std::string &GetName()const {return name_; } |
| 37 | + |
| 38 | +private: |
| 39 | + std::string name_; |
| 40 | +}; |
| 41 | + |
| 42 | +static BigObj obj; |
| 43 | + |
| 44 | +TEST(ScopedStorageTestGroup, BasicUsage) { |
| 45 | + buildcc::ScopedStorage storage; |
| 46 | + storage.Add<BigObjWithParameters>("identifier","name",10, obj); |
| 47 | + storage.Add<BigObjWithParameters>("identifier2","name2",12, obj); |
| 48 | + |
| 49 | +// Usage |
| 50 | + storage.ConstRef<BigObjWithParameters>("identifier").GetName(); |
| 51 | + storage.Ref<BigObjWithParameters>("identifier2").GetName(); |
| 52 | + |
| 53 | +// Automatic cleanup here |
| 54 | +} |
| 55 | + |
| 56 | +TEST(ScopedStorageTestGroup, IncorrectUsage) { |
| 57 | + buildcc::ScopedStorage storage; |
| 58 | + storage.Add<BigObjWithParameters>("identifier","name",10, obj); |
| 59 | + |
| 60 | +// We try to cast to a different type! |
| 61 | +CHECK_THROWS(std::exception, storage.Ref<BigObj>("identifier")); |
| 62 | + |
| 63 | +// We use a wrong identifier |
| 64 | +CHECK_THROWS(std::exception, |
| 65 | + storage.Ref<BigObjWithParameters>("identifier2")); |
| 66 | +} |
| 67 | + |
| 68 | +std::string &toReference(std::string *pointer) {return *pointer; } |
| 69 | + |
| 70 | +TEST(ScopedStorageTestGroup, NullptrDelete) { |
| 71 | + buildcc::ScopedStorage storage; |
| 72 | + storage.Remove<std::string>(nullptr); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(StorageTestGroup, BasicUsage) { |
| 76 | + buildcc::Storage::Add<BigObjWithParameters>("identifier","name",10, obj); |
| 77 | + buildcc::Storage::Add<BigObjWithParameters>("identifier2","name2",12, obj); |
| 78 | + |
| 79 | +// Usage |
| 80 | +constauto &bigobj = |
| 81 | + buildcc::Storage::ConstRef<BigObjWithParameters>("identifier").GetName(); |
| 82 | +constauto &bigobj2 = |
| 83 | + buildcc::Storage::Ref<BigObjWithParameters>("identifier2").GetName(); |
| 84 | + |
| 85 | +STRCMP_EQUAL(bigobj.c_str(),"name"); |
| 86 | +STRCMP_EQUAL(bigobj2.c_str(),"name2"); |
| 87 | +} |
| 88 | + |
| 89 | +intmain(int ac,char **av) { |
| 90 | +returnCommandLineTestRunner::RunAllTests(ac, av); |
| 91 | +} |