Ignore:
Timestamp:
Jul 22, 2008, 7:32:53 PM (13 years ago)
Author:
Peter
Message:

adding functions inverse

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/utility_test.cc

    r1313 r1385  
    3232#include <fstream>
    3333#include <iostream>
     34#include <map>
    3435#include <string>
     36#include <vector>
    3537
    3638using namespace theplu::yat;
    3739void test_quantile_normalize(test::Suite&);
     40void test_inverse(test::Suite&);
     41
     42template<typename InputIterator, typename Key>
     43void test_inverse_validate(InputIterator first, InputIterator last,
     44                           const std::map<Key, std::vector<size_t> >& m,
     45                           test::Suite& suite);
     46template<typename InputIterator, typename Key>
     47void test_inverse_validate(InputIterator first,
     48                           const std::multimap<Key, size_t>& m,
     49                           test::Suite& suite);
    3850
    3951int main(int argc, char* argv[])
     
    140152
    141153  test_quantile_normalize(suite);
     154  test_inverse(suite);
    142155
    143156  return suite.return_value();
    144157}
     158
     159
     160void test_inverse(test::Suite& suite)
     161{
     162  suite.err() << "Testing inverse\n";
     163  std::vector<std::string> vec;
     164  vec.push_back("No");
     165  vec.push_back("one");
     166  vec.push_back("shall");
     167  vec.push_back("be");
     168  vec.push_back("subjected");
     169  std::map<std::string, std::vector<size_t> > map;
     170  utility::inverse(vec.begin(), vec.end(), map);
     171  test_inverse_validate(vec.begin(), vec.end(), map, suite);
     172  utility::inverse(vec.begin()+1, vec.end(), map);
     173  test_inverse_validate(vec.begin()+1, vec.end(), map, suite);
     174  const std::vector<std::string> vec2(vec);
     175  utility::inverse(vec2.begin(), vec2.end(), map);
     176  test_inverse_validate(vec2.begin(), vec2.end(), map, suite);
     177  std::multimap<std::string, size_t> mmap;
     178  utility::inverse(vec.begin(), vec.end(), mmap);
     179  test_inverse_validate(vec.begin(), mmap, suite);
     180  utility::inverse(vec.begin()+1, vec.end(), mmap);
     181  test_inverse_validate(vec.begin()+1, mmap, suite);
     182}
     183
     184
     185template<typename InputIterator, typename Key>
     186void test_inverse_validate(InputIterator first, InputIterator last,
     187                           const std::map<Key, std::vector<size_t> >& m,
     188                           test::Suite& suite)
     189{
     190  typedef typename std::map<Key, std::vector<size_t> >::const_iterator Iter;
     191  for ( InputIterator iter=first; iter != last; ++iter) {
     192    Iter map_iter = m.find(*iter);
     193    if (!suite.add(map_iter!=m.end())) {
     194      suite.err() << "test_inverse_validate() failed\n";
     195      suite.err() << "  could not find: " << *first << " in map m\n";
     196    }
     197    for (size_t i=0; i<map_iter->second.size(); ++i)
     198      if (!suite.add(map_iter->second[i] ==
     199                     static_cast<size_t>(distance(first, iter))) ) {
     200        suite.err() << "test_inverse_validate() failed\n";
     201        suite.err() << "  comparing: " << map_iter->second[i]
     202                    << " and " << distance(first, iter)
     203                    << " expected them to be equal\n";
     204      }
     205  }
     206}
     207
     208
     209template<typename InputIterator, typename Key>
     210void test_inverse_validate(InputIterator first,
     211                           const std::multimap<Key, size_t>& m,
     212                           test::Suite& suite)
     213{
     214  for (typename std::multimap<Key, size_t>::const_iterator iter=m.begin();
     215       iter!=m.end(); ++iter) {
     216    suite.add(*(first + iter->second) == iter->first);
     217  }
     218 
     219}
     220
    145221
    146222void test_quantile_normalize(test::Suite& suite)
Note: See TracChangeset for help on using the changeset viewer.