Path: blob/main_old/src/libANGLE/BinaryStream_unittest.cpp
1693 views
//1// Copyright 2015 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56// BinaryStream_unittest.cpp: Unit tests of the binary stream classes.78#include <gtest/gtest.h>910#include "libANGLE/BinaryStream.h"1112namespace angle13{1415// Test that errors are properly generated for overflows.16TEST(BinaryInputStream, Overflow)17{18const uint8_t goodValue = 2;19const uint8_t badValue = 255;2021const size_t dataSize = 1024;22const size_t slopSize = 1024;2324std::vector<uint8_t> data(dataSize + slopSize);25std::fill(data.begin(), data.begin() + dataSize, goodValue);26std::fill(data.begin() + dataSize, data.end(), badValue);2728std::vector<uint8_t> outputData(dataSize);2930auto checkDataIsSafe = [=](uint8_t item) { return item == goodValue; };3132{33// One large read34gl::BinaryInputStream stream(data.data(), dataSize);35stream.readBytes(outputData.data(), dataSize);36ASSERT_FALSE(stream.error());37ASSERT_TRUE(std::all_of(outputData.begin(), outputData.end(), checkDataIsSafe));38ASSERT_TRUE(stream.endOfStream());39}4041{42// Two half-sized reads43gl::BinaryInputStream stream(data.data(), dataSize);44stream.readBytes(outputData.data(), dataSize / 2);45ASSERT_FALSE(stream.error());46stream.readBytes(outputData.data() + dataSize / 2, dataSize / 2);47ASSERT_FALSE(stream.error());48ASSERT_TRUE(std::all_of(outputData.begin(), outputData.end(), checkDataIsSafe));49ASSERT_TRUE(stream.endOfStream());50}5152{53// One large read that is too big54gl::BinaryInputStream stream(data.data(), dataSize);55stream.readBytes(outputData.data(), dataSize + 1);56ASSERT_TRUE(stream.error());57}5859{60// Two reads, one that overflows the offset61gl::BinaryInputStream stream(data.data(), dataSize);62stream.readBytes(outputData.data(), dataSize - 1);63ASSERT_FALSE(stream.error());64stream.readBytes(outputData.data(), std::numeric_limits<size_t>::max() - dataSize - 2);65}66}6768// Test that readIntVector and writeIntVector match.69TEST(BinaryStream, IntVector)70{71std::vector<unsigned int> writeData = {1, 2, 3, 4, 5};72std::vector<unsigned int> readData;7374gl::BinaryOutputStream out;75out.writeIntVector(writeData);7677gl::BinaryInputStream in(out.data(), out.length());78in.readIntVector<unsigned int>(&readData);7980ASSERT_EQ(writeData.size(), readData.size());8182for (size_t i = 0; i < writeData.size(); ++i)83{84ASSERT_EQ(writeData[i], readData[i]);85}86}87} // namespace angle888990