| 1 | /* |
| 2 | * Copyright (c) 2017, Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included |
| 12 | * in all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include "cm_test.h" |
| 24 | |
| 25 | class BufferTest: public CmTest |
| 26 | { |
| 27 | public: |
| 28 | static const uint32_t ELEMENT_COUNT = 64; |
| 29 | static const uint32_t SIZE = ELEMENT_COUNT*sizeof(uint32_t); |
| 30 | |
| 31 | BufferTest(): m_buffer(nullptr) {} |
| 32 | |
| 33 | ~BufferTest() {} |
| 34 | |
| 35 | int32_t CreateDestroy(uint32_t size) |
| 36 | { |
| 37 | int32_t result = m_mockDevice->CreateBuffer(size, m_buffer); |
| 38 | if (result != CM_SUCCESS) |
| 39 | { |
| 40 | return result; |
| 41 | } |
| 42 | SurfaceIndex *surface_index = nullptr; |
| 43 | result = m_buffer->GetIndex(surface_index); |
| 44 | EXPECT_EQ(CM_SUCCESS, result); |
| 45 | EXPECT_GT(surface_index->get_data(), static_cast<uint32_t>(0)); |
| 46 | return m_mockDevice->DestroySurface(m_buffer); |
| 47 | }//=============================================== |
| 48 | |
| 49 | int32_t ReadWrite() |
| 50 | { |
| 51 | uint32_t to_buffer[ELEMENT_COUNT] = {1}; |
| 52 | uint32_t from_buffer[ELEMENT_COUNT] = {2}; |
| 53 | for (uint8_t i = 0; i < ELEMENT_COUNT; ++i) |
| 54 | { |
| 55 | to_buffer[i] = i; |
| 56 | } |
| 57 | int32_t result = m_mockDevice->CreateBuffer(SIZE, m_buffer); |
| 58 | EXPECT_EQ(CM_SUCCESS, result); |
| 59 | |
| 60 | result = m_buffer->WriteSurface(reinterpret_cast<uint8_t*>(to_buffer), |
| 61 | nullptr); |
| 62 | EXPECT_EQ(CM_SUCCESS, result); |
| 63 | |
| 64 | result = m_buffer->ReadSurface(reinterpret_cast<uint8_t*>(from_buffer), |
| 65 | nullptr); |
| 66 | EXPECT_EQ(CM_SUCCESS, result); |
| 67 | |
| 68 | result = memcmp(to_buffer, from_buffer, sizeof(to_buffer)); |
| 69 | EXPECT_EQ(0, result); |
| 70 | |
| 71 | return m_mockDevice->DestroySurface(m_buffer); |
| 72 | }//=============================================== |
| 73 | |
| 74 | int32_t Initialize() |
| 75 | { |
| 76 | int32_t result = m_mockDevice->CreateBuffer(SIZE, m_buffer); |
| 77 | EXPECT_EQ(CM_SUCCESS, result); |
| 78 | |
| 79 | result = m_buffer->InitSurface(0x42434445, nullptr); |
| 80 | EXPECT_EQ(CM_SUCCESS, result); |
| 81 | |
| 82 | uint32_t data[ELEMENT_COUNT] = {0}; |
| 83 | result = m_buffer->ReadSurface(reinterpret_cast<uint8_t*>(data), |
| 84 | nullptr); |
| 85 | EXPECT_EQ(CM_SUCCESS, result); |
| 86 | EXPECT_EQ(0x42434445, data[0]); |
| 87 | EXPECT_EQ(0x42434445, data[ELEMENT_COUNT - 1]); |
| 88 | return m_mockDevice->DestroySurface(m_buffer); |
| 89 | }//=============================================== |
| 90 | |
| 91 | protected: |
| 92 | CMRT_UMD::CmBuffer *m_buffer; |
| 93 | };//============================= |
| 94 | |
| 95 | TEST_F(BufferTest, MultipleSizes) |
| 96 | { |
| 97 | RunEach<int32_t>(CM_SUCCESS, |
| 98 | [this]() { return CreateDestroy(SIZE); }); |
| 99 | |
| 100 | RunEach<int32_t>(CM_INVALID_WIDTH, |
| 101 | [this]() { return CreateDestroy(0); }); |
| 102 | |
| 103 | RunEach<int32_t>(CM_SUCCESS, |
| 104 | [this]() { return CreateDestroy(1); }); |
| 105 | |
| 106 | RunEach<int32_t>(CM_SUCCESS, |
| 107 | [this]() { return CreateDestroy(16*1024*1024); }); |
| 108 | |
| 109 | RunEach<int32_t>(CM_SUCCESS, |
| 110 | [this]() { return CreateDestroy(64*1024*1024); }); |
| 111 | |
| 112 | uint32_t large_size = 0x80000001; // 1-byte larger than maximum size. |
| 113 | RunEach<int32_t>( |
| 114 | CM_INVALID_WIDTH, |
| 115 | [this, large_size]() { return CreateDestroy(large_size); }); |
| 116 | |
| 117 | return; |
| 118 | }//======== |
| 119 | |
| 120 | TEST_F(BufferTest, ReadWrite) |
| 121 | { |
| 122 | RunEach<int32_t>(CM_SUCCESS, |
| 123 | [this]() { return ReadWrite(); }); |
| 124 | return; |
| 125 | }//======== |
| 126 | |
| 127 | TEST_F(BufferTest, Initialization) |
| 128 | { |
| 129 | RunEach<int32_t>(CM_SUCCESS, |
| 130 | [this]() { return Initialize(); }); |
| 131 | return; |
| 132 | }//======== |