source: trunk/test/cigar_iterator.cc @ 3372

Last change on this file since 3372 was 3372, checked in by Peter, 9 years ago

Fix bug in CigarIterator? that assumed BASE could be constructed from
NULL. Extend test to avoid regression.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1// $Id: cigar_iterator.cc 3372 2015-02-11 01:31:03Z peter $
2
3/*
4  Copyright (C) 2014 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include <config.h>
23
24#include "Suite.h"
25
26#include "yat/utility/Cigar.h"
27#include "yat/utility/CigarIterator.h"
28
29#include <boost/cstdint.hpp>
30
31#include <cassert>
32#include <deque>
33#include <vector>
34
35using namespace theplu::yat;
36
37void test1(test::Suite& suite);
38
39int main(int argc, char* argv[])
40{
41  test::Suite suite(argc, argv);
42
43  test1(suite);
44
45  return suite.return_value();
46}
47
48void test1(test::Suite& suite)
49{
50  // create a cigar 5S4M1D3M1I4M
51  std::vector<uint32_t> oplen;
52  std::vector<uint32_t> op;
53  oplen.push_back(5);
54  oplen.push_back(4);
55  oplen.push_back(1);
56  oplen.push_back(3);
57  oplen.push_back(1);
58  oplen.push_back(4);
59
60  op.push_back(BAM_CSOFT_CLIP);
61  op.push_back(BAM_CMATCH);
62  op.push_back(BAM_CDEL);
63  op.push_back(BAM_CMATCH);
64  op.push_back(BAM_CINS);
65  op.push_back(BAM_CMATCH);
66
67  assert(oplen.size()==op.size());
68  std::vector<uint32_t> cigar;
69  std::vector<uint32_t> long_cigar;
70  for (size_t i=0; i<op.size(); ++i) {
71    cigar.push_back(bam_cigar_gen(oplen[i], op[i]));
72    for (size_t j=0; j<oplen[i]; ++j)
73      long_cigar.push_back(op[i]);
74  }
75
76  typedef std::vector<uint32_t>::const_iterator Base;
77  utility::CigarIterator<Base> iter(cigar.begin());
78  utility::CigarIterator<Base> end(cigar.end());
79  std::vector<uint32_t>::const_iterator iter2 = long_cigar.begin();
80  while (iter!=end) {
81    assert(iter2<long_cigar.end());
82    if (*iter != *iter2) {
83      suite.add(false);
84      suite.err() << (iter2 - long_cigar.begin()) << " "
85                  << *iter << " " << *iter2 << "\n";
86    }
87    ++iter;
88    ++iter2;
89  }
90
91  // check default constructor
92  utility::CigarIterator<Base> iter3;
93  // check assignment
94  iter3 = utility::CigarIterator<Base>(cigar.begin());
95  utility::CigarIterator<Base> iter4(cigar.begin()+3, 1);
96  std::advance(iter3, oplen[0]+oplen[1]+oplen[2]+1);
97  if (iter3 != iter4) {
98    suite.add(false);
99    suite.err() << "error: iter3!=iter4\n";
100  }
101  // do not run compiler test
102  if (false) {
103    test::test_readable_iterator(iter3);
104    test::test_bidirectional_traversal_iterator(iter3);
105    // test default constructor
106    utility::CigarIterator<std::deque<uint32_t>::const_iterator> iter5;
107    test::avoid_compiler_warning(iter5);
108  }
109}
Note: See TracBrowser for help on using the repository browser.