sgpemv2/src/testsuite/test-stepforward.cc

301 lines
7.4 KiB
C++
Raw Normal View History

// src/testsuite/test-stepforward.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/* This executable tests for workingness of the stepForward method,
* and nothing else. */
#include <glibmm/module.h> // ??
#include <cassert>
#include <string>
#include "config.h"
#include "gettext.h"
#include "glibmm/ustring.h"
#include <vector>
#include <iostream>
#include "backend/process.hh"
#include "backend/observed_subject.hh"
#include "backend/schedulable_list.hh"
#include "backend/schedulable_status.hh"
#include "templates/smartp.hh"
#include "scheduler.hh"
#include "user_interrupt_exception.hh"
#include <iostream>
namespace sgpem
{
/** An hard-coded Priority Round Robin policy
* It's actually called PRRPolicy, altough my personal taste would have suggested
* naming it
* Prioriy-Reliant Roughly-Realized Recently-Reimplemented Round-Robin Policy,
* i.e. PRRRRRRR-Policy.
* it adds a new constructor taking the quantum size (time slice)*/
class PRRPolicy : public Policy
{
public:
Policy()
{
_instance = this;
}
Policy(int quantum) : _quantum(quantum)
{
_instance = this;
}
static Policy& get_instance()
{
if(!_instance) _instance = new Policy(3); // quantum size
return *_instance;
}
virtual ~Policy()
{
}
virtual void configure()
{
}
virtual void sort_queue(Scheduler::event event) const throw(UserInterruptException)
{ // here a lot of fun, exactly O(n^2) fun!
SchedulableList sl = History.get_instance().get_simulation_status_at(get_current_time());
for (int i = 0; i < sl.size(); i++)
{
for (int j = 0; j < sl.size()-1; j++)
{
if (sl.get_item_at(j).get_schedulable().get_priority() < sl.get_item_at(j+1).get_schedulable().get_priority())
{
sl.swap(j, j+1);
}
}
}
}
int get_id() const
{
return 42;
}
virtual Glib::ustring get_description()
{
return "42";
}
virtual bool is_pre_emptive() const throw(UserInterruptException)
{
return 1;
}
virtual int get_time_slice() const throw(UserInterruptException)
{
return _quantum;
}
PolicyParameters& get_parameters()
{
return _parameters;
}
protected:
PolicyParameters _parameters;
int _id;
int _quantum;
private:
static Policy* _instance;
};
Policy*
PRRPolicy::_instance = NULL;
// A PolicyManager stub
class PolicyManager
{
public:
PolicyManager();
virtual ~PolicyManager();
virtual Policy& get_policy()
{
return PRRPolicy.get_instance();
}
virtual void init()
{
}
static PolicyManager& get_registered_manager();
private:
static PolicyManager* _registered;
};
PolicyManager*
PolicyManager::_registered = NULL;
PolicyManager::PolicyManager()
{
_registered = this;
}
PolicyManager::~PolicyManager()
{
if(_registered == this) _registered = NULL;
}
PolicyManager&
PolicyManager::get_registered_manager()
{
return *_registered;
}
// a History stub, should only save the last state included. but...
class History : public ObservedSubject
{
public:
memory::smart_ptr<sgpem::SchedulableStatus> get_scheduled_at(int time) const {}
memory::smart_ptr<sgpem::SchedulableList> get_simulation_status_at(int time) const;
int get_current_time() const {return _total_time_elapsed;}
void enqueue_slice(const sgpem::SchedulableList& status);
void truncate_at(int instant) {}
static History& get_instance();
private:
History(int); //private constructor. The parameter is discarded
static History _instance;
int _total_time_elapsed;
std::vector<sgpem::Slice> _slices;
};
History&
History::get_instance()
{
if(!_instance) _instance = new Policy(3); // quantum size
return *_instance;
}
} //~ namespace sgpem
// this class should invoke the Scheduler Stepforward method.
class StepForwardTester
{
}
// from here and further until the bottom, all to throw away I suppose
int
main(int argc, char** argv) {
using namespace sgpem;
using Glib::Module;
std::string command(ERERERT); // the sequence of commands to test
// sets up the test data
Process p1("P1", 1,5,1);
Process p2("P2", 5,55,2);
Process p3("P3", 36,30,3);
Process p4("P4", 4,26,3);
Process p5("P5", 15,200,3);
Process p6("P6", 6,250,1);
Process p7("P7", 8,42,15);
Process p8("P8", 8,56,1);
Process p9("P9", 9,42,1);
Process p10("PA", 12,42,1);
Process p11("PB", 106,42,1);
Process p12("PC", 100,42,1);
Process p13("PD", 29,42,18);
Process p14("PE", 0,42,1);
Process p15("PF", 2,88,1);
Process p16("PG", 3666,9,1);
Process p17("PH", 5,72,10);
Process p18("PJ", 6,26,1);
Process p19("PK", 10,24,17);
Process p20("PK2", 11,34,67); // not used!
SchedulableStatus ss1(p1);
SchedulableStatus ss2(p2);
SchedulableStatus ss3(p3);
SchedulableStatus ss4(p4);
SchedulableStatus ss5(p5);
SchedulableStatus ss6(p6);
SchedulableStatus ss7(p7);
SchedulableStatus ss8(p8);
SchedulableStatus ss9(p9);
SchedulableStatus ss10(p10);
SchedulableStatus ss11(p11);
SchedulableStatus ss12(p12);
SchedulableStatus ss13(p13);
SchedulableStatus ss14(p14);
SchedulableStatus ss15(p15);
SchedulableStatus ss16(p16);
SchedulableStatus ss17(p17);
SchedulableStatus ss18(p18);
SchedulableStatus ss19(p19); // not used!
SchedulableList initial;
initial.add_at_bottom(ss1);
initial.add_at_bottom(ss2);
initial.add_at_bottom(ss3);
initial.add_at_bottom(ss4);
initial.add_at_bottom(ss5);
initial.add_at_bottom(ss6);
initial.add_at_bottom(ss7);
initial.add_at_bottom(ss8);
initial.add_at_bottom(ss9);
initial.add_at_bottom(ss10);
initial.add_at_bottom(ss11);
initial.add_at_bottom(ss12);
initial.add_at_bottom(ss13);
initial.add_at_bottom(ss14);
initial.add_at_bottom(ss15);
initial.add_at_bottom(ss16);
initial.add_at_bottom(ss17);
initial.add_at_bottom(ss18);
HistoryTester HT(initial);
HT.test("ERERERERTTTETRERERETETTTTTTTTTTTTTT");
exit(0);
}