blob: 51921d53bdaae07c6a175892a77ff2a172aab8c7 [file] [log] [blame]
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "../model/sync-digest.h"
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080024#include <boost/lexical_cast.hpp>
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080025#include <iostream>
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080026#include <sstream>
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080027
28using namespace Sync;
29using namespace std;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080030using namespace boost;
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080031
32int
33main (int argc, char **argv)
34{
35 Digest test;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080036 test << "1\n";
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080037
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080038 // try
39 // {
40 // cout << "Trying to print without explicit getHash() call: ";
41 // cout << test;
42 // cout << "Failed (should be asserted)\n";
43 // }
44 // catch (...)
45 // {
46 // cout << "OK (exception)\n";
47 // }
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080048
49 // without explicit finalizing, Digest will not be complete and printing out will cause assert
50 test.getHash ();
51
52 try
53 {
54 cout << "Hash for '1' is " << test << std::endl;
55 }
56 catch (...)
57 {
58 cout << "Hash calculation failed\n";
59 }
60
61 try
62 {
63 cout << "Trying to add data to hash after finalizing: ";
64 test << "2"; // should cause an assert
65 cout << "Failed (should be asserted)\n";
66 }
67 catch (...)
68 {
69 cout << "OK (exception)\n";
70 }
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080071
72 Digest test2;
73// #ifdef DIGEST_BASE64
74// string testString = "sCYyTGkEsqnLS4jW1hyB0Q==";
75// #else
76 string testString = lexical_cast<string> (test); //"b026324c6904b2a9cb4b88d6d61c81d1";
77// #endif
78 cout << "Hash from string: " << testString << "\n";
79 istringstream is (testString);
80 is >> test2;
81
82 cout << "Result from hash: " << test2 << "\n";
83
84 cout << "Compare two hashes: " << (test == test2) << "\n";
85
86 Digest test3;
87 if (testString[0] != '1')
88 testString[0] = '1';
89 else
90 testString[0] = '2';
91
92 istringstream is2(testString);
93 is2 >> test3;
94
95 cout << "Hash from string: " << test3 << "\n";
96 cout << "Compare two hashes: " << (test == test3) << "\n";
97
Alexander Afanasyevec1d4a72012-03-05 11:06:54 -080098 return 0;
99}