torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bitstream/TestHelpers.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for Boost.Test helper functions.
18 
19 #ifndef TORC_BITSTREAM_TESTHELPERS_HPP
20 #define TORC_BITSTREAM_TESTHELPERS_HPP
21 
22 #include <boost/filesystem.hpp>
23 #include <iostream>
24 
25 namespace torc {
26 namespace bitstream {
27 
28 /// \brief Template function to compare the contents of two packet vectors.
29 template <class V, class P> bool packetVectorsAreIdentical(const V& inVector1, const V& inVector2);
30 template <class V, class P> bool packetVectorsAreIdentical(const V& inVector1, const V& inVector2) {
31  // the vectors are not identical if their lengths differ
32  if(inVector1.size() != inVector2.size()) return false;
33  // iterate through both vectors
34  typename V::const_iterator p1 = inVector1.begin();
35  typename V::const_iterator e1 = inVector1.end();
36  typename V::const_iterator p2 = inVector2.begin();
37  while(p1 < e1) {
38  // look up the current packets
39  const P& packet1 = *p1++;
40  const P& packet2 = *p2++;
41  // the packets are not identical if their lengths differ
42  uint32_t size1 = packet1.getWordSize();
43  uint32_t size2 = packet2.getWordSize();
44  if(size1 != size2) return false;
45  // iterate through every packet word
46  for(uint32_t i = 0; i < size1; i++) if(packet1[i] != packet2[i]) return false;
47  }
48  // the vectors are identical if we got to here
49  return true;
50 }
51 
52 /// \brief Template function to recalculate bitstream CRC checksums and compare to reference values.
53 /// \param inPath The path to the reference bitstream.
54 template <class V> bool checkCRC(boost::filesystem::path inPath,
55  bool inDebug = false) {
56  std::fstream fileStream(inPath.string().c_str(), std::ios::binary | std::ios::in);
57  // read packets into the first bitstream and copy them into the second bitstream
58  V bitstream1;
59  bitstream1.read(fileStream, false);
60  V bitstream2(bitstream1);
61  // recalculate the CRC checksums in the second bitstream
62  if(inDebug) std::cout << bitstream1 << std::endl;
63  bitstream2.preflightPackets();
64  if(inDebug) std::cout << bitstream2 << std::endl;
65  // compare the packet vectors and return the result to the caller
66  return packetVectorsAreIdentical<V, typename V::value_type>(bitstream1, bitstream2);
67 };
68 
69 } // namespace bitstream
70 } // namespace torc
71 
72 #endif // TORC_BITSTREAM_TESTHELPERS_HPP
boost::filesystem::path path
bool checkCRC(boost::filesystem::path inPath, bool inDebug=false)
Template function to recalculate bitstream CRC checksums and compare to reference values...
bool packetVectorsAreIdentical(const V &inVector1, const V &inVector2)
Template function to compare the contents of two packet vectors.