mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-03 10:55:57 -04:00
Add tests for RWops
This commit is contained in:
parent
fbf5d046ea
commit
5542acd252
1
testdata/test.txt
vendored
Normal file
1
testdata/test.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
abcdefgh
|
@ -6,6 +6,8 @@ SET(HEADER_TESTS
|
||||
header_point
|
||||
header_rect
|
||||
header_renderer
|
||||
header_rwops
|
||||
header_extrarwops
|
||||
header_sdl
|
||||
header_sdl2pp
|
||||
header_texture
|
||||
@ -15,6 +17,7 @@ SET(HEADER_TESTS
|
||||
# simple command-line tests
|
||||
SET(CLI_TESTS
|
||||
test_pointrect
|
||||
test_rwops
|
||||
)
|
||||
|
||||
# tests which test graphics functionality and thus requre working
|
||||
@ -23,6 +26,8 @@ SET(GUI_TESTS
|
||||
gui_rendering
|
||||
)
|
||||
|
||||
ADD_DEFINITIONS(-DTESTDATA_DIR="${PROJECT_SOURCE_DIR}/testdata")
|
||||
|
||||
FOREACH(TEST ${HEADER_TESTS})
|
||||
ADD_EXECUTABLE(${TEST} ${TEST}.cc)
|
||||
ENDFOREACH(TEST ${TESTS})
|
||||
|
5
tests/header_extrarwops.cc
Normal file
5
tests/header_extrarwops.cc
Normal file
@ -0,0 +1,5 @@
|
||||
#include <SDL2pp/ExtraRWops.hh>
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
}
|
5
tests/header_rwops.cc
Normal file
5
tests/header_rwops.cc
Normal file
@ -0,0 +1,5 @@
|
||||
#include <SDL2pp/RWops.hh>
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
}
|
132
tests/test_rwops.cc
Normal file
132
tests/test_rwops.cc
Normal file
@ -0,0 +1,132 @@
|
||||
#include <vector>
|
||||
|
||||
#include <SDL2pp/Exception.hh>
|
||||
#include <SDL2pp/ExtraRWops.hh>
|
||||
#include <SDL2pp/RWops.hh>
|
||||
|
||||
#include "testing.h"
|
||||
|
||||
using namespace SDL2pp;
|
||||
|
||||
BEGIN_TEST()
|
||||
// First check our custom ContainerRWops, not touching
|
||||
// SDL-provided RWops services
|
||||
{
|
||||
std::vector<char> buffer = { 'a', 'b', 'c', 'd' };
|
||||
|
||||
RWops rw((ContainerRWops<std::vector<char>>(buffer)));
|
||||
|
||||
{
|
||||
// Initial position
|
||||
EXPECT_TRUE(rw.Tell() == 0);
|
||||
}
|
||||
|
||||
{
|
||||
// Seeks
|
||||
EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0);
|
||||
EXPECT_TRUE(rw.Tell() == 0);
|
||||
EXPECT_TRUE(rw.Seek(1, SEEK_SET) == 1);
|
||||
EXPECT_TRUE(rw.Tell() == 1);
|
||||
EXPECT_TRUE(rw.Seek(1, SEEK_CUR) == 2);
|
||||
EXPECT_TRUE(rw.Tell() == 2);
|
||||
EXPECT_TRUE(rw.Seek(-1, SEEK_END) == 3);
|
||||
EXPECT_TRUE(rw.Tell() == 3);
|
||||
}
|
||||
|
||||
{
|
||||
// Read via C++
|
||||
EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0);
|
||||
|
||||
char buf[4] = {0};
|
||||
EXPECT_TRUE(rw.Read(buf, 1, 4) == 4);
|
||||
EXPECT_TRUE(buf[0] == 'a' && buf[3] == 'd');
|
||||
|
||||
// Position after read
|
||||
EXPECT_TRUE(rw.Tell() == 4);
|
||||
}
|
||||
|
||||
{
|
||||
// Read via C++
|
||||
EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0);
|
||||
|
||||
char buf[4] = {0};
|
||||
EXPECT_TRUE(rw.Read(buf, 4, 1) == 1);
|
||||
EXPECT_TRUE(buf[0] == 'a' && buf[3] == 'd');
|
||||
|
||||
// Position after read
|
||||
EXPECT_TRUE(rw.Tell() == 4);
|
||||
}
|
||||
|
||||
{
|
||||
// Read via SDL
|
||||
EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0);
|
||||
|
||||
char buf[4] = {0};
|
||||
EXPECT_TRUE(SDL_RWread(rw.Get(), buf, 1, 4) == 4);
|
||||
EXPECT_TRUE(buf[0] == 'a' && buf[3] == 'd');
|
||||
|
||||
// Position after read
|
||||
EXPECT_TRUE(rw.Tell() == 4);
|
||||
}
|
||||
|
||||
{
|
||||
// Overread
|
||||
EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0);
|
||||
|
||||
char buf[6] = {0};
|
||||
EXPECT_TRUE(SDL_RWread(rw.Get(), buf, 3, 2) == 1);
|
||||
rw.Seek(0, SEEK_SET);
|
||||
EXPECT_TRUE(SDL_RWread(rw.Get(), buf, 2, 3) == 2);
|
||||
}
|
||||
|
||||
{
|
||||
// Write
|
||||
EXPECT_TRUE(rw.Seek(0, SEEK_SET) == 0);
|
||||
|
||||
char buf[2] = {'1', '2'};
|
||||
EXPECT_TRUE(rw.Write(buf, 1, 2) == 2);
|
||||
EXPECT_TRUE(rw.Write(buf, 2, 1) == 1);
|
||||
EXPECT_TRUE(SDL_RWwrite(rw.Get(), buf, 1, 2) == 2);
|
||||
EXPECT_TRUE(SDL_RWwrite(rw.Get(), buf, 2, 1) == 1);
|
||||
|
||||
EXPECT_TRUE(rw.Tell() == 8);
|
||||
EXPECT_TRUE(buffer.size() == 8);
|
||||
EXPECT_TRUE(buffer == std::vector<char>({'1', '2', '1', '2', '1', '2', '1', '2'}));
|
||||
}
|
||||
|
||||
{
|
||||
// Write past EOF
|
||||
char buf[2] = {'x', 'y'};
|
||||
EXPECT_TRUE(rw.Seek(100, SEEK_SET) == 100);
|
||||
|
||||
EXPECT_TRUE(rw.Write(buf, 1, 2) == 2);
|
||||
EXPECT_TRUE(rw.Tell() == 102);
|
||||
EXPECT_TRUE(buffer.size() == 102);
|
||||
EXPECT_TRUE(buffer[99] == '\0');
|
||||
EXPECT_TRUE(buffer[100] == 'x');
|
||||
EXPECT_TRUE(buffer[101] == 'y');
|
||||
}
|
||||
|
||||
{
|
||||
// Test moving
|
||||
RWops rw1(std::move(rw));
|
||||
rw1.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// SDL file read test
|
||||
{
|
||||
RWops rw = RWops::FromFile(TESTDATA_DIR "/test.txt");
|
||||
|
||||
EXPECT_TRUE(rw.Tell() == 0);
|
||||
char buf[8] = {0};
|
||||
EXPECT_TRUE(rw.Read(buf, 1, 8) == 8);
|
||||
EXPECT_TRUE(buf[0] == 'a');
|
||||
EXPECT_TRUE(buf[7] == 'h');
|
||||
EXPECT_TRUE(rw.Tell() == 8);
|
||||
|
||||
rw.Close();
|
||||
}
|
||||
HANDLE_EXCEPTION(Exception& e)
|
||||
std::cerr << "unexpected SDL exception was thrown during the test: " << e.what() << ": " << e.GetSDLError() << std::endl;
|
||||
END_TEST()
|
Loading…
x
Reference in New Issue
Block a user