Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / qa_gr_circular_file.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <qa_gr_circular_file.h>
27 #include <gr_circular_file.h>
28 #include <cppunit/TestAssert.h>
29 #include <iostream>
30 #include <stdio.h>
31 #include <unistd.h>
32
33 static const char *test_file = "qa_gr_circular_file.data";
34 static const int BUFFER_SIZE = 8192;
35 static const int NWRITE = 8192 * 9 / 8;
36
37 void
38 qa_gr_circular_file::t1 ()
39 {
40 #ifdef HAVE_MMAP
41   gr_circular_file      *cf_writer;
42   gr_circular_file      *cf_reader;
43   
44   // write the data...
45
46   cf_writer = new gr_circular_file (test_file, true, BUFFER_SIZE * sizeof (short));
47
48   short sd;
49   for (int i = 0; i < NWRITE; i++){
50     sd = i;
51     cf_writer->write (&sd, sizeof (sd));
52   }
53
54   delete cf_writer;
55
56   // now read it back...
57
58   cf_reader = new gr_circular_file (test_file);
59   for (int i = 0; i < BUFFER_SIZE; i++){
60     int n = cf_reader->read (&sd, sizeof (sd));
61     CPPUNIT_ASSERT_EQUAL ((int) sizeof (sd), n);
62     CPPUNIT_ASSERT_EQUAL (NWRITE - BUFFER_SIZE + i, (int) sd);
63   }
64
65   int n = cf_reader->read (&sd, sizeof (sd));
66   CPPUNIT_ASSERT_EQUAL (0, n);
67
68   delete cf_reader;
69   unlink (test_file);
70 #endif // HAVE_MMAP
71 }
72