summaryrefslogtreecommitdiff
path: root/utils/sv_test.cc
blob: b006e66df19f25f0b5b91f3b1cec6d862fb16755 (plain)
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
#define BOOST_TEST_MODULE WeightsTest
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <sstream>
#include <string>
#include "sparse_vector.h"
#include "fdict.h"

using namespace std;

BOOST_AUTO_TEST_CASE(Dot) {
  SparseVector<double> x;
  SparseVector<double> y;
  x.set_value(1,0.8);
  y.set_value(1,5);
  x.set_value(2,-2);
  y.set_value(2,1);
  x.set_value(3,80);
  BOOST_CHECK_CLOSE(x.dot(y), 2.0, 1e-9);
}

BOOST_AUTO_TEST_CASE(Equality) {
  SparseVector<double> x;
  SparseVector<double> y;
  x.set_value(1,-1);
  y.set_value(1,-1);
  BOOST_CHECK(x == y);
}

BOOST_AUTO_TEST_CASE(Division) {
  SparseVector<double> x;
  SparseVector<double> y;
  x.set_value(1,1);
  y.set_value(1,-1);
  BOOST_CHECK(!(x == y));
  x /= -1;
  BOOST_CHECK(x == y);
}

BOOST_AUTO_TEST_CASE(Serialization) {
  string arc;
  FD::dict_.clear();
  {
    SparseVector<double> x;
    x.set_value(FD::Convert("Feature1"), 1.0);
    x.set_value(FD::Convert("Pi"), 3.14);
    ostringstream os;
    boost::archive::text_oarchive oa(os);
    oa << x;
    arc = os.str();
  }
  FD::dict_.clear();
  FD::Convert("SomeNewString");
  {
    SparseVector<double> x;
    istringstream is(arc);
    boost::archive::text_iarchive ia(is);
    ia >> x;
    cerr << x << endl;
    BOOST_CHECK_CLOSE(x.get(FD::Convert("Pi")), 3.14, 1e-9);
    BOOST_CHECK_CLOSE(x.get(FD::Convert("Feature1")), 1.0, 1e-9);
  }
}