Changeset 422

Show
Ignore:
Timestamp:
07/20/09 12:41:12 (15 years ago)
Author:
vbarta
Message:

RV partial cleanup: more arguments passed by reference, less inline functions, tests integrated into the testsuite

Location:
library
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.cpp

    r384 r422  
    5555} 
    5656 
    57 void RV::init ( Array<std::string> in_names, ivec in_sizes,ivec in_times ) { 
     57void RV::init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times) 
     58{ 
    5859        len = in_names.length(); 
    5960        it_assert_debug ( in_names.length() ==in_times.length(), "check \"times\" " ); 
     
    106107        ret.dsize=ret.countsize(); 
    107108        return ret; 
     109} 
     110 
     111RV RV::operator()(int di1, int di2) const 
     112{ 
     113    ivec sz = cumsizes(); 
     114    int i1 = 0; 
     115    while (sz(i1) < di1) i1++; 
     116    int i2 = i1; 
     117    while (sz(i2) < di2) i2++; 
     118    return subselect(linspace(i1, i2)); 
    108119} 
    109120 
  • library/bdm/base/bdmbase.h

    r412 r422  
    3232extern Array<string> RV_NAMES; 
    3333 
    34 //! Structure of RV (used internally), i.e. expanded RVs - TODO tak proc je ve verejnem prostoru jmen? upravit 
     34//! Structure of RV, i.e. RVs expanded into a flat list of IDs, used for debugging. 
    3535class str 
    3636{ 
     
    9898private: 
    9999  //! auxiliary function used in constructor 
    100   void init(Array<std::string> in_names, ivec in_sizes, ivec in_times); 
    101   int init(const  string &name, int size); 
     100  void init(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times); 
     101  int init(const string &name, int size); 
    102102public: 
    103103  //! \name Constructors 
     
    105105 
    106106  //! Full constructor 
    107   RV(Array<std::string> in_names, ivec in_sizes, ivec in_times) {init(in_names, in_sizes, in_times);}; 
     107  RV(const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times) { init(in_names, in_sizes, in_times); } 
     108 
    108109  //! Constructor with times=0 
    109   RV(Array<std::string> in_names, ivec in_sizes) {init(in_names, in_sizes, zeros_i(in_names.length()));}; 
     110  RV(const Array<std::string> &in_names, const ivec &in_sizes) { init(in_names, in_sizes, zeros_i(in_names.length())); } 
     111 
    110112  //! Constructor with sizes=1, times=0 
    111   RV(Array<std::string> in_names) {init(in_names, ones_i(in_names.length()), zeros_i(in_names.length()));} 
     113  RV(const Array<std::string> &in_names) { init(in_names, ones_i(in_names.length()), zeros_i(in_names.length())); } 
     114 
    112115  //! Constructor of empty RV 
    113   RV() : dsize(0), len(0), ids(0), times(0) {}; 
     116  RV() : dsize(0), len(0), ids(0), times(0) {} 
    114117  //! Constructor of a single RV with given id 
    115118  RV(string name, int sz, int tm = 0); 
     
    119122  //!@{ 
    120123 
    121   //! Printing output e.g. for debugging. 
     124  //! State output, e.g. for debugging. 
    122125  friend std::ostream &operator<< (std::ostream &os, const RV &rv); 
    123   int _dsize() const {return dsize;} ; 
     126 
     127  int _dsize() const { return dsize; } 
     128 
    124129  //! Recount size of the corresponding data vector 
    125130  int countsize() const; 
    126131  ivec cumsizes() const; 
    127   int length() const {return len;} ; 
    128   int id(int at) const {return ids(at);}; 
    129   int size(int at) const {return RV_SIZES(ids(at));}; 
    130   int time(int at) const {return times(at);}; 
    131   std::string name(int at) const {return RV_NAMES(ids(at));}; 
    132   void set_time(int at, int time0) {times(at) = time0;}; 
     132  int length() const {return len;} 
     133  int id(int at) const {return ids(at);} 
     134  int size(int at) const { return RV_SIZES(ids(at)); } 
     135  int time(int at) const { return times(at); } 
     136  std::string name(int at) const { return RV_NAMES(ids(at)); } 
     137  void set_time(int at, int time0) { times(at) = time0; } 
    133138  //!@} 
    134139 
     
    146151  //! Subtract  another variable from the current one 
    147152  RV subt(const RV &rv2) const; 
    148   //! Select only variables at indeces ind 
     153  //! Select only variables at indices ind 
    149154  RV subselect(const ivec &ind) const; 
    150   //! Select only variables at indeces ind 
    151   RV operator()(const ivec &ind) const {return subselect(ind);}; 
     155 
     156  //! Select only variables at indices ind 
     157  RV operator()(const ivec &ind) const { return subselect(ind); } 
     158 
    152159  //! Select from data vector starting at di1 to di2 
    153   RV operator()(int di1, int di2) const { 
    154     ivec sz = cumsizes(); 
    155     int i1 = 0; 
    156     while (sz(i1) < di1) i1++; 
    157     int i2 = i1; 
    158     while (sz(i2) < di2) i2++; 
    159     return subselect(linspace(i1, i2)); 
    160   }; 
    161   //! Shift \c time shifted by delta. 
     160  RV operator()(int di1, int di2) const; 
     161 
     162  //! Shift \c time by delta. 
    162163  void t(int delta); 
    163164  //!@} 
     
    168169  //! generate \c str from rv, by expanding sizes TODO to_string.. 
    169170  str tostr() const; 
    170   //! when this rv is a part of bigger rv, this function returns indeces of self in the data vector of the bigger crv. 
     171  //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. 
    171172  //! Then, data can be copied via: data_of_this = cdata(ind); 
    172173  ivec dataind(const RV &crv) const; 
    173   //! generate mutual indeces when copying data betwenn self and crv. 
     174  //! generate mutual indices when copying data between self and crv. 
    174175  //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) 
    175176  void dataind(const RV &rv2, ivec &selfi, ivec &rv2i) const; 
    176177  //! Minimum time-offset 
    177   int mint() const {return min(times);}; 
     178  int mint() const {return min(times);} 
    178179  //!@} 
    179180 
  • library/tests/CMakeLists.txt

    r419 r422  
    1111 
    1212# BASIC EXECS 
    13 EXEC(rv_test) 
    1413EXEC(datalink_test) 
    1514EXEC(loggers_test) 
     
    4241 
    4342# using UnitTest++ 
    44 add_executable(testsuite testsuite.cpp test_user_info.cpp test_shared_ptr.cpp) 
     43add_executable(testsuite rv_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp) 
    4544target_link_libraries(testsuite bdm itpp unittest) 
    4645 
  • library/tests/rv_test.cpp

    r386 r422  
    1  
     1#include <string> 
     2#include <sstream> 
    23#include "../bdm/base/bdmbase.h" 
    34#include "../bdm/math/square_mat.h" 
    45#include "../bdm/math/chmat.h" 
     6#include "UnitTest++.h" 
    57 
    68using namespace bdm; 
    79 
    8 //These lines are needed for use of cout and endl 
    9 using std::cout; 
    10 using std::endl; 
     10TEST(test_rv) 
     11{ 
     12    RV a = RV("{a }", "3"); 
     13    CHECK_EQUAL(1, a.length()); 
     14    CHECK_EQUAL(3, a.size(0)); 
     15    CHECK_EQUAL(std::string("a"), a.name(0)); 
    1116 
    12 int main() 
    13 { 
    14         RV a = RV ( "{a }","3"); 
    15         RV b = RV ( "{b }","2"); 
    16         RV c = RV ( "{c }"); 
    17         RV ef = RV ("{e f }"); 
     17    RV b = RV("{b }", "2"); 
     18    CHECK_EQUAL(0, b.mint()); 
     19 
     20    RV c = RV("{c }"); 
     21    CHECK_EQUAL(1, c.length()); 
     22    CHECK_EQUAL(1, c.size(0)); 
     23 
     24    RV trv = RV("{e f }", "1 2", "3 4"); 
     25    CHECK_EQUAL(2, trv.length()); 
     26    CHECK_EQUAL(3, trv.mint()); 
     27 
     28    // add a and b 
     29    RV ab = a; 
     30    bool added = ab.add(b); 
     31    CHECK(added); 
     32    CHECK_EQUAL(2, ab.length()); 
     33    CHECK_EQUAL(3, ab.size(0)); 
     34    CHECK_EQUAL(std::string("a"), ab.name(0)); 
     35    CHECK_EQUAL(2, ab.size(1)); 
     36    CHECK_EQUAL(std::string("b"), ab.name(1)); 
     37 
     38    std::stringstream abss; 
     39    abss << ab; 
     40    CHECK_EQUAL(std::string("1(3)=a_{0}; 2(2)=b_{0}; "), abss.str()); 
     41 
     42    // concat a, b and c 
     43    RV abc = concat(ab, c); 
     44    CHECK_EQUAL(3, abc.length()); 
     45    std::stringstream abcss; 
     46    abcss << abc; 
     47    CHECK_EQUAL(std::string("1(3)=a_{0}; 2(2)=b_{0}; 3(1)=c_{0}; "), abcss.str()); 
     48 
     49    // structure of a, b, c 
     50    str s = abc.tostr(); 
     51    int exp_ids[] = { 1, 1, 1, 2, 2, 3 }; 
     52    int exp_sz = sizeof(exp_ids) / sizeof(exp_ids[0]); 
     53    CHECK_EQUAL(exp_sz, s.ids.size()); 
     54    CHECK_EQUAL(exp_sz, s.times.size()); 
     55    for (int i = 0; i < exp_sz; ++i) { 
     56        CHECK_EQUAL(exp_ids[i], s.ids(i)); 
     57        CHECK_EQUAL(0, s.times(i)); 
     58    } 
     59 
     60    RV slice = abc(1, 2); 
     61    CHECK_EQUAL(1, slice.length()); 
     62    CHECK_EQUAL(3, slice.size(0)); 
     63    CHECK_EQUAL(std::string("a"), slice.name(0)); 
     64 
     65    // find a in abc 
     66    ivec f = a.findself(abc); 
     67    CHECK_EQUAL(1, f.length()); 
     68    CHECK_EQUAL(0, f(0)); 
     69 
     70    // find abc in a 
     71    f = abc.findself(a); 
     72    int exp_indices[] = { 0, -1, -1 }; 
     73    CHECK_EQUAL(3, f.length()); 
     74    for (unsigned i = 0; 
     75         i < sizeof(exp_indices) / sizeof(exp_indices[0]); 
     76         ++i) { 
     77        CHECK_EQUAL(exp_indices[i], f(i)); 
     78    } 
     79 
     80    // subtract b from abc 
     81    RV ac = abc.subt(b); 
     82    std::stringstream acss; 
     83    acss << ac; 
     84    CHECK_EQUAL(std::string("1(3)=a_{0}; 3(1)=c_{0}; "), acss.str()); 
     85 
     86    // data index of ac in abc 
     87    ivec di = ac.dataind(abc); 
     88    int exp_di[] = { 0, 1, 2, 5 }; 
     89    exp_sz = sizeof(exp_di) / sizeof(exp_di[0]); 
     90    CHECK_EQUAL(exp_sz, di.size()); 
     91    for (int i = 0; i < exp_sz; ++i) { 
     92        CHECK_EQUAL(exp_di[i], di(i)); 
     93    } 
     94 
     95    // Copy indices between ba and ab 
     96    RV ba = b; 
     97    ba.add(a); 
    1898         
    19         cout << "Add a and b"<<endl; 
    20         RV ab = a; 
    21         ab.add(b); 
    22         cout << ab <<endl; 
    23          
    24         cout << "Concat a,b and c "<<endl; 
    25         RV abc = concat(ab,c); 
    26         cout << abc <<endl; 
    27         cout << "Structure of a,b,c "<<endl; 
    28         str s =abc.tostr(); 
    29         cout << s.ids <<endl; 
    30         cout << s.times <<endl; 
    31                  
    32         cout << "Find a in abc "<<endl; 
    33         cout << a.findself(abc)<<endl; 
    34         cout << "Find abc in a "<<endl; 
    35         cout << abc.findself(a)<<endl; 
    36          
    37         cout << "Subtract b from abc"<<endl; 
    38         RV ac = abc.subt(b); 
    39         cout << ac <<endl; 
    40          
    41         cout << "Data index of ac in abc"<<endl; 
    42         cout << ac.dataind(abc) <<endl; 
    43          
    44         cout<< "copy indeces between ba and ab" << endl; 
    45         RV ba = b; 
    46         ba.add(a); 
    47          
    48         ivec ai; 
    49         ivec bi; 
    50         ba.dataind(ac,ai,bi); 
    51         cout << "ba(" << ai <<")=ac(" << bi <<")"<<endl; 
    52          
    53         //Exit program: 
    54          
    55         //getchar(); 
    56         return 0; 
     99    ivec ai; 
     100    ivec bi; 
     101    ba.dataind(ac, ai, bi); 
    57102 
     103    int exp_ai[] = { 2, 3, 4 }; 
     104    exp_sz = sizeof(exp_ai) / sizeof(exp_ai[0]); 
     105    CHECK_EQUAL(exp_sz, ai.size()); 
     106    for (unsigned i = 0; 
     107         i < sizeof(exp_ai) / sizeof(exp_ai[0]); 
     108         ++i) { 
     109        CHECK_EQUAL(exp_ai[i], ai(i)); 
     110    } 
     111 
     112    int exp_bi[] = { 0, 1, 2 }; 
     113    exp_sz = sizeof(exp_bi) / sizeof(exp_bi[0]); 
     114    CHECK_EQUAL(exp_sz, bi.size()); 
     115    for (unsigned i = 0; 
     116         i < sizeof(exp_bi) / sizeof(exp_bi[0]); 
     117         ++i) { 
     118        CHECK_EQUAL(exp_bi[i], bi(i)); 
     119    } 
    58120}