Skip to content

Commit

Permalink
Add DataView unit Test, delete extra unit test in ReadPixelsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
illidanstormrange committed Oct 30, 2024
1 parent c193612 commit 4a6d080
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 23 deletions.
83 changes: 83 additions & 0 deletions test/src/DataViewTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////
#include <tgfx/core/DataView.h>
#include <tgfx/core/Stream.h>
#include <tgfx/core/UTF.h>
#include "tgfx/core/Buffer.h"
#include "utils/TestUtils.h"

namespace tgfx{
TGFX_TEST(DataViewTest, PNGDataCheck) {
auto stream =
Stream::MakeFromFile(ProjectPath::Absolute("resources/apitest/test_timestretch.png"));
ASSERT_TRUE(stream != nullptr && stream->size() >= 14);
Buffer buffer(14);
ASSERT_TRUE(stream->read(buffer.data(), 14) == 14);
auto data = DataView(buffer.bytes(), buffer.size());
auto secondByte = data.getUint8(1);
auto thirdByte = data.getUint8(2);
auto fourthByte = data.getUint8(3);
ASSERT_TRUE(secondByte == 'P' && thirdByte == 'N' && fourthByte == 'G');
buffer.clear();
}

TGFX_TEST(DataViewTest, ReadString) {
Buffer buffer(100);
std::string text = "Hello TGFX 123";
auto dataView = DataView(buffer.bytes(), buffer.size());
const char* textStart = text.data();
const char* textStop = textStart + text.size();
while (textStart < textStop) {
auto offset = text.size() - static_cast<size_t>(textStop - textStart);
auto unichar = UTF::NextUTF8(&textStart, textStop);
dataView.setInt32(offset, unichar);
}
ASSERT_EQ(std::string((char*)buffer.bytes()), text);
}

TGFX_TEST(DataViewTest, ReadWriteData) {
Buffer buffer(100);
auto dataView = DataView(buffer.bytes(), buffer.size());
dataView.setInt8(0, 'T');
dataView.setUint8(1, 0xFF);
dataView.setInt16(2, 'G');
dataView.setUint16(4, 0xFFFF);
dataView.setInt32(6, 'F');
dataView.setUint32(10, 0xFFFFFFFF);
dataView.setInt64(14, 'X');
dataView.setUint64(22, 0xFFFFFFFFFFFFFFFF);
dataView.setFloat(30, 1.123f);
dataView.setDouble(34, 1.0E+39);
ASSERT_TRUE(dataView.getInt8(0) == 'T');
ASSERT_TRUE(dataView.getUint8(1) == 0xFF);
ASSERT_TRUE(dataView.getInt16(2) == 'G');
ASSERT_TRUE(dataView.getUint16(4) == 0xFFFF);
ASSERT_TRUE(dataView.getInt32(6) == 'F');
ASSERT_TRUE(dataView.getUint32(10) == 0xFFFFFFFF);
ASSERT_TRUE(dataView.getInt64(14) == 'X');
ASSERT_TRUE(dataView.getUint64(22) == 0xFFFFFFFFFFFFFFFF);
ASSERT_TRUE(dataView.getFloat(30) == 1.123f);
ASSERT_TRUE(dataView.getDouble(34) == 1.0E+39);
buffer.clear();
dataView.setByteOrder(ByteOrder::BigEndian);
dataView.setUint16(0, 0x1234);
ASSERT_TRUE(dataView.getUint16(0) == 0x1234);
dataView.setByteOrder(ByteOrder::LittleEndian);
ASSERT_TRUE(dataView.getUint16(0) == 0x3412);
}
}
23 changes: 0 additions & 23 deletions test/src/ReadPixelsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include <tgfx/core/DataView.h>
#include <tgfx/core/Stream.h>
#include <vector>
#include "gpu/opengl/GLUtil.h"
#include "tgfx/core/Buffer.h"
Expand Down Expand Up @@ -473,25 +471,4 @@ TGFX_TEST(ReadPixelsTest, NativeCodec) {
ASSERT_TRUE(codec->readPixels(A8Info, pixels));
CHECK_PIXELS(A8Info, pixels, "NativeCodec_Encode_Alpha8");
}

TGFX_TEST(ImageReaderTest, DataCheck) {
auto stream =
Stream::MakeFromFile(ProjectPath::Absolute("resources/apitest/test_timestretch.png"));
ASSERT_TRUE(stream != nullptr && stream->size() >= 14);
Buffer buffer(14);
ASSERT_TRUE(stream->read(buffer.data(), 14) == 14);
auto data = DataView(buffer.bytes(), buffer.size());
auto secondByte = data.getUint8(1);
auto thirdByte = data.getUint8(2);
auto fouthByte = data.getUint8(3);
ASSERT_TRUE(secondByte == 'P' && thirdByte == 'N' && fouthByte == 'G');
buffer.clear();
data.setUint8(1, 'J');
data.setUint8(2, 'P');
data.setUint8(3, 'G');
secondByte = data.getUint8(1);
thirdByte = data.getUint8(2);
fouthByte = data.getUint8(3);
ASSERT_TRUE(secondByte == 'J' && thirdByte == 'P' && fouthByte == 'G');
}
} // namespace tgfx

0 comments on commit 4a6d080

Please sign in to comment.