File: a2functionalTest.cc

package info (click to toggle)
aria2 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 14,748 kB
  • ctags: 13,441
  • sloc: cpp: 86,740; ansic: 16,496; sh: 4,916; makefile: 1,312; ruby: 397; yacc: 291; xml: 170; sed: 16
file content (127 lines) | stat: -rw-r--r-- 2,915 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "a2functional.h"

#include <string>
#include <numeric>
#include <algorithm>

#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

class a2functionalTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(a2functionalTest);
  CPPUNIT_TEST(testMemFunSh);
  CPPUNIT_TEST(testAdopt2nd);
  CPPUNIT_TEST(testStrjoin);
  CPPUNIT_TEST(testStrconcat);
  CPPUNIT_TEST(testStrappend);
  CPPUNIT_TEST(testLeastRecentAccess);
  CPPUNIT_TEST_SUITE_END();
public:
  void testMemFunSh();
  void testAdopt2nd();
  void testStrjoin();
  void testStrconcat();
  void testStrappend();
  void testLeastRecentAccess();

  class Greeting {
  public:
    virtual ~Greeting() {}

    virtual std::string sayGreeting() = 0;

    virtual std::string sayGreetingConst() const = 0;
  };

  typedef SharedHandle<Greeting> GreetingHandle;

  class JapaneseGreeting:public Greeting
  {
    virtual std::string sayGreeting()
    {
      return "HAROO WAARUDO";
    }

    virtual std::string sayGreetingConst() const
    {
      return "HAROO WAARUDO";
    }

  };

  struct LastAccess {
    time_t lastAccess_;
    LastAccess(time_t lastAccess):lastAccess_(lastAccess) {}

    time_t getLastAccess() const
    {
      return lastAccess_;
    }
  };
};


CPPUNIT_TEST_SUITE_REGISTRATION(a2functionalTest);

void a2functionalTest::testMemFunSh()
{
  GreetingHandle greeting(new JapaneseGreeting());

  CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreeting)(greeting));

  CPPUNIT_ASSERT_EQUAL(std::string("HAROO WAARUDO"), mem_fun_sh(&Greeting::sayGreetingConst)(greeting));

}

void a2functionalTest::testAdopt2nd()
{
  GreetingHandle greeting(new JapaneseGreeting());

  CPPUNIT_ASSERT_EQUAL(std::string("A Japanese said:HAROO WAARUDO"),
                       adopt2nd(std::plus<std::string>(), mem_fun_sh(&Greeting::sayGreeting))("A Japanese said:", greeting));
}

void a2functionalTest::testStrjoin()
{
  std::vector<std::string> v;
  CPPUNIT_ASSERT_EQUAL(std::string(""), strjoin(v.begin(), v.end(), " "));

  v.push_back("A");

  CPPUNIT_ASSERT_EQUAL(std::string("A"), strjoin(v.begin(), v.end(), " "));

  v.push_back("hero");
  v.push_back("is");
  v.push_back("lonely");

  CPPUNIT_ASSERT_EQUAL(std::string("A hero is lonely"),
                       strjoin(v.begin(), v.end(), " "));
}

void a2functionalTest::testStrconcat()
{
  CPPUNIT_ASSERT_EQUAL(std::string("X=3"), strconcat("X=", "3"));
}

void a2functionalTest::testStrappend()
{
  std::string str = "X=";
  strappend(str, "3", ",Y=", "5");
  CPPUNIT_ASSERT_EQUAL(std::string("X=3,Y=5"), str);
}

void a2functionalTest::testLeastRecentAccess()
{
  std::vector<LastAccess> v;
  for(int i = 99; i >= 0; --i) {
    v.push_back(LastAccess(i));
  }
  std::sort(v.begin(), v.end(), LeastRecentAccess<LastAccess>());
  for(int i = 0; i < 100; ++i) {
    CPPUNIT_ASSERT_EQUAL((time_t)i, v[i].lastAccess_);
  }
}

} // namespace aria2