Changeset 603


Ignore:
Timestamp:
Apr 16, 2008, 5:39:33 AM (13 years ago)
Author:
Peter Johansson
Message:

avoid copy and paste code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/stats_test.cc

    r596 r603  
    3333#include <algorithm>
    3434#include <cassert>
     35#include <cstddef>
    3536#include <iterator>
    3637#include <iostream>
    3738#include <numeric>
    3839#include <string>
     40#include <vector>
    3941
    4042namespace theplu{
    4143namespace svndigest{
    4244
     45  bool check(const Stats& stats, const std::vector<size_t>& correct,
     46             int linetype, const std::string& descr, const std::string& author);
    4347  bool test_add(void);
    4448  bool test_blame(void);
     
    5155{
    5256  using namespace theplu::svndigest;
     57  bool verbose=false;
    5358  bool ok=true;
     59  if (argc>1 && argv[1]==std::string("-v") )
     60    verbose=true;
    5461  SVN* svn=SVN::instance(test::filename("toy_project"));
    5562  if (!svn){
     
    6269  ok &= test_classic();
    6370                                                                               
     71  if (verbose)
     72    if (ok)
     73      std::cout << "Test is ok.\n";
     74    else
     75      std::cout << "Test failed.\n";
    6476  if (ok)
    6577    return 0;
     
    101113
    102114    // testing copyright lines for peter
    103     std::vector<size_t> correct(48);
     115    std::vector<size_t> correct(48,0);
    104116    correct[47]=2;
    105    
    106     std::partial_sum(correct.begin(), correct.end(), correct.begin());
    107     for (size_t rev=0; rev<correct.size(); ++rev)
    108       if (cs(LineTypeParser::copyright, "peter", rev)!=correct[rev]){
    109         std::cerr << "error: copyright peter rev:" << rev << ": found "
    110                   << cs(LineTypeParser::copyright, "peter", rev)
    111                   << " expected " << correct[rev] << "\n";
    112         ok = false;
    113       }
     117    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
    114118
    115119    // testing code lines for jari
    116     correct = std::vector<size_t>(48,0);
     120    correct.resize(0);
     121    correct.resize(48,0);
    117122    correct[15]=20;
    118123    correct[16]=1;
     
    120125    correct[28]=8;
    121126    correct[30]=54;
    122    
    123     std::partial_sum(correct.begin(), correct.end(), correct.begin());
    124     for (size_t rev=0; rev<correct.size(); ++rev)
    125       if (cs(LineTypeParser::code, "jari", rev)!=correct[rev]){
    126         std::cerr << "error: code jari rev:" << rev << ": found "
    127                   << cs(LineTypeParser::code, "jari", rev)
    128                   << " expected " << correct[rev] << "\n";
    129         ok = false;
    130       }
     127    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
    131128
    132129    // testing comment lines for jari
    133     correct = std::vector<size_t>(48,0);
     130    correct.resize(0);
     131    correct.resize(48,0);
    134132    correct[15]=1;
    135133    correct[28]=13;
    136134    correct[30]=7;
    137    
    138     std::partial_sum(correct.begin(), correct.end(), correct.begin());
    139     for (size_t rev=0; rev<correct.size(); ++rev)
    140       if (cs(LineTypeParser::comment, "jari", rev)!=correct[rev]){
    141         std::cerr << "error: comment jari rev:" << rev << ": found "
    142                   << cs(LineTypeParser::comment, "jari", rev)
    143                   << " expected " << correct[rev] << "\n";
    144         ok = false;
    145       }
     135    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
    146136
    147137    // testing blank lines for jari
    148     correct = std::vector<size_t>(48,0);
     138    correct.resize(0);
     139    correct.resize(48,0);
    149140    correct[15]=10;
    150141    correct[28]=10;
    151142    correct[30]=2;
    152    
    153     std::partial_sum(correct.begin(), correct.end(), correct.begin());
    154     for (size_t rev=0; rev<correct.size(); ++rev)
    155       if (cs(LineTypeParser::other, "jari", rev)!=correct[rev]){
    156         std::cerr << "error: other jari rev:" << rev << ": found "
    157                   << cs(LineTypeParser::other, "jari", rev)
    158                   << " expected " << correct[rev] << "\n";
    159         ok = false;
    160       }
     143    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
    161144
    162145    // testing code all lines for total
    163     correct = std::vector<size_t>(48,0);
     146    correct.resize(0);
     147    correct.resize(48,0);
    164148    correct[15]=31;
    165149    correct[16]=1;
     
    168152    correct[30]=63;
    169153    correct[47]=2;
    170 
    171     std::partial_sum(correct.begin(), correct.end(), correct.begin());
    172     for (size_t rev=0; rev<correct.size(); ++rev)
    173       if (cs(LineTypeParser::total, "all", rev)!=correct[rev]){
    174         std::cerr << "error: total all rev:" << rev << ": found "
    175                   << cs(LineTypeParser::total, "all", rev)
    176                   << " expected " << correct[rev] << "\n";
    177         ok = false;
    178       }
     154    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
    179155
    180156    return ok;
    181157  }
    182158 
     159  bool check(const Stats& stats, const std::vector<size_t>& correct,
     160             int linetype, const std::string& descr, const std::string& author)
     161  {
     162    bool ok=true;
     163    std::vector<size_t> sum(correct.size());
     164    std::partial_sum(correct.begin(), correct.end(), sum.begin());
     165    for (size_t rev=0; rev<sum.size(); ++rev){
     166      size_t n = stats(linetype, author, rev);
     167      if (n != sum[rev]){
     168        std::cerr << "error: " << descr << " " << author << " rev:" << rev
     169                  << ": found " << n << " expected " << sum[rev] << "\n";
     170        ok = false;
     171      }
     172    }
     173    return true;
     174  }
     175
    183176}} // end of namespace svndigest and theplu
Note: See TracChangeset for help on using the changeset viewer.