summaryrefslogtreecommitdiffstats
path: root/osi/test/eager_reader_test.cpp
blob: ad00e17b0763a219025be53d0bde03ba2bd2b3f2 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/******************************************************************************
 *
 *  Copyright (C) 2014 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include <gtest/gtest.h>

#include "AllocationTestHarness.h"

extern "C" {
#include <stdint.h>
#include <unistd.h>

#include "allocator.h"
#include "eager_reader.h"
#include "osi.h"
#include "semaphore.h"
#include "thread.h"
}

#define BUFFER_SIZE 32

static const char *small_data = "white chocolate lindor truffles";
static const char *large_data =
  "Let him make him examine and thoroughly sift everything he reads, and "
  "lodge nothing in his fancy upon simple authority and upon trust. "
  "Aristotle's principles will then be no more principles to him, than those "
  "of Epicurus and the Stoics: let this diversity of opinions be propounded "
  "to, and laid before him; he will himself choose, if he be able; if not, "
  "he will remain in doubt. "
  ""
  "   \"Che non men the saver, dubbiar m' aggrata.\" "
  "   [\"I love to doubt, as well as to know.\"--Dante, Inferno, xi. 93] "
  ""
  "for, if he embrace the opinions of Xenophon and Plato, by his own reason, "
  "they will no more be theirs, but become his own.  Who follows another, "
  "follows nothing, finds nothing, nay, is inquisitive after nothing. "
  ""
  "   \"Non sumus sub rege; sibi quisque se vindicet.\" "
  "   [\"We are under no king; let each vindicate himself.\" --Seneca, Ep.,33] "
  ""
  "let him, at least, know that he knows.  it will be necessary that he "
  "imbibe their knowledge, not that he be corrupted with their precepts; "
  "and no matter if he forget where he had his learning, provided he know "
  "how to apply it to his own use.  truth and reason are common to every "
  "one, and are no more his who spake them first, than his who speaks them "
  "after: 'tis no more according to plato, than according to me, since both "
  "he and i equally see and understand them.  bees cull their several sweets "
  "from this flower and that blossom, here and there where they find them, "
  "but themselves afterwards make the honey, which is all and purely their "
  "own, and no more thyme and marjoram: so the several fragments he borrows "
  "from others, he will transform and shuffle together to compile a work "
  "that shall be absolutely his own; that is to say, his judgment: "
  "his instruction, labour and study, tend to nothing else but to form that. ";

static semaphore_t *done;

class EagerReaderTest : public AllocationTestHarness {
  protected:
    virtual void SetUp() {
      AllocationTestHarness::SetUp();
      pipe(pipefd);
      done = semaphore_new(0);
    }

    virtual void TearDown() {
      semaphore_free(done);
      AllocationTestHarness::TearDown();
    }

    int pipefd[2];
};

static void expect_data(eager_reader_t *reader, void *context) {
  char *data = (char *)context;
  int length = strlen(data);

  for (int i = 0; i < length; i++) {
    uint8_t byte;
    EXPECT_EQ((size_t)1, eager_reader_read(reader, &byte, 1, true));
    EXPECT_EQ(data[i], byte);
  }

  semaphore_post(done);
}

static void expect_data_multibyte(eager_reader_t *reader, void *context) {
  char *data = (char *)context;
  int length = strlen(data);

  for (int i = 0; i < length;) {
    uint8_t buffer[28];
    int bytes_to_read = (length - i) > 28 ? 28 : (length - i);
    int bytes_read = eager_reader_read(reader, buffer, bytes_to_read, false);
    EXPECT_EQ(bytes_to_read, bytes_read);
    for (int j = 0; j < bytes_read && i < length; j++, i++) {
      EXPECT_EQ(data[i], buffer[j]);
    }
  }

  semaphore_post(done);
}

TEST_F(EagerReaderTest, test_new_free_simple) {
  eager_reader_t *reader = eager_reader_new(pipefd[0], &allocator_malloc, BUFFER_SIZE, SIZE_MAX, "test_thread");
  ASSERT_TRUE(reader != NULL);
  eager_reader_free(reader);
}

TEST_F(EagerReaderTest, test_small_data) {
  eager_reader_t *reader = eager_reader_new(pipefd[0], &allocator_malloc, BUFFER_SIZE, SIZE_MAX, "test_thread");

  thread_t *read_thread = thread_new("read_thread");
  eager_reader_register(reader, thread_get_reactor(read_thread), expect_data, (void *)small_data);

  write(pipefd[1], small_data, strlen(small_data));

  semaphore_wait(done);
  eager_reader_free(reader);
  thread_free(read_thread);
}

TEST_F(EagerReaderTest, test_large_data_multibyte) {
  eager_reader_t *reader = eager_reader_new(pipefd[0], &allocator_malloc, BUFFER_SIZE, SIZE_MAX, "test_thread");

  thread_t *read_thread = thread_new("read_thread");
  eager_reader_register(reader, thread_get_reactor(read_thread), expect_data_multibyte, (void *)large_data);

  write(pipefd[1], large_data, strlen(large_data));

  semaphore_wait(done);
  eager_reader_free(reader);
  thread_free(read_thread);
}