1 // Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <gtest/gtest.h>
7
8 extern "C" {
9 #include "cras_types.h"
10 #include "buffer_share.h"
11 }
12
13 namespace {
14
15 class BufferShareTestSuite : public testing::Test{
16 protected:
SetUp()17 virtual void SetUp() {
18 }
19
TearDown()20 virtual void TearDown() {
21 }
22
23 };
24
TEST_F(BufferShareTestSuite,Create)25 TEST_F(BufferShareTestSuite, Create) {
26 buffer_share *dm = buffer_share_create(1024);
27 EXPECT_NE(static_cast<buffer_share *>(NULL), dm);
28 buffer_share_destroy(dm);
29 }
30
TEST_F(BufferShareTestSuite,AddRmDev)31 TEST_F(BufferShareTestSuite, AddRmDev) {
32 buffer_share *dm = buffer_share_create(1024);
33 int rc;
34
35 rc = buffer_share_add_id(dm, 0xf00, NULL);
36 EXPECT_EQ(0, rc);
37 rc = buffer_share_add_id(dm, 0xf00, NULL);
38 EXPECT_NE(0, rc);
39
40 rc = buffer_share_rm_id(dm, 0xf00);
41 EXPECT_EQ(0, rc);
42 rc = buffer_share_rm_id(dm, 0xf00);
43 EXPECT_NE(0, rc);
44
45 buffer_share_destroy(dm);
46 }
47
TEST_F(BufferShareTestSuite,AddManyDevs)48 TEST_F(BufferShareTestSuite, AddManyDevs) {
49 buffer_share *dm = buffer_share_create(1024);
50
51 for (unsigned int i = 0; i < INITIAL_ID_SIZE; i++)
52 EXPECT_EQ(0, buffer_share_add_id(dm, 0xf00 + i, NULL));
53
54 EXPECT_EQ(0, buffer_share_add_id(dm, 0xf00 + INITIAL_ID_SIZE, NULL));
55 EXPECT_EQ(0, buffer_share_rm_id(dm, 0xf00 + INITIAL_ID_SIZE));
56
57 for (unsigned int i = 0; i < INITIAL_ID_SIZE; i++)
58 EXPECT_EQ(0, buffer_share_rm_id(dm, 0xf00 + i));
59
60 buffer_share_destroy(dm);
61 }
62
TEST_F(BufferShareTestSuite,OneDev)63 TEST_F(BufferShareTestSuite, OneDev) {
64 buffer_share *dm = buffer_share_create(1024);
65 int rc;
66
67 rc = buffer_share_add_id(dm, 0xf00, NULL);
68 EXPECT_EQ(0, rc);
69
70 buffer_share_offset_update(dm, 0xf00, 500);
71 EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
72
73 buffer_share_offset_update(dm, 0xf00, 500);
74 EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
75
76 buffer_share_offset_update(dm, 0xf00, 500);
77 EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
78
79 buffer_share_offset_update(dm, 0xf00, 500);
80 EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
81
82 buffer_share_destroy(dm);
83 }
84
TEST_F(BufferShareTestSuite,TwoDevs)85 TEST_F(BufferShareTestSuite, TwoDevs) {
86 buffer_share *dm = buffer_share_create(1024);
87 int rc;
88
89 rc = buffer_share_add_id(dm, 0xf00, NULL);
90 EXPECT_EQ(0, rc);
91 rc = buffer_share_add_id(dm, 0xf02, NULL);
92 EXPECT_EQ(0, rc);
93
94 buffer_share_offset_update(dm, 0xf00, 500);
95 EXPECT_EQ(0, buffer_share_get_new_write_point(dm));
96
97 buffer_share_offset_update(dm, 0xf02, 750);
98 EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
99
100 buffer_share_offset_update(dm, 0xf00, 500);
101 EXPECT_EQ(250, buffer_share_get_new_write_point(dm));
102
103 buffer_share_offset_update(dm, 0xf02, 750);
104 EXPECT_EQ(250, buffer_share_get_new_write_point(dm));
105
106 buffer_share_offset_update(dm, 0xf00, 500);
107 EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
108
109 buffer_share_destroy(dm);
110 }
111
112 } // namespace
113
main(int argc,char ** argv)114 int main(int argc, char **argv) {
115 ::testing::InitGoogleTest(&argc, argv);
116 return RUN_ALL_TESTS();
117 }
118