113465Sgabeblack@google.com/* 213465Sgabeblack@google.com * Copyright (c) 2015, 2018 ARM Limited 313465Sgabeblack@google.com * All rights reserved 413465Sgabeblack@google.com * 513465Sgabeblack@google.com * The license below extends only to copyright in the software and shall 613465Sgabeblack@google.com * not be construed as granting a license to any other intellectual 713465Sgabeblack@google.com * property including but not limited to intellectual property relating 813465Sgabeblack@google.com * to a hardware implementation of the functionality of the software 913465Sgabeblack@google.com * licensed hereunder. You may use the software subject to the license 1013465Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated 1113465Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software, 1213465Sgabeblack@google.com * modified or unmodified, in source code or in binary form. 1313465Sgabeblack@google.com * 1413465Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 1513465Sgabeblack@google.com * modification, are permitted provided that the following conditions are 1613465Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 1713465Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 1813465Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1913465Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 2013465Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 2113465Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 2213465Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 2313465Sgabeblack@google.com * this software without specific prior written permission. 2413465Sgabeblack@google.com * 2513465Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2613465Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2713465Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2813465Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2913465Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3013465Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3113465Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3213465Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3313465Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3413465Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3513465Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3613465Sgabeblack@google.com * 3713465Sgabeblack@google.com * Authors: Andreas Sandberg 3813465Sgabeblack@google.com */ 3913465Sgabeblack@google.com 4013465Sgabeblack@google.com#include <gtest/gtest.h> 4113465Sgabeblack@google.com 4213465Sgabeblack@google.com#include "base/circlebuf.hh" 4313465Sgabeblack@google.com 4413465Sgabeblack@google.comconst char data[] = { 4513465Sgabeblack@google.com 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 4613465Sgabeblack@google.com 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 4713465Sgabeblack@google.com}; 4813465Sgabeblack@google.com 4913465Sgabeblack@google.com// Basic non-overflow functionality 5013465Sgabeblack@google.comTEST(CircleBufTest, BasicReadWriteNoOverflow) 5113465Sgabeblack@google.com{ 5213465Sgabeblack@google.com CircleBuf<char> buf(8); 5313465Sgabeblack@google.com char foo[16]; 5413465Sgabeblack@google.com 5513465Sgabeblack@google.com // Write empty buffer, no overflow 5613465Sgabeblack@google.com buf.write(data, 8); 5713465Sgabeblack@google.com EXPECT_EQ(buf.size(), 8); 5813465Sgabeblack@google.com buf.peek(foo, 8); 5913465Sgabeblack@google.com EXPECT_EQ(memcmp(foo, data, 8), 0); 6013465Sgabeblack@google.com 6113465Sgabeblack@google.com // Read 2 6213465Sgabeblack@google.com buf.read(foo, 2); 6313465Sgabeblack@google.com EXPECT_EQ(memcmp(foo, data, 2), 0); 6413465Sgabeblack@google.com EXPECT_EQ(buf.size(), 6); 6513465Sgabeblack@google.com buf.read(foo, 6); 6613465Sgabeblack@google.com EXPECT_EQ(memcmp(foo, data + 2, 6), 0); 6713465Sgabeblack@google.com EXPECT_EQ(buf.size(), 0); 6813465Sgabeblack@google.com} 6913465Sgabeblack@google.com 7013465Sgabeblack@google.com// Basic single write overflow functionality 7113465Sgabeblack@google.comTEST(CircleBufTest, SingleWriteOverflow) 7213465Sgabeblack@google.com{ 7313465Sgabeblack@google.com CircleBuf<char> buf(8); 7413465Sgabeblack@google.com char foo[16]; 7513465Sgabeblack@google.com 7613465Sgabeblack@google.com buf.write(data, 16); 7713465Sgabeblack@google.com EXPECT_EQ(buf.size(), 8); 7813465Sgabeblack@google.com buf.peek(foo, 8); 7913465Sgabeblack@google.com EXPECT_EQ(memcmp(data + 8, foo, 8), 0); 8013465Sgabeblack@google.com} 8113465Sgabeblack@google.com 8213465Sgabeblack@google.com 8313465Sgabeblack@google.com// Multi-write overflow functionality 8413465Sgabeblack@google.comTEST(CircleBufTest, MultiWriteOverflow) 8513465Sgabeblack@google.com{ 8613465Sgabeblack@google.com CircleBuf<char> buf(8); 8713465Sgabeblack@google.com char foo[16]; 8813465Sgabeblack@google.com 8913465Sgabeblack@google.com // Write, no overflow, write overflow 9013465Sgabeblack@google.com buf.write(data, 6); 9113465Sgabeblack@google.com buf.write(data + 8, 6); 9213465Sgabeblack@google.com EXPECT_EQ(buf.size(), 8); 9313465Sgabeblack@google.com buf.peek(foo, 8); 9413465Sgabeblack@google.com EXPECT_EQ(memcmp(data + 4, foo, 2), 0); 9513465Sgabeblack@google.com EXPECT_EQ(memcmp(data + 8, foo + 2, 6), 0); 9613465Sgabeblack@google.com} 9713465Sgabeblack@google.com 9813465Sgabeblack@google.com// Pointer wrap around 9913465Sgabeblack@google.comTEST(CircleBufTest, PointerWrapAround) 10013465Sgabeblack@google.com{ 10113465Sgabeblack@google.com CircleBuf<char> buf(8); 10213465Sgabeblack@google.com char foo[16]; 10313465Sgabeblack@google.com 10413465Sgabeblack@google.com // _start == 0, _stop = 8 10513465Sgabeblack@google.com buf.write(data, 8); 10613465Sgabeblack@google.com // _start == 4, _stop = 8 10713465Sgabeblack@google.com buf.read(foo, 4); 10813465Sgabeblack@google.com // _start == 4, _stop = 12 10913465Sgabeblack@google.com buf.write(data + 8, 4); 11013465Sgabeblack@google.com EXPECT_EQ(buf.size(), 8); 11113465Sgabeblack@google.com // _start == 10, _stop = 12 11213465Sgabeblack@google.com // Normalized: _start == 2, _stop = 4 11313465Sgabeblack@google.com buf.read(foo + 4, 6); 11413465Sgabeblack@google.com EXPECT_EQ(buf.size(), 2); 11513465Sgabeblack@google.com EXPECT_EQ(memcmp(data, foo, 10), 0); 11613465Sgabeblack@google.com // Normalized: _start == 4, _stop = 4 11713465Sgabeblack@google.com buf.read(foo + 10, 2); 11813465Sgabeblack@google.com EXPECT_EQ(buf.size(), 0); 11913465Sgabeblack@google.com EXPECT_EQ(memcmp(data, foo, 12), 0); 12013465Sgabeblack@google.com} 121