add steps

add unit tests
This commit is contained in:
fps 2013-08-27 15:56:27 +00:00
parent 1c73f7846c
commit d6721a3981
24 changed files with 946 additions and 102 deletions

View File

@ -25,36 +25,33 @@
using namespace XAO; using namespace XAO;
BooleanField::BooleanField(const XAO::Dimension dimension, const int nbComponents) BooleanField::BooleanField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents)
: Field("", dimension, nbElements, nbComponents)
{ {
m_dimension = dimension;
m_nbComponents = nbComponents;
} }
BooleanField::BooleanField(const std::string name, const XAO::Dimension dimension, const int nbComponents) BooleanField::BooleanField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents)
: Field(name, dimension, nbElements, nbComponents)
{ {
m_dimension = dimension;
m_nbComponents = nbComponents;
m_name = name;
} }
BooleanStep* BooleanField::addStep(const int step) BooleanStep* BooleanField::addStep(const int& step)
{ {
BooleanStep* bstep = new BooleanStep(step, m_nbElements, m_nbComponents); BooleanStep* bstep = new BooleanStep(step, m_nbElements, m_nbComponents);
m_steps.push_back(bstep); m_steps.push_back(bstep);
return bstep; return bstep;
} }
BooleanStep* BooleanField::addStep(const int step, const int stamp) BooleanStep* BooleanField::addStep(const int& step, const int& stamp)
{ {
BooleanStep* bstep = new BooleanStep(step, stamp, m_nbElements, m_nbComponents); BooleanStep* bstep = new BooleanStep(step, stamp, m_nbElements, m_nbComponents);
m_steps.push_back(bstep); m_steps.push_back(bstep);
return bstep; return bstep;
} }
BooleanStep* BooleanField::getStep(const int index) BooleanStep* BooleanField::getStep(const int& index)
{ {
if (index < m_steps.size()) if (index < m_steps.size())
return m_steps[index]; return (BooleanStep*)m_steps[index];
throw SALOME_Exception("IndexOutOfRange"); throw SALOME_Exception("IndexOutOfRange");
} }

View File

@ -34,17 +34,17 @@ namespace XAO
class BooleanField : public Field class BooleanField : public Field
{ {
public: public:
BooleanField(const XAO::Dimension dimension, const int nbComponents); BooleanField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents);
BooleanField(const std::string name, const XAO::Dimension dimension, const int nbComponents); BooleanField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() { return XAO::BOOLEAN; } virtual const XAO::Type getType() { return XAO::BOOLEAN; }
BooleanStep* addStep(const int step); BooleanStep* addStep(const int& step);
BooleanStep* addStep(const int step, const int stamp); BooleanStep* addStep(const int& step, const int& stamp);
BooleanStep* getStep(const int index); BooleanStep* getStep(const int& index);
private: // private:
std::vector<BooleanStep*> m_steps; // std::vector<BooleanStep*> m_steps;
}; };
} }

View File

@ -48,8 +48,11 @@ void BooleanStep::Init(const int& step, const int& stamp, const int& nbElements,
m_values.reserve(m_nbElements); m_values.reserve(m_nbElements);
for (int i = 0; i < m_nbElements; ++i) for (int i = 0; i < m_nbElements; ++i)
{ {
std::vector<bool> row;
row.reserve(m_nbComponents);
for (int j = 0; j < m_nbComponents; ++j) for (int j = 0; j < m_nbComponents; ++j)
m_values[i][j] = false; row.push_back(false);
m_values.push_back(row);
} }
} }
@ -78,7 +81,7 @@ std::vector<bool> BooleanStep::getElement(const int& element)
std::vector<bool> BooleanStep::getComponent(const int& component) std::vector<bool> BooleanStep::getComponent(const int& component)
{ {
checkElement(component); checkComponent(component);
std::vector<bool> result; std::vector<bool> result;
result.reserve(m_nbElements); result.reserve(m_nbElements);

57
src/XAO/DoubleField.cxx Normal file
View File

@ -0,0 +1,57 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#include "DoubleField.hxx"
#include "DoubleStep.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
DoubleField::DoubleField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents)
: Field("", dimension, nbElements, nbComponents)
{
}
DoubleField::DoubleField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents)
: Field(name, dimension, nbElements, nbComponents)
{
}
DoubleStep* DoubleField::addStep(const int& step)
{
DoubleStep* bstep = new DoubleStep(step, m_nbElements, m_nbComponents);
m_steps.push_back(bstep);
return bstep;
}
DoubleStep* DoubleField::addStep(const int& step, const int& stamp)
{
DoubleStep* bstep = new DoubleStep(step, stamp, m_nbElements, m_nbComponents);
m_steps.push_back(bstep);
return bstep;
}
DoubleStep* DoubleField::getStep(const int& index)
{
if (index < m_steps.size())
return (DoubleStep*)m_steps[index];
throw SALOME_Exception("IndexOutOfRange");
}

View File

@ -35,14 +35,17 @@ namespace XAO
class DoubleField : public Field class DoubleField : public Field
{ {
public: public:
DoubleField(const XAO::Dimension dimension, const int nbComponents); DoubleField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents);
DoubleField(const std::string name, const XAO::Dimension dimension, const int nbComponents); DoubleField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() { return XAO::DOUBLE; } virtual const XAO::Type getType() { return XAO::DOUBLE; }
DoubleStep* addStep(const int step); DoubleStep* addStep(const int& step);
DoubleStep* addStep(const int step, const int stamp); DoubleStep* addStep(const int& step, const int& stamp);
DoubleStep* getStep(const int index); DoubleStep* getStep(const int& index);
// private:
// std::vector<DoubleStep*> m_steps;
}; };
} }

147
src/XAO/DoubleStep.cxx Normal file
View File

@ -0,0 +1,147 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#include "DoubleStep.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
DoubleStep::DoubleStep(const int& nbElements, const int& nbComponents)
{
Init(0, 0, nbElements, nbComponents);
}
DoubleStep::DoubleStep(const int& step, const int& nbElements, const int& nbComponents)
{
Init(step, 0, nbElements, nbComponents);
}
DoubleStep::DoubleStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
Init(step, stamp, nbElements, nbComponents);
}
void DoubleStep::Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
m_nbElements = nbElements;
m_nbComponents = nbComponents;
m_step = step;
m_stamp = stamp;
m_values.reserve(m_nbElements);
for (int i = 0; i < m_nbElements; ++i)
{
std::vector<double> row;
row.reserve(m_nbComponents);
for (int j = 0; j < m_nbComponents; ++j)
row.push_back(0);
m_values.push_back(row);
}
}
std::vector<double> DoubleStep::getValues()
{
std::vector<double> result;
result.reserve(m_nbElements * m_nbComponents);
std::vector< std::vector<double> >::iterator it;
for (it = m_values.begin(); it != m_values.end(); ++it)
{
std::vector<double> eltValues = *it;
result.insert(result.end(), eltValues.begin(), eltValues.end());
}
return result;
}
std::vector<double> DoubleStep::getElement(const int& element)
{
checkElement(element);
std::vector<double> result(m_values[element]);
return result;
}
std::vector<double> DoubleStep::getComponent(const int& component)
{
checkComponent(component);
std::vector<double> result;
result.reserve(m_nbElements);
std::vector< std::vector<double> >::iterator it;
for (it = m_values.begin(); it != m_values.end(); ++it)
{
std::vector<double> eltValues = *it;
result.push_back(eltValues[component]);
}
return result;
}
const double DoubleStep::getValue(const int& element, const int& component)
{
checkElement(element);
checkComponent(component);
return m_values[element][component];
}
void DoubleStep::setValues(const std::vector<double>& values)
{
if (values.size() != m_nbComponents * m_nbElements)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbElements; ++i)
{
for (int j = 0; j < m_nbComponents; ++j)
{
m_values[i][j] = values[i * m_nbComponents + j];
}
}
}
void DoubleStep::setElements(const int& element, const std::vector<double>& elements)
{
checkElement(element);
if (elements.size() != m_nbComponents)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbComponents; ++i)
m_values[element][i] = elements[i];
}
void DoubleStep::setComponents(const int& component, const std::vector<double>& components)
{
checkElement(component);
if (components.size() != m_nbElements)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbElements; ++i)
m_values[i][component] = components[i];
}
void DoubleStep::setValue(const int& element, const int& component, const double& value)
{
checkElement(element);
checkComponent(component);
m_values[element][component] = value;
}

View File

@ -25,28 +25,35 @@
#include <vector> #include <vector>
#include "Xao.hxx" #include "Xao.hxx"
#include "Step.hxx"
namespace XAO namespace XAO
{ {
class DoubleStep : public Step class DoubleStep : public Step
{ {
public: public:
DoubleStep(const int nbElements, const int nbComponents); DoubleStep(const int& nbElements, const int& nbComponents);
DoubleStep(const int step, const int nbElements, const int nbComponents); DoubleStep(const int& step, const int& nbElements, const int& nbComponents);
DoubleStep(const int step, const int stamp, const int nbElements, const int nbComponents); DoubleStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() { return XAO::DOUBLE; } virtual const XAO::Type getType() { return XAO::DOUBLE; }
std::vector<double> getValues(); std::vector<double> getValues();
std::vector<double> getElement(const int i); std::vector<double> getElement(const int& element);
std::vector<double> getComponent(const int j); std::vector<double> getComponent(const int& component);
const double getValue(const int i, const int j); const double getValue(const int& element, const int& component);
void setValues(std::vector<double> values); void setValues(const std::vector<double>& values);
void setElements(const int i, std::vector<double> elements); void setElements(const int& element, const std::vector<double>& elements);
void setComponents(const int j, std::vector<double> components); void setComponents(const int& component, const std::vector<double>& components);
void setValue(const int i, const int j, const double value); void setValue(const int& element, const int& component, const double& value);
private:
void Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
private:
std::vector< std::vector<double> > m_values;
}; };
} }

View File

@ -33,39 +33,67 @@ using namespace XAO;
// ------------------------------------------------------- // -------------------------------------------------------
Field* Field::createField(const XAO::Type type, const XAO::Dimension dimension, const int nbComponents) Field::Field(const std::string& name, const XAO::Dimension& dimension,
const int& nbElements, const int& nbComponents)
: m_name(name), m_dimension(dimension), m_nbElements(nbElements), m_nbComponents(nbComponents)
{ {
return createField(type, "", dimension, nbComponents); m_components.reserve(nbComponents);
for (int i = 0; i < nbComponents; ++i)
m_components.push_back("");
} }
Field* Field::createField(const XAO::Type type, const std::string name, const XAO::Dimension dimension, const int nbComponents) Field* Field::createField(const XAO::Type& type, const XAO::Dimension& dimension,
const int& nbElements, const int& nbComponents)
{
return createField(type, "", dimension, nbElements, nbComponents);
}
Field* Field::createField(const XAO::Type& type, const std::string& name, const XAO::Dimension& dimension,
const int& nbElements, const int& nbComponents)
{ {
if (type == XAO::BOOLEAN) if (type == XAO::BOOLEAN)
return new BooleanField(name, dimension, nbComponents); return new BooleanField(name, dimension, nbElements, nbComponents);
if (type == XAO::INTEGER) if (type == XAO::INTEGER)
return new IntegerField(name, dimension, nbComponents); return new IntegerField(name, dimension, nbElements, nbComponents);
if (type == XAO::DOUBLE) if (type == XAO::DOUBLE)
return new DoubleField(name, dimension, nbComponents); return new DoubleField(name, dimension, nbElements, nbComponents);
if (type == XAO::STRING) if (type == XAO::STRING)
return new StringField(name, dimension, nbComponents); return new StringField(name, dimension, nbElements, nbComponents);
throw SALOME_Exception("Bad Type"); throw SALOME_Exception("Bad Type");
} }
const std::string Field::getComponentName(const int index) const std::string Field::getComponentName(const int& index)
{ {
if (index < m_components.size()) if (index < m_components.size())
return m_components[index]; return m_components[index];
// TODO: throw throw SALOME_Exception("IndexOutOfRange component");
return "";
} }
void Field::setComponentName(const int index, const std::string name) void Field::setComponentName(const int& index, const std::string& name)
{ {
if (index < m_components.size()) if (index < m_components.size())
{ {
m_components[index] = name; m_components[index] = name;
return;
} }
// TODO: throw
throw SALOME_Exception("IndexOutOfRange component");
}
bool Field::removeStep(Step* step)
{
std::vector<Step*>::iterator it = m_steps.begin();
for (; it != m_steps.end(); ++it)
{
Step* current = *it;
if (step == current)
{
m_steps.erase(it);
return true;
}
}
return false;
} }

View File

@ -38,13 +38,16 @@ namespace XAO
class Field class Field
{ {
protected: protected:
Field(); Field(const std::string& name, const XAO::Dimension& dimension,
const int& nbElements, const int& nbComponents);
public: public:
static Field* createField(const XAO::Type type, const XAO::Dimension dimension, const int nbComponents); static Field* createField(const XAO::Type& type, const XAO::Dimension& dimension,
static Field* createField(const XAO::Type type, const std::string name, const XAO::Dimension dimension, const int nbComponents); const int& nbElements, const int& nbComponents);
static Field* createField(const XAO::Type& type, const std::string& name, const XAO::Dimension& dimension,
const int& nbElements, const int& nbComponents);
virtual ~Field(); virtual ~Field() {};
/** /**
* Gets the Type of the field. * Gets the Type of the field.
@ -65,7 +68,7 @@ namespace XAO
* Sets the name of the Field. * Sets the name of the Field.
* @param name the name to set. * @param name the name to set.
*/ */
void setName(std::string name) void setName(const std::string& name)
{ {
m_name = name; m_name = name;
} }
@ -79,7 +82,14 @@ namespace XAO
return m_dimension; return m_dimension;
} }
int countElements(); /**
* Gets the number of elements of each step.
* @return the number of elements of each step.
*/
const int countElements()
{
return m_nbElements;
}
/** /**
* Gets the number of components. * Gets the number of components.
@ -87,41 +97,48 @@ namespace XAO
*/ */
const int countComponents() const int countComponents()
{ {
return m_components.size(); return m_nbComponents;
} }
const int countValues(); /**
* Gets the number of values for each step.
* @return the number of values for each step.
*/
const int countValues()
{
return m_nbElements * m_nbComponents;
}
/** /**
* Gets the number of the steps. * Gets the number of the steps.
* @return the number of steps. * @return the number of steps.
*/ */
const int countSteps() const int countSteps() { return m_steps.size(); }
{
return m_steps.size();
}
/** /**
* Gets the name of a component. * Gets the name of a component.
* @param index the index of the component to get. * @param index the index of the component to get.
* @return the name of the component for the given index. * @return the name of the component for the given index.
*/ */
const std::string getComponentName(const int index); const std::string getComponentName(const int& index);
/** /**
* Sets the name of a component. * Sets the name of a component.
* @param componentIndex the index of the component to set. * @param componentIndex the index of the component to set.
* @param name the name to set. * @param name the name to set.
*/ */
void setComponentName(const int componentIndex, const std::string name); void setComponentName(const int& componentIndex, const std::string& name);
/** /**
* Remove a step. * Remove a step.
* @param step the step to remove. * @param step the step to remove.
* @return * @return true if the step has been removed.
*/ */
bool removeStep(Step* step); bool removeStep(Step* step);
private:
void checkComponent(const int& component);
protected: protected:
/** The name of the Field. */ /** The name of the Field. */
std::string m_name; std::string m_name;
@ -132,10 +149,11 @@ namespace XAO
int m_nbComponents; int m_nbComponents;
/** The components of the field. */ /** The components of the field. */
std::vector<std::string> m_components; std::vector<std::string> m_components;
/** The steps. */ /** The number of elements. */
std::map<int, Step* > m_steps;
int m_nbElements; int m_nbElements;
/** The list of steps. */
std::vector<Step*> m_steps;
}; };
} }

View File

@ -35,14 +35,17 @@ namespace XAO
class IntegerField : public Field class IntegerField : public Field
{ {
public: public:
IntegerField(const XAO::Dimension dimension, const int nbComponents); IntegerField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents);
IntegerField(const std::string name, const XAO::Dimension dimension, const int nbComponents); IntegerField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() { return XAO::INTEGER; } virtual const XAO::Type getType() { return XAO::INTEGER; }
IntegerStep* addStep(const int step); IntegerStep* addStep(const int& step);
IntegerStep* addStep(const int step, const int stamp); IntegerStep* addStep(const int& step, const int& stamp);
IntegerStep* getStep(const int index); IntegerStep* getStep(const int& index);
// private:
// std::vector<IntegerStep*> m_steps;
}; };
} }

147
src/XAO/IntegerStep.cxx Normal file
View File

@ -0,0 +1,147 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#include "IntegerStep.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
IntegerStep::IntegerStep(const int& nbElements, const int& nbComponents)
{
Init(0, 0, nbElements, nbComponents);
}
IntegerStep::IntegerStep(const int& step, const int& nbElements, const int& nbComponents)
{
Init(step, 0, nbElements, nbComponents);
}
IntegerStep::IntegerStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
Init(step, stamp, nbElements, nbComponents);
}
void IntegerStep::Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
m_nbElements = nbElements;
m_nbComponents = nbComponents;
m_step = step;
m_stamp = stamp;
m_values.reserve(m_nbElements);
for (int i = 0; i < m_nbElements; ++i)
{
std::vector<int> row;
row.reserve(m_nbComponents);
for (int j = 0; j < m_nbComponents; ++j)
row.push_back(0);
m_values.push_back(row);
}
}
std::vector<int> IntegerStep::getValues()
{
std::vector<int> result;
result.reserve(m_nbElements * m_nbComponents);
std::vector< std::vector<int> >::iterator it;
for (it = m_values.begin(); it != m_values.end(); ++it)
{
std::vector<int> eltValues = *it;
result.insert(result.end(), eltValues.begin(), eltValues.end());
}
return result;
}
std::vector<int> IntegerStep::getElement(const int& element)
{
checkElement(element);
std::vector<int> result(m_values[element]);
return result;
}
std::vector<int> IntegerStep::getComponent(const int& component)
{
checkComponent(component);
std::vector<int> result;
result.reserve(m_nbElements);
std::vector< std::vector<int> >::iterator it;
for (it = m_values.begin(); it != m_values.end(); ++it)
{
std::vector<int> eltValues = *it;
result.push_back(eltValues[component]);
}
return result;
}
const int IntegerStep::getValue(const int& element, const int& component)
{
checkElement(element);
checkComponent(component);
return m_values[element][component];
}
void IntegerStep::setValues(const std::vector<int>& values)
{
if (values.size() != m_nbComponents * m_nbElements)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbElements; ++i)
{
for (int j = 0; j < m_nbComponents; ++j)
{
m_values[i][j] = values[i * m_nbComponents + j];
}
}
}
void IntegerStep::setElements(const int& element, const std::vector<int>& elements)
{
checkElement(element);
if (elements.size() != m_nbComponents)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbComponents; ++i)
m_values[element][i] = elements[i];
}
void IntegerStep::setComponents(const int& component, const std::vector<int>& components)
{
checkElement(component);
if (components.size() != m_nbElements)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbElements; ++i)
m_values[i][component] = components[i];
}
void IntegerStep::setValue(const int& element, const int& component, const int& value)
{
checkElement(element);
checkComponent(component);
m_values[element][component] = value;
}

View File

@ -25,28 +25,35 @@
#include <vector> #include <vector>
#include "Xao.hxx" #include "Xao.hxx"
#include "Step.hxx"
namespace XAO namespace XAO
{ {
class IntegerStep : public Step class IntegerStep : public Step
{ {
public: public:
IntegerStep(const int nbElements, const int nbComponents); IntegerStep(const int& nbElements, const int& nbComponents);
IntegerStep(const int step, const int nbElements, const int nbComponents); IntegerStep(const int& step, const int& nbElements, const int& nbComponents);
IntegerStep(const int step, const int stamp, const int nbElements, const int nbComponents); IntegerStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() { return XAO::INTEGER; } virtual const XAO::Type getType() { return XAO::INTEGER; }
std::vector<int> getValues(); std::vector<int> getValues();
std::vector<int> getElement(const int i); std::vector<int> getElement(const int& element);
std::vector<int> getComponent(const int j); std::vector<int> getComponent(const int& component);
const int getValue(const int i, const int j); const int getValue(const int& element, const int& component);
void setValues(std::vector<int> values); void setValues(const std::vector<int>& values);
void setElements(const int i, std::vector<int> elements); void setElements(const int& element, const std::vector<int>& elements);
void setComponents(const int j, std::vector<int> components); void setComponents(const int& element, const std::vector<int>& components);
void setValue(const int i, const int j, const int value); void setValue(const int& element, const int& component, const int& value);
private:
void Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
private:
std::vector< std::vector<int> > m_values;
}; };
} }

View File

@ -53,8 +53,14 @@ dist_libXAO_la_SOURCES = \
Group.cxx \ Group.cxx \
Field.cxx \ Field.cxx \
BooleanField.cxx \ BooleanField.cxx \
IntegerField.cxx \
DoubleField.cxx \
StringField.cxx \
Step.cxx \ Step.cxx \
BooleanStep.cxx \ BooleanStep.cxx \
IntegerStep.cxx \
DoubleStep.cxx \
StringStep.cxx \
XaoExporter.cxx XaoExporter.cxx

View File

@ -29,7 +29,7 @@
using namespace XAO; using namespace XAO;
Step* Step::createStep(const XAO::Type& type, const int& nbElements, const int &nbComponents) Step* Step::createStep(const XAO::Type& type, const int& nbElements, const int& nbComponents)
{ {
return createStep(type, 0, 0, nbElements, nbComponents); return createStep(type, 0, 0, nbElements, nbComponents);
} }

View File

@ -29,7 +29,7 @@ namespace XAO
class Step class Step
{ {
protected: protected:
Step(); Step() {}
public: public:
static Step* createStep(const XAO::Type& type, const int& nbElements, const int& nbComponents); static Step* createStep(const XAO::Type& type, const int& nbElements, const int& nbComponents);
@ -66,7 +66,7 @@ namespace XAO
const int countElements() { return m_nbElements; } const int countElements() { return m_nbElements; }
virtual const int countValues(); const int countValues() { return m_nbElements * m_nbComponents; }
protected: protected:
void checkElement(const int& element); void checkElement(const int& element);
@ -77,7 +77,6 @@ namespace XAO
int m_stamp; int m_stamp;
int m_nbComponents; int m_nbComponents;
int m_nbElements; int m_nbElements;
}; };
} }

57
src/XAO/StringField.cxx Normal file
View File

@ -0,0 +1,57 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#include "StringField.hxx"
#include "StringStep.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
StringField::StringField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents)
: Field("", dimension, nbElements, nbComponents)
{
}
StringField::StringField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents)
: Field(name, dimension, nbElements, nbComponents)
{
}
StringStep* StringField::addStep(const int& step)
{
StringStep* bstep = new StringStep(step, m_nbElements, m_nbComponents);
m_steps.push_back(bstep);
return bstep;
}
StringStep* StringField::addStep(const int& step, const int& stamp)
{
StringStep* bstep = new StringStep(step, stamp, m_nbElements, m_nbComponents);
m_steps.push_back(bstep);
return bstep;
}
StringStep* StringField::getStep(const int& index)
{
if (index < m_steps.size())
return (StringStep*)m_steps[index];
throw SALOME_Exception("IndexOutOfRange");
}

View File

@ -35,14 +35,17 @@ namespace XAO
class StringField : public Field class StringField : public Field
{ {
public: public:
StringField(const XAO::Dimension dimension, const int nbComponents); StringField(const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents);
StringField(const std::string name, const XAO::Dimension dimension, const int nbComponents); StringField(const std::string& name, const XAO::Dimension& dimension, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() { return XAO::STRING; } virtual const XAO::Type getType() { return XAO::STRING; }
StringStep* addStep(const int step); StringStep* addStep(const int& step);
StringStep* addStep(const int step, const int stamp); StringStep* addStep(const int& step, const int& stamp);
StringStep* getStep(const int index); StringStep* getStep(const int& index);
// private:
// std::vector<StringStep*> m_steps;
}; };
} }

147
src/XAO/StringStep.cxx Normal file
View File

@ -0,0 +1,147 @@
// Copyright (C) 2013 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// Author : Frederic Pons (OpenCascade)
#include "StringStep.hxx"
#include <Utils_SALOME_Exception.hxx>
using namespace XAO;
StringStep::StringStep(const int& nbElements, const int& nbComponents)
{
Init(0, 0, nbElements, nbComponents);
}
StringStep::StringStep(const int& step, const int& nbElements, const int& nbComponents)
{
Init(step, 0, nbElements, nbComponents);
}
StringStep::StringStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
Init(step, stamp, nbElements, nbComponents);
}
void StringStep::Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents)
{
m_nbElements = nbElements;
m_nbComponents = nbComponents;
m_step = step;
m_stamp = stamp;
m_values.reserve(m_nbElements);
for (int i = 0; i < m_nbElements; ++i)
{
std::vector<std::string> row;
row.reserve(m_nbComponents);
for (int j = 0; j < m_nbComponents; ++j)
row.push_back("");
m_values.push_back(row);
}
}
std::vector<std::string> StringStep::getValues()
{
std::vector<std::string> result;
result.reserve(m_nbElements * m_nbComponents);
std::vector< std::vector<std::string> >::iterator it;
for (it = m_values.begin(); it != m_values.end(); ++it)
{
std::vector<std::string> eltValues = *it;
result.insert(result.end(), eltValues.begin(), eltValues.end());
}
return result;
}
std::vector<std::string> StringStep::getElement(const int& element)
{
checkElement(element);
std::vector<std::string> result(m_values[element]);
return result;
}
std::vector<std::string> StringStep::getComponent(const int& component)
{
checkComponent(component);
std::vector<std::string> result;
result.reserve(m_nbElements);
std::vector< std::vector<std::string> >::iterator it;
for (it = m_values.begin(); it != m_values.end(); ++it)
{
std::vector<std::string> eltValues = *it;
result.push_back(eltValues[component]);
}
return result;
}
const std::string StringStep::getValue(const int& element, const int& component)
{
checkElement(element);
checkComponent(component);
return m_values[element][component];
}
void StringStep::setValues(const std::vector<std::string>& values)
{
if (values.size() != m_nbComponents * m_nbElements)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbElements; ++i)
{
for (int j = 0; j < m_nbComponents; ++j)
{
m_values[i][j] = values[i * m_nbComponents + j];
}
}
}
void StringStep::setElements(const int& element, const std::vector<std::string>& elements)
{
checkElement(element);
if (elements.size() != m_nbComponents)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbComponents; ++i)
m_values[element][i] = elements[i];
}
void StringStep::setComponents(const int& component, const std::vector<std::string>& components)
{
checkElement(component);
if (components.size() != m_nbElements)
throw SALOME_Exception("bad size"); // TODO
for (int i = 0; i < m_nbElements; ++i)
m_values[i][component] = components[i];
}
void StringStep::setValue(const int& element, const int& component, const std::string& value)
{
checkElement(element);
checkComponent(component);
m_values[element][component] = value;
}

View File

@ -26,28 +26,35 @@
#include <vector> #include <vector>
#include "Xao.hxx" #include "Xao.hxx"
#include "Step.hxx"
namespace XAO namespace XAO
{ {
class StringStep : public Step class StringStep : public Step
{ {
public: public:
StringStep(const int nbElements, const int nbComponents); StringStep(const int& nbElements, const int& nbComponents);
StringStep(const int step, const int nbElements, const int nbComponents); StringStep(const int& step, const int& nbElements, const int& nbComponents);
StringStep(const int step, const int stamp, const int nbElements, const int nbComponents); StringStep(const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
virtual const XAO::Type getType() { return XAO::STRING; } virtual const XAO::Type getType() { return XAO::STRING; }
std::vector<std::string> getValues(); std::vector<std::string> getValues();
std::vector<std::string> getElement(const int i); std::vector<std::string> getElement(const int& element);
std::vector<std::string> getComponent(const int j); std::vector<std::string> getComponent(const int& component);
const std::string getValue(const int i, const int j); const std::string getValue(const int& element, const int& component);
void setValues(std::vector<std::string> values); void setValues(const std::vector<std::string>& values);
void setElements(const int i, std::vector<std::string> elements); void setElements(const int& element, const std::vector<std::string>& elements);
void setComponents(const int j, std::vector<std::string> components); void setComponents(const int& component, const std::vector<std::string>& components);
void setValue(const int i, const int j, const std::string value); void setValue(const int& element, const int& component, const std::string& value);
private:
void Init(const int& step, const int& stamp, const int& nbElements, const int& nbComponents);
private:
std::vector< std::vector<std::string> > m_values;
}; };
} }

160
src/XAO/tests/FieldTest.cxx Normal file
View File

@ -0,0 +1,160 @@
#include <vector>
#include <Utils_SALOME_Exception.hxx>
#include "FieldTest.hxx"
#include "../Xao.hxx"
#include "../Field.hxx"
#include "../Step.hxx"
#include "../IntegerStep.hxx"
using namespace XAO;
void FieldTest::setUp()
{
}
void FieldTest::tearDown()
{
}
void FieldTest::cleanUp()
{
}
void FieldTest::testField(XAO::Type type)
{
Field* f = Field::createField(type, XAO::FACE, 10, 3);
CPPUNIT_ASSERT(f->getName().size() == 0);
CPPUNIT_ASSERT(f->getType() == type);
CPPUNIT_ASSERT(f->getDimension() == XAO::FACE);
CPPUNIT_ASSERT(f->countComponents() == 3);
CPPUNIT_ASSERT(f->countElements() == 10);
CPPUNIT_ASSERT(f->countValues() == 30);
f->setName("field1");
CPPUNIT_ASSERT(f->getName() == "field1");
CPPUNIT_ASSERT(f->getComponentName(0).size() == 0);
f->setComponentName(0, "x");
f->setComponentName(1, "y");
f->setComponentName(2, "z");
CPPUNIT_ASSERT(f->countComponents() == 3);
CPPUNIT_ASSERT(f->getComponentName(0) == "x");
CPPUNIT_ASSERT(f->getComponentName(1) == "y");
CPPUNIT_ASSERT(f->getComponentName(2) == "z");
CPPUNIT_ASSERT_THROW(f->setComponentName(3, "a"), SALOME_Exception);
CPPUNIT_ASSERT_THROW(f->getComponentName(3), SALOME_Exception);
}
void FieldTest::testBooleanField()
{
testField(XAO::BOOLEAN);
}
void FieldTest::testIntegerField()
{
testField(XAO::INTEGER);
}
void FieldTest::testDoubleField()
{
testField(XAO::DOUBLE);
}
void FieldTest::testStringField()
{
testField(XAO::STRING);
}
void FieldTest::testStep(XAO::Type type)
{
Step* step = Step::createStep(type, 5, 3);
CPPUNIT_ASSERT(step->getType() == type);
CPPUNIT_ASSERT(step->getStep() == 0);
step->setStep(10);
CPPUNIT_ASSERT(step->getStep() == 10);
CPPUNIT_ASSERT(step->getStamp() == 0);
step->setStamp(100);
CPPUNIT_ASSERT(step->getStamp() == 100);
CPPUNIT_ASSERT(step->countElements() == 5);
CPPUNIT_ASSERT(step->countComponents() == 3);
CPPUNIT_ASSERT(step->countValues() == 15);
}
void FieldTest::testBooleanStep()
{
testStep(XAO::BOOLEAN);
}
void FieldTest::testIntegerStep()
{
testStep(XAO::INTEGER);
}
void FieldTest::testDoubleStep()
{
testStep(XAO::DOUBLE);
}
void FieldTest::testStringStep()
{
testStep(XAO::STRING);
}
void FieldTest::testIntegerStepValues()
{
int nbComponents = 3;
int nbElements = 5;
IntegerStep* istep = (IntegerStep*)Step::createStep(XAO::INTEGER, nbElements, nbComponents);
for (int i = 0; i < istep->countElements(); ++i)
{
for (int j = 0; j < istep->countComponents(); ++j)
istep->setValue(i, j, i*10 + j);
}
CPPUNIT_ASSERT(istep->getValue(1, 2) == 12);
CPPUNIT_ASSERT_THROW(istep->getValue(nbElements, 2), SALOME_Exception);
CPPUNIT_ASSERT_THROW(istep->getValue(1, nbComponents), SALOME_Exception);
// get all values
std::vector<int> values;
values = istep->getValues();
CPPUNIT_ASSERT(values.size() == nbElements * nbComponents);
for (int i = 0; i < nbElements; ++i)
{
for (int j = 0; j < nbComponents; ++j)
CPPUNIT_ASSERT(values[i*nbComponents+j] == 10*i+j);
}
// get one element
values = istep->getElement(2);
CPPUNIT_ASSERT_THROW(istep->getElement(nbElements), SALOME_Exception);
CPPUNIT_ASSERT(values.size() == nbComponents);
for (int i = 0; i < nbComponents; ++i)
CPPUNIT_ASSERT(values[i] == 20+i);
// get one component
values = istep->getComponent(1);
CPPUNIT_ASSERT_THROW(istep->getComponent(nbComponents), SALOME_Exception);
CPPUNIT_ASSERT(values.size() == nbElements);
for (int i = 0; i < nbElements; ++i)
CPPUNIT_ASSERT(values[i] == 10*i+1);
// set one element
std::vector<int> newEltValues;
newEltValues.push_back(1);
CPPUNIT_ASSERT_THROW(istep->setElements(2, newEltValues), SALOME_Exception);
for (int i = 1; i < nbComponents; ++i)
newEltValues.push_back(1);
istep->setElements(2, newEltValues);
// set one component
std::vector<int> newCompValues;
newCompValues.push_back(100);
CPPUNIT_ASSERT_THROW(istep->setComponents(1, newCompValues), SALOME_Exception);
for (int i = 1; i < nbElements; ++i)
newCompValues.push_back(100);
istep->setComponents(1, newCompValues);
}

View File

@ -0,0 +1,45 @@
#ifndef __XAO_FIELD_TEST_HXX__
#define __XAO_FIELD_TEST_HXX__
#include <cppunit/extensions/HelperMacros.h>
#include "../Xao.hxx"
namespace XAO
{
class FieldTest: public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(FieldTest);
CPPUNIT_TEST(testBooleanField);
CPPUNIT_TEST(testDoubleField);
CPPUNIT_TEST(testIntegerField);
CPPUNIT_TEST(testStringField);
CPPUNIT_TEST(testBooleanStep);
CPPUNIT_TEST(testIntegerStep);
CPPUNIT_TEST(testDoubleStep);
CPPUNIT_TEST(testStringStep);
CPPUNIT_TEST(testIntegerStepValues);
CPPUNIT_TEST_SUITE_END();
public:
void setUp();
void tearDown();
void cleanUp();
void testField(XAO::Type type);
void testBooleanField();
void testIntegerField();
void testDoubleField();
void testStringField();
void testStep(XAO::Type type);
void testBooleanStep();
void testIntegerStep();
void testDoubleStep();
void testStringStep();
void testIntegerStepValues();
};
}
#endif // __XAO_FIELD_TEST_HXX__

View File

@ -1,5 +1,5 @@
#ifndef __BUILDING_IMPORT_TEST_HXX__ #ifndef __XAO_IMPORT_TEST_HXX__
#define __BUILDING_IMPORT_TEST_HXX__ #define __XAO_IMPORT_TEST_HXX__
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
@ -32,4 +32,4 @@ namespace XAO
}; };
} }
#endif #endif // __XAO_IMPORT_TEST_HXX__

View File

@ -39,6 +39,7 @@ TestXAO_LDFLAGS = \
../libXAO.la ../libXAO.la
dist_TestXAO_SOURCES = \ dist_TestXAO_SOURCES = \
FieldTest.cxx \
ImportExportTest.cxx \ ImportExportTest.cxx \
XAOTests.cxx XAOTests.cxx

View File

@ -1,5 +1,7 @@
#include "FieldTest.hxx"
#include "ImportExportTest.hxx" #include "ImportExportTest.hxx"
CPPUNIT_TEST_SUITE_REGISTRATION(XAO::FieldTest);
CPPUNIT_TEST_SUITE_REGISTRATION(XAO::ImportExportTest); CPPUNIT_TEST_SUITE_REGISTRATION(XAO::ImportExportTest);
#include "MainTest.hxx" #include "MainTest.hxx"