File: test_stdio_filebuf.cc

package info (click to toggle)
jellyfish 2.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,276 kB
  • sloc: cpp: 35,703; sh: 995; ruby: 578; makefile: 397; python: 165; perl: 36
file content (56 lines) | stat: -rw-r--r-- 1,714 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdio>
#include <fstream>
#include <istream>
#include <memory>

#include <gtest/gtest.h>

#include <unit_tests/test_main.hpp>
#include <jellyfish/stdio_filebuf.hpp>

namespace {
typedef jellyfish::stdio_filebuf<char> stdio_filebuf;
TEST(StdioFileBuf, Read) {
  const char* file_name = "test_stdio_file_buf_read";
  file_unlink fu(file_name);
  std::vector<char> buffer(8192);
  for(size_t i = 0; i < buffer.size(); ++i)
    buffer[i] = '@' + (i % 64);
  {
    std::ofstream out(file_name);
    out.write(buffer.data(), buffer.size());
  }

  int fd = open(file_name, O_RDONLY);
  EXPECT_GT(fd, -1);
  FILE* file = fopen(file_name, "r");
  EXPECT_NE((FILE*)0, file);

  std::unique_ptr<stdio_filebuf> fd_buf(new stdio_filebuf(fd, std::ios::in));
  std::istream fd_stream(fd_buf.get());
  std::unique_ptr<stdio_filebuf> file_buf(new stdio_filebuf(file, std::ios::in));
  std::istream file_stream(file_buf.get());

  size_t have_read = 0;
  char read_buffer[256];
  while(have_read < buffer.size()) {
    const size_t to_read     = random_bits(8);
    const size_t expect_read = std::min(buffer.size() - have_read, to_read);

    fd_stream.read(read_buffer, to_read);
    EXPECT_EQ(expect_read, (size_t)fd_stream.gcount());
    EXPECT_EQ(0, std::strncmp(&buffer[have_read], read_buffer, expect_read));

    file_stream.read(read_buffer, to_read);
    EXPECT_EQ(expect_read, (size_t)fd_stream.gcount());
    EXPECT_EQ(0, std::strncmp(&buffer[have_read], read_buffer, expect_read));

    have_read += expect_read;
  }
  EXPECT_TRUE(fd_stream.eof() || fd_stream.peek() == EOF);
  EXPECT_TRUE(file_stream.eof() || file_stream.peek() == EOF);
}
}