Skip to content

Commit

Permalink
build switch for precise timer,fixes #131 cursor blinking test macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffaello committed May 16, 2020
1 parent 096daac commit aa05c9d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 30 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ include(CMakeDependentOption)
CMAKE_DEPENDENT_OPTION(TEST_DUMP_SNAPSHOT "build test to dump the expected output instead of verifing it" OFF
"BUILD_SNAPSHOT" OFF)
option(WITH_SDL2_STATIC "linking STATIC LIB with SDL2 STATIC" OFF)
option(HAVE_PRECISE_TIMER "if SDL2 timers are precise" ON)

enable_testing()
add_subdirectory ("sdl2-vga-terminal")
14 changes: 1 addition & 13 deletions azure-pipelines/analyzer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,8 @@ stages:
BUILD_EXAMPLES: OFF
BUILD_SHARED_LIBS: ON
ENABLE_CODE_COVERAGE: ON
HAVE_PRECISE_TIMER: OFF
jobs:
# - template: 'templates/sonarcloud-job.yml'
# parameters:
# imageName: ${{ variables.windows_image }}
# jobName: SonarCloud_win
# jobDisplayName: SonarCloud (WIN)
# vcpkg_key_cache: $(CACHE_VCPKG_KEY)
# triplet: x64-windows
# arch: 64
# build_type: Debug
# sonar_wrapper_url: 'https://sonarcloud.io/static/cpp/build-wrapper-win-x86.zip'
# build_wrapper: 'build-wrapper-win-x86\build-wrapper-win-x86-64.exe'
# build_wrapper_path: build/build-wrapper-win-x86
# cmake_ignore_path: $(CMAKE_IGNORE_PATH_WIN)
- template: 'templates/sonarcloud-job.yml'
parameters:
imageName: ${{ variables.mac_image }}
Expand Down
2 changes: 2 additions & 0 deletions azure-pipelines/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ stages:
- stage: CI
jobs:
- job: macOS
variables:
HAVE_PRECISE_TIMER: OFF
strategy:
matrix:
Debug:
Expand Down
2 changes: 2 additions & 0 deletions azure-pipelines/matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ stages:
- stage: matrix_macos
displayName: Matrix MacOS
dependsOn: Dep
variables:
HAVE_PRECISE_TIMER: OFF
jobs:
- template: 'templates/matrix-job.yml'
parameters:
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines/variables/build_switches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ variables:
BUILD_EXAMPLES: ON
BUILD_SNAPSHOT: OFF
ENABLE_CODE_COVERAGE: OFF
HAVE_PRECISE_TIMER: ON
2 changes: 1 addition & 1 deletion azure-pipelines/variables/global.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variables:
CACHE_VCPKG_KEY: v4 | vcpkg-root
VCPKG_CMAKE: "$(Build.SourcesDirectory)/vcpkg/scripts/buildsystems/vcpkg.cmake"
CMAKE_ARGS: -DWITH_SDL2_STATIC=$(SDL2_STATIC) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DBUILD_SNAPSHOT=$(BUILD_SNAPSHOT) -DENABLE_CODE_COVERAGE=$(ENABLE_CODE_COVERAGE)
CMAKE_ARGS: -DWITH_SDL2_STATIC=$(SDL2_STATIC) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DBUILD_SNAPSHOT=$(BUILD_SNAPSHOT) -DENABLE_CODE_COVERAGE=$(ENABLE_CODE_COVERAGE) -DHAVE_PRECISE_TIMER=$(HAVE_PRECISE_TIMER)
CMAKE_IGNORE_PATH_WIN: "C:/MinGW/bin;C:/Strawberry/c/bin;C:/Program Files (x86)/LLVM/bin;C:/ProgramData/chocolatey/bin"
5 changes: 5 additions & 0 deletions sdl2-vga-terminal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ if (WIN32)
set(LIB_SDL2main SDL2::SDL2main)
endif()

if(HAVE_PRECISE_TIMER)
else()
add_compile_definitions(NO_PRECISE_TIMER)
endif()

########################### Static Lib #######################################
set(LIB_NAME vga-terminal-static)
set(LIB_CONF ${LIB_NAME}-config)
Expand Down
4 changes: 4 additions & 0 deletions sdl2-vga-terminal/test/Environment.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <SDL2/SDL.h>

class Environment : public ::testing::Environment {
public:
~Environment() override {}
Expand Down
45 changes: 29 additions & 16 deletions sdl2-vga-terminal/test/VgaTerminalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ void cmpTerminalChar(const VgaTerminal::terminalChar_t& tc1, const VgaTerminal::
EXPECT_EQ(tc1.col, tc2.col);
}

// expect around ~1% tolerance on the given value to be matched
void cmpTicks(const uint32_t start, const uint32_t end, const uint16_t value)
// expect around ~10% tolerance on the given value to be matched
void cmpTicks(const uint64_t start, const uint64_t end, const uint16_t value)
{
double te = static_cast<double>(value) * 0.01;
double t = abs(static_cast<double>(end - start) - value) - te;
EXPECT_LE(t, 1);
double te = ceil(static_cast<double>(value) * 0.10);
double diff = static_cast<double>(end - start)/ (SDL_GetPerformanceFrequency()/1000.0);
double dt = abs(diff - static_cast<double>(value));
std::cout << "value=" << value << " --- diff=" << diff << " --- te=" << te << " --- dt=" << dt << std::endl;
EXPECT_LE(dt, te);
}

TEST(VgaTerminal, checkVersion)
Expand Down Expand Up @@ -355,32 +357,40 @@ TEST(VgaTerminal, cursorNoBlinking)
{
SDL_Event e;
std::memset(&e, 0, sizeof(SDL_Event));
SDL_EventState(SDL_WINDOWEVENT, SDL_DISABLE);

std::string title = ::testing::UnitTest::GetInstance()->current_test_info()->name();
VgaTerminal term = VgaTerminal(title, SDL_WINDOW_HIDDEN, -1, 0);
term.cursor_time = 100;
term.blinkCursor = false;
term.setCursorSpeed(VgaTerminal::CURSOR_SPEED::CURSOR_SPEED_FAST);
uint16_t cursorWaitTime = static_cast<uint32_t>(term.getCursorSpeed()) * 2;

// flush the SDL_event queue too...
SDL_FlushEvents(0, 0xFFFFFFFF);
EXPECT_EQ(0, SDL_WaitEventTimeout(&e, 1000));
EXPECT_EQ(0, SDL_WaitEventTimeout(&e, cursorWaitTime));
EXPECT_EQ(e.type, 0);
}

class CursorBlinkingTests: public ::testing::TestWithParam<uint16_t> {};
TEST_P(CursorBlinkingTests, cursorBlinking)
{
uint16_t cursorTime = GetParam();
uint16_t cursorWaitTime = cursorTime * 4;
SDL_Event e;
std::memset(&e, 0, sizeof(SDL_Event));
SDL_EventState(SDL_WINDOWEVENT, SDL_DISABLE);

std::string title = ::testing::UnitTest::GetInstance()->current_test_info()->name();
VgaTerminal term = VgaTerminal(title, SDL_WINDOW_HIDDEN, -1, 0);
SDL_FlushEvents(0, 0xFFFFFFFF);
uint32_t init = SDL_GetTicks();
uint16_t old_time = term.cursor_time;
term.cursor_time = cursorTime;
ASSERT_EQ(1, SDL_WaitEventTimeout(&e, 1000));
uint32_t start = SDL_GetTicks();
ASSERT_EQ(1, SDL_WaitEventTimeout(&e, cursorTime * 2));
uint32_t end = SDL_GetTicks();
EXPECT_EQ(old_time, static_cast<uint16_t>(VgaTerminal::CURSOR_SPEED::CURSOR_SPEED_NORMAL));
SDL_FlushEvents(0, 0xFFFFFFFF);
// Discard first "blink" as it could be the default one
EXPECT_EQ(1, SDL_WaitEventTimeout(&e, cursorWaitTime));
uint64_t start = SDL_GetPerformanceCounter();
ASSERT_EQ(1, SDL_WaitEventTimeout(&e, cursorWaitTime));
uint64_t end = SDL_GetPerformanceCounter();

EXPECT_EQ(e.type, SDL_USEREVENT);
EXPECT_EQ(e.window.windowID, term.getWindowId());
Expand All @@ -389,16 +399,19 @@ TEST_P(CursorBlinkingTests, cursorBlinking)
EXPECT_TRUE(NULL == e.user.data2);
EXPECT_EQ(e.user.type, SDL_USEREVENT); // ???
EXPECT_EQ(e.user.windowID, term.getWindowId());
// give a tollerance of 1%
// give some tollerance (~2%)
#ifndef NO_PRECISE_TIMER
cmpTicks(start, end, cursorTime);
cmpTicks(init, start, old_time);
#endif
}
INSTANTIATE_TEST_SUITE_P(
VgaTerminal,
CursorBlinkingTests,
::testing::Values(
500,
VgaTerminal::CURSOR_SPEED::CURSOR_SPEED_NORMAL
VgaTerminal::CURSOR_SPEED::CURSOR_SPEED_NORMAL,
VgaTerminal::CURSOR_SPEED::CURSOR_SPEED_SLOW,
VgaTerminal::CURSOR_SPEED::CURSOR_SPEED_FAST
)
);

Expand Down

0 comments on commit aa05c9d

Please sign in to comment.