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 <gtest/gtest.h>
6 #include <stdio.h>
7 
8 extern "C" {
9 #include "buffer_share.h"
10 #include "cras_types.h"
11 }
12 
13 namespace {
14 
15 class BufferShareTestSuite : public testing::Test {
16  protected:
SetUp()17   virtual void SetUp() {}
18 
TearDown()19   virtual void TearDown() {}
20 };
21 
TEST_F(BufferShareTestSuite,Create)22 TEST_F(BufferShareTestSuite, Create) {
23   buffer_share* dm = buffer_share_create(1024);
24   EXPECT_NE(static_cast<buffer_share*>(NULL), dm);
25   buffer_share_destroy(dm);
26 }
27 
TEST_F(BufferShareTestSuite,AddRmDev)28 TEST_F(BufferShareTestSuite, AddRmDev) {
29   buffer_share* dm = buffer_share_create(1024);
30   int rc;
31 
32   rc = buffer_share_add_id(dm, 0xf00, NULL);
33   EXPECT_EQ(0, rc);
34   rc = buffer_share_add_id(dm, 0xf00, NULL);
35   EXPECT_NE(0, rc);
36 
37   rc = buffer_share_rm_id(dm, 0xf00);
38   EXPECT_EQ(0, rc);
39   rc = buffer_share_rm_id(dm, 0xf00);
40   EXPECT_NE(0, rc);
41 
42   buffer_share_destroy(dm);
43 }
44 
TEST_F(BufferShareTestSuite,AddManyDevs)45 TEST_F(BufferShareTestSuite, AddManyDevs) {
46   buffer_share* dm = buffer_share_create(1024);
47 
48   for (unsigned int i = 0; i < INITIAL_ID_SIZE; i++)
49     EXPECT_EQ(0, buffer_share_add_id(dm, 0xf00 + i, NULL));
50 
51   EXPECT_EQ(0, buffer_share_add_id(dm, 0xf00 + INITIAL_ID_SIZE, NULL));
52   EXPECT_EQ(0, buffer_share_rm_id(dm, 0xf00 + INITIAL_ID_SIZE));
53 
54   for (unsigned int i = 0; i < INITIAL_ID_SIZE; i++)
55     EXPECT_EQ(0, buffer_share_rm_id(dm, 0xf00 + i));
56 
57   buffer_share_destroy(dm);
58 }
59 
TEST_F(BufferShareTestSuite,OneDev)60 TEST_F(BufferShareTestSuite, OneDev) {
61   buffer_share* dm = buffer_share_create(1024);
62   int rc;
63 
64   rc = buffer_share_add_id(dm, 0xf00, NULL);
65   EXPECT_EQ(0, rc);
66 
67   buffer_share_offset_update(dm, 0xf00, 500);
68   EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
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_destroy(dm);
80 }
81 
TEST_F(BufferShareTestSuite,TwoDevs)82 TEST_F(BufferShareTestSuite, TwoDevs) {
83   buffer_share* dm = buffer_share_create(1024);
84   int rc;
85 
86   rc = buffer_share_add_id(dm, 0xf00, NULL);
87   EXPECT_EQ(0, rc);
88   rc = buffer_share_add_id(dm, 0xf02, NULL);
89   EXPECT_EQ(0, rc);
90 
91   buffer_share_offset_update(dm, 0xf00, 500);
92   EXPECT_EQ(0, buffer_share_get_new_write_point(dm));
93 
94   buffer_share_offset_update(dm, 0xf02, 750);
95   EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
96 
97   buffer_share_offset_update(dm, 0xf00, 500);
98   EXPECT_EQ(250, buffer_share_get_new_write_point(dm));
99 
100   buffer_share_offset_update(dm, 0xf02, 750);
101   EXPECT_EQ(250, buffer_share_get_new_write_point(dm));
102 
103   buffer_share_offset_update(dm, 0xf00, 500);
104   EXPECT_EQ(500, buffer_share_get_new_write_point(dm));
105 
106   buffer_share_destroy(dm);
107 }
108 
109 }  //  namespace
110 
main(int argc,char ** argv)111 int main(int argc, char** argv) {
112   ::testing::InitGoogleTest(&argc, argv);
113   return RUN_ALL_TESTS();
114 }
115