Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 24 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 25 | #include <iostream> |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 26 | #include <sstream> |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace Sync; |
| 29 | using namespace std; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 30 | using namespace boost; |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 31 | |
| 32 | int |
| 33 | main (int argc, char **argv) |
| 34 | { |
| 35 | Digest test; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 36 | test << "1\n"; |
Alexander Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 38 | // 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 Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 48 | |
| 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 Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 71 | |
| 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 Afanasyev | ec1d4a7 | 2012-03-05 11:06:54 -0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |