1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "gtest/gtest.h" 18 19 #include "chre/util/buffer.h" 20 #include "chre/util/macros.h" 21 22 using chre::Buffer; 23 24 void fillBufferWithSequentialValues(float *buffer, size_t size) { 25 for (size_t i = 0; i < size; i++) { 26 buffer[i] = static_cast<float>(i); 27 } 28 } 29 30 TEST(Buffer, EmptyByDefault) { 31 Buffer<float> buffer; 32 EXPECT_EQ(buffer.data(), nullptr); 33 EXPECT_EQ(buffer.size(), 0); 34 } 35 36 TEST(Buffer, Wrap) { 37 float buf[128]; 38 fillBufferWithSequentialValues(buf, ARRAY_SIZE(buf)); 39 40 Buffer<float> buffer; 41 buffer.wrap(buf, ARRAY_SIZE(buf)); 42 EXPECT_EQ(buffer.data(), buf); 43 EXPECT_EQ(buffer.size(), ARRAY_SIZE(buf)); 44 } 45 46 TEST(Buffer, CopyArray) { 47 float buf[128]; 48 fillBufferWithSequentialValues(buf, ARRAY_SIZE(buf)); 49 50 Buffer<float> buffer; 51 EXPECT_TRUE(buffer.copy_array(buf, ARRAY_SIZE(buf))); 52 EXPECT_EQ(buffer.size(), ARRAY_SIZE(buf)); 53 54 for (size_t i = 0; i < ARRAY_SIZE(buf); i++) { 55 EXPECT_EQ(buffer.data()[i], static_cast<float>(i)); 56 } 57 } 58 59 TEST(Buffer, CopyArrayEmpty) { 60 Buffer<float> buffer; 61 EXPECT_TRUE(buffer.copy_array(nullptr, 0)); 62 EXPECT_EQ(buffer.data(), nullptr); 63 EXPECT_EQ(buffer.size(), 0); 64 } 65 66 TEST(Buffer, CopyArrayEmptyAfterWrap) { 67 float buf[128]; 68 fillBufferWithSequentialValues(buf, ARRAY_SIZE(buf)); 69 70 Buffer<float> buffer; 71 buffer.wrap(buf, ARRAY_SIZE(buf)); 72 EXPECT_TRUE(buffer.copy_array(nullptr, 0)); 73 EXPECT_EQ(buffer.data(), nullptr); 74 EXPECT_EQ(buffer.size(), 0); 75 } 76 77 TEST(Buffer, CopyArrayEmptyAfterCopy) { 78 float buf[128]; 79 fillBufferWithSequentialValues(buf, ARRAY_SIZE(buf)); 80 81 Buffer<float> buffer; 82 EXPECT_TRUE(buffer.copy_array(buf, ARRAY_SIZE(buf))); 83 EXPECT_TRUE(buffer.copy_array(nullptr, 0)); 84 EXPECT_EQ(buffer.data(), nullptr); 85 EXPECT_EQ(buffer.size(), 0); 86 } 87