Compare commits
5 Commits
d947729d68
...
74812d0a1f
| Author | SHA1 | Date |
|---|---|---|
|
|
74812d0a1f | |
|
|
ebc1f4a58d | |
|
|
b796a1afde | |
|
|
13f919f22d | |
|
|
2e32335fa1 |
|
|
@ -17,7 +17,7 @@ jobs:
|
|||
run: |
|
||||
sed -i 's/SigLevel = Required DatabaseOptional/SigLevel = Optional TrustAll/' /etc/pacman.conf
|
||||
pacman --noconfirm --noprogressbar -Syyu
|
||||
pacman --noconfirm --noprogressbar -Sy gcc base-devel cmake clang libc++ pixman
|
||||
pacman --noconfirm --noprogressbar -Sy gcc gtest base-devel cmake clang libc++ pixman
|
||||
|
||||
- name: Build hyprutils with gcc
|
||||
run: |
|
||||
|
|
@ -44,7 +44,7 @@ jobs:
|
|||
run: |
|
||||
sed -i 's/SigLevel = Required DatabaseOptional/SigLevel = Optional TrustAll/' /etc/pacman.conf
|
||||
pacman --noconfirm --noprogressbar -Syyu
|
||||
pacman --noconfirm --noprogressbar -Sy gcc base-devel cmake clang libc++ pixman
|
||||
pacman --noconfirm --noprogressbar -Sy gcc gtest base-devel cmake clang libc++ pixman
|
||||
|
||||
- name: Build hyprutils with clang
|
||||
run: |
|
||||
|
|
|
|||
|
|
@ -53,64 +53,22 @@ set_target_properties(hyprutils PROPERTIES VERSION ${hyprutils_VERSION}
|
|||
SOVERSION 9)
|
||||
target_link_libraries(hyprutils PkgConfig::deps)
|
||||
|
||||
# tests
|
||||
add_custom_target(tests)
|
||||
|
||||
add_executable(hyprutils_memory "tests/memory.cpp")
|
||||
target_link_libraries(hyprutils_memory PRIVATE hyprutils PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Memory"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_memory "memory")
|
||||
add_dependencies(tests hyprutils_memory)
|
||||
|
||||
add_executable(hyprutils_string "tests/string.cpp")
|
||||
target_link_libraries(hyprutils_string PRIVATE hyprutils PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "String"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_string "string")
|
||||
add_dependencies(tests hyprutils_string)
|
||||
|
||||
add_executable(hyprutils_signal "tests/signal.cpp")
|
||||
target_link_libraries(hyprutils_signal PRIVATE hyprutils PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Signal"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_signal "signal")
|
||||
add_dependencies(tests hyprutils_signal)
|
||||
|
||||
add_executable(hyprutils_math "tests/math.cpp")
|
||||
target_link_libraries(hyprutils_math PRIVATE hyprutils PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Math"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_math "math")
|
||||
add_dependencies(tests hyprutils_math)
|
||||
|
||||
add_executable(hyprutils_os "tests/os.cpp")
|
||||
target_link_libraries(hyprutils_os PRIVATE hyprutils PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "OS"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_os "os")
|
||||
add_dependencies(tests hyprutils_os)
|
||||
|
||||
add_executable(hyprutils_filedescriptor "tests/filedescriptor.cpp")
|
||||
target_link_libraries(hyprutils_filedescriptor PRIVATE hyprutils PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Filedescriptor"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_filedescriptor "filedescriptor")
|
||||
add_dependencies(tests hyprutils_filedescriptor)
|
||||
|
||||
add_executable(hyprutils_animation "tests/animation.cpp")
|
||||
target_link_libraries(hyprutils_animation PRIVATE hyprutils PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Animation"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_animation "utils")
|
||||
add_dependencies(tests hyprutils_animation)
|
||||
if(BUILD_TESTING)
|
||||
# GTest
|
||||
find_package(GTest CONFIG REQUIRED)
|
||||
include(GoogleTest)
|
||||
add_executable(hyprutils_inline_tests ${SRCFILES})
|
||||
target_compile_definitions(hyprutils_inline_tests PRIVATE HU_UNIT_TESTS=1)
|
||||
target_compile_options(hyprutils_inline_tests PRIVATE --coverage)
|
||||
target_link_options(hyprutils_inline_tests PRIVATE --coverage)
|
||||
target_include_directories(
|
||||
hyprutils_inline_tests
|
||||
PUBLIC "./include"
|
||||
PRIVATE "./src" "./src/include" "./protocols" "${CMAKE_BINARY_DIR}")
|
||||
target_link_libraries(hyprutils_inline_tests PRIVATE GTest::gtest_main
|
||||
PkgConfig::deps)
|
||||
gtest_discover_tests(hyprutils_inline_tests)
|
||||
endif()
|
||||
|
||||
# Installation
|
||||
install(TARGETS hyprutils)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,12 @@
|
|||
hyprutils (0.10.2-0.1) UNRELEASED; urgency=medium
|
||||
|
||||
* Non-maintainer upload
|
||||
* New upstream release (Closes: #1120292)
|
||||
* d/control: add libgtest-dev to build-deps
|
||||
* d/tests: port testsuite to gTest
|
||||
|
||||
-- Carl Keinath <carl.keinath@gmail.com> Thu, 13 Nov 2025 23:01:28 +0100
|
||||
|
||||
hyprutils (0.10.0-0.1) unstable; urgency=medium
|
||||
|
||||
* Non-maintainer upload
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ Build-Depends:
|
|||
cmake,
|
||||
libpixman-1-dev,
|
||||
pkgconf,
|
||||
libgtest-dev,
|
||||
Standards-Version: 4.7.2
|
||||
Homepage: https://github.com/hyprwm/hyprutils
|
||||
Vcs-Browser: https://salsa.debian.org/NyxTrail/hyprutils
|
||||
|
|
|
|||
|
|
@ -1,74 +1,30 @@
|
|||
cmake_minimum_required(VERSION 3.19)
|
||||
project(hyprutils)
|
||||
|
||||
include(CTest)
|
||||
project(hyprutils_tests LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
add_compile_options(
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wno-unused-parameter
|
||||
-Wno-unused-value
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-narrowing
|
||||
-Wno-pointer-arith
|
||||
)
|
||||
|
||||
enable_testing()
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wno-unused-parameter
|
||||
-Wno-unused-value
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-narrowing
|
||||
-Wno-pointer-arith)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(GTest REQUIRED)
|
||||
pkg_check_modules(HYPRUTILS REQUIRED hyprutils)
|
||||
pkg_check_modules(deps REQUIRED IMPORTED_TARGET pixman-1)
|
||||
|
||||
find_library(HYPRUTILS_LIB hyprutils REQUIRED)
|
||||
file(GLOB_RECURSE SRCFILES "src/*.cpp" "include/*.hpp")
|
||||
|
||||
add_executable(hyprutils_inline_tests ${SRCFILES})
|
||||
target_compile_definitions(hyprutils_inline_tests PRIVATE HU_UNIT_TESTS=1)
|
||||
|
||||
add_executable(hyprutils_memory "tests/memory.cpp")
|
||||
target_link_libraries(hyprutils_memory PRIVATE ${HYPRUTILS_LIB} PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Memory"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_memory "memory")
|
||||
|
||||
add_executable(hyprutils_string "tests/string.cpp")
|
||||
target_link_libraries(hyprutils_string PRIVATE ${HYPRUTILS_LIB} PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "String"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_string "string")
|
||||
|
||||
add_executable(hyprutils_signal "tests/signal.cpp")
|
||||
target_link_libraries(hyprutils_signal PRIVATE ${HYPRUTILS_LIB} PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Signal"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_signal "signal")
|
||||
|
||||
add_executable(hyprutils_math "tests/math.cpp")
|
||||
target_link_libraries(hyprutils_math PRIVATE ${HYPRUTILS_LIB} PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Math"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_math "math")
|
||||
|
||||
add_executable(hyprutils_os "tests/os.cpp")
|
||||
target_link_libraries(hyprutils_os PRIVATE ${HYPRUTILS_LIB} PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "OS"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_os "os")
|
||||
|
||||
add_executable(hyprutils_filedescriptor "tests/filedescriptor.cpp")
|
||||
target_link_libraries(hyprutils_filedescriptor PRIVATE ${HYPRUTILS_LIB} PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Filedescriptor"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_filedescriptor "filedescriptor")
|
||||
|
||||
add_executable(hyprutils_animation "tests/animation.cpp")
|
||||
target_link_libraries(hyprutils_animation PRIVATE ${HYPRUTILS_LIB} PkgConfig::deps)
|
||||
add_test(
|
||||
NAME "Animation"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
|
||||
COMMAND hyprutils_animation "utils")
|
||||
target_include_directories(hyprutils_inline_tests PRIVATE ../include)
|
||||
target_link_libraries(hyprutils_inline_tests PRIVATE ${HYPRUTILS_LIBRARIES}
|
||||
GTest::gtest_main
|
||||
PkgConfig::deps)
|
||||
|
||||
gtest_discover_tests(hyprutils_inline_tests)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@
|
|||
set -eu
|
||||
|
||||
mkdir -p "$AUTOPKGTEST_TMP"
|
||||
cp -r tests "$AUTOPKGTEST_TMP"/tests
|
||||
cp -r src "$AUTOPKGTEST_TMP"/src
|
||||
cp -r include "$AUTOPKGTEST_TMP"/include
|
||||
cp debian/tests/CMakeLists.txt "$AUTOPKGTEST_TMP"/CMakeLists.txt
|
||||
|
||||
mkdir -p "$AUTOPKGTEST_TMP"/build
|
||||
|
|
@ -11,4 +12,3 @@ cd "$AUTOPKGTEST_TMP"/build
|
|||
|
||||
cmake ..
|
||||
cmake --build .
|
||||
ctest --output-on-failure
|
||||
|
|
|
|||
|
|
@ -0,0 +1,55 @@
|
|||
#pragma once
|
||||
|
||||
#include "../memory/UniquePtr.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
|
||||
namespace Hyprutils::I18n {
|
||||
struct SI18nEngineImpl;
|
||||
|
||||
typedef std::unordered_map<std::string, std::string> translationVarMap;
|
||||
typedef std::function<std::string(const translationVarMap&)> translationFn;
|
||||
|
||||
class CI18nLocale {
|
||||
public:
|
||||
~CI18nLocale() = default;
|
||||
|
||||
std::string locale();
|
||||
std::string stem();
|
||||
std::string full();
|
||||
|
||||
private:
|
||||
CI18nLocale(std::string fullLocale);
|
||||
|
||||
std::string m_locale, m_rawFullLocale;
|
||||
|
||||
friend class CI18nEngine;
|
||||
};
|
||||
|
||||
class CI18nEngine {
|
||||
public:
|
||||
CI18nEngine();
|
||||
~CI18nEngine();
|
||||
/*
|
||||
Register a translation entry. The internal translation db is kept as a vector,
|
||||
so make sure your keys are linear, don't use e.g. 2 billion as that will call
|
||||
.resize() on the vec to 2 billion.
|
||||
|
||||
If you pass a Fn, you can do logic, e.g. "1 point" vs "2 points".
|
||||
*/
|
||||
void registerEntry(const std::string& locale, uint64_t key, std::string&& translationUTF8);
|
||||
void registerEntry(const std::string& locale, uint64_t key, translationFn&& translationFn);
|
||||
|
||||
void setFallbackLocale(const std::string& locale);
|
||||
|
||||
std::string localizeEntry(const std::string& locale, uint64_t key, const translationVarMap& map);
|
||||
|
||||
CI18nLocale getSystemLocale();
|
||||
|
||||
private:
|
||||
Memory::CUniquePointer<SI18nEngineImpl> m_impl;
|
||||
};
|
||||
}
|
||||
|
|
@ -4,8 +4,10 @@
|
|||
namespace Hyprutils {
|
||||
namespace String {
|
||||
// trims beginning and end of whitespace characters
|
||||
std::string trim(const std::string& in);
|
||||
bool isNumber(const std::string& str, bool allowfloat = false);
|
||||
void replaceInString(std::string& string, const std::string& what, const std::string& to);
|
||||
std::string trim(const char* in);
|
||||
std::string trim(const std::string& in);
|
||||
std::string_view trim(const std::string_view& in);
|
||||
bool isNumber(const std::string& str, bool allowfloat = false);
|
||||
void replaceInString(std::string& string, const std::string& what, const std::string& to);
|
||||
};
|
||||
};
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
#pragma once
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace Hyprutils {
|
||||
namespace String {
|
||||
class CVarList2 {
|
||||
public:
|
||||
/** Split string into arg list
|
||||
Prefer this over CConstVarList / CVarList, this is better.
|
||||
|
||||
@param lastArgNo stop splitting after argv reaches maximum size, last arg will contain rest of unsplit args
|
||||
@param delim if delimiter is 's', use std::isspace
|
||||
@param removeEmpty remove empty args from argv
|
||||
@param allowEscape whether to allow escaping the delimiter
|
||||
*/
|
||||
CVarList2(std::string&& in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false, const bool allowEscape = true);
|
||||
|
||||
~CVarList2() = default;
|
||||
|
||||
size_t size() const {
|
||||
return m_args.size();
|
||||
}
|
||||
|
||||
std::string join(const std::string& joiner, size_t from = 0, size_t to = 0) const;
|
||||
void append(std::string&& arg);
|
||||
bool contains(const std::string& el);
|
||||
|
||||
std::string_view operator[](const size_t& idx) const {
|
||||
if (idx >= m_args.size())
|
||||
return "";
|
||||
return m_args[idx];
|
||||
}
|
||||
|
||||
// for range-based loops
|
||||
std::vector<std::string_view>::const_iterator begin() const {
|
||||
return m_args.begin();
|
||||
}
|
||||
std::vector<std::string_view>::const_iterator end() const {
|
||||
return m_args.end();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_inString;
|
||||
std::vector<std::string> m_copyStrings;
|
||||
std::vector<std::string_view> m_args;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@
|
|||
stdenvAdapters,
|
||||
cmake,
|
||||
pkg-config,
|
||||
gtest,
|
||||
pixman,
|
||||
version ? "git",
|
||||
doCheck ? false,
|
||||
|
|
@ -11,10 +12,12 @@
|
|||
# whether to use the mold linker
|
||||
# disable this for older machines without SSE4_2 and AVX2 support
|
||||
withMold ? true,
|
||||
}: let
|
||||
}:
|
||||
let
|
||||
inherit (builtins) foldl';
|
||||
inherit (lib.lists) flatten;
|
||||
inherit (lib.strings) optionalString;
|
||||
inherit (lib.attrsets) mapAttrsToList;
|
||||
inherit (lib.lists) flatten optional;
|
||||
inherit (lib.strings) cmakeBool optionalString;
|
||||
|
||||
adapters = flatten [
|
||||
(lib.optional withMold stdenvAdapters.useMoldLinker)
|
||||
|
|
@ -23,31 +26,36 @@
|
|||
|
||||
customStdenv = foldl' (acc: adapter: adapter acc) stdenv adapters;
|
||||
in
|
||||
customStdenv.mkDerivation {
|
||||
pname = "hyprutils" + optionalString debug "-debug";
|
||||
inherit version doCheck;
|
||||
src = ../.;
|
||||
customStdenv.mkDerivation {
|
||||
pname = "hyprutils" + optionalString debug "-debug";
|
||||
inherit version doCheck;
|
||||
src = ../.;
|
||||
|
||||
nativeBuildInputs = [
|
||||
cmake
|
||||
pkg-config
|
||||
];
|
||||
nativeBuildInputs = [
|
||||
cmake
|
||||
pkg-config
|
||||
];
|
||||
|
||||
buildInputs = [
|
||||
pixman
|
||||
];
|
||||
buildInputs = flatten [
|
||||
(optional doCheck gtest)
|
||||
pixman
|
||||
];
|
||||
|
||||
outputs = ["out" "dev"];
|
||||
outputs = [
|
||||
"out"
|
||||
"dev"
|
||||
];
|
||||
|
||||
cmakeBuildType =
|
||||
if debug
|
||||
then "Debug"
|
||||
else "RelWithDebInfo";
|
||||
cmakeBuildType = if debug then "Debug" else "RelWithDebInfo";
|
||||
|
||||
meta = with lib; {
|
||||
homepage = "https://github.com/hyprwm/hyprutils";
|
||||
description = "Small C++ library for utilities used across the Hypr* ecosystem";
|
||||
license = licenses.bsd3;
|
||||
platforms = platforms.linux;
|
||||
};
|
||||
}
|
||||
cmakeFlags = mapAttrsToList cmakeBool {
|
||||
"DISABLE_TESTING" = !doCheck;
|
||||
};
|
||||
|
||||
meta = with lib; {
|
||||
homepage = "https://github.com/hyprwm/hyprutils";
|
||||
description = "Small C++ library for utilities used across the Hypr* ecosystem";
|
||||
license = licenses.bsd3;
|
||||
platforms = platforms.linux;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,3 +99,404 @@ const std::unordered_map<std::string, SP<CBezierCurve>>& CAnimationManager::getA
|
|||
CWeakPointer<CAnimationManager::SAnimationManagerSignals> CAnimationManager::getSignals() const {
|
||||
return m_events;
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <hyprutils/animation/AnimationConfig.hpp>
|
||||
#include <hyprutils/animation/AnimationManager.hpp>
|
||||
#include <hyprutils/animation/AnimatedVariable.hpp>
|
||||
#include <hyprutils/memory/WeakPtr.hpp>
|
||||
#include <hyprutils/memory/UniquePtr.hpp>
|
||||
|
||||
#define SP CSharedPointer
|
||||
#define WP CWeakPointer
|
||||
#define UP CUniquePointer
|
||||
|
||||
using namespace Hyprutils::Animation;
|
||||
using namespace Hyprutils::Math;
|
||||
using namespace Hyprutils::Memory;
|
||||
|
||||
class EmtpyContext {};
|
||||
|
||||
template <typename VarType>
|
||||
using CAnimatedVariable = CGenericAnimatedVariable<VarType, EmtpyContext>;
|
||||
|
||||
template <typename VarType>
|
||||
using PANIMVAR = SP<CAnimatedVariable<VarType>>;
|
||||
|
||||
template <typename VarType>
|
||||
using PANIMVARREF = WP<CAnimatedVariable<VarType>>;
|
||||
|
||||
enum eAVTypes {
|
||||
INT = 1,
|
||||
TEST,
|
||||
};
|
||||
|
||||
struct SomeTestType {
|
||||
bool done = false;
|
||||
bool operator==(const SomeTestType& other) const {
|
||||
return done == other.done;
|
||||
}
|
||||
SomeTestType& operator=(const SomeTestType& other) {
|
||||
done = other.done;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
CAnimationConfigTree animationTree;
|
||||
|
||||
class CMyAnimationManager : public CAnimationManager {
|
||||
public:
|
||||
void tick() {
|
||||
for (size_t i = 0; i < m_vActiveAnimatedVariables.size(); i++) {
|
||||
const auto PAV = m_vActiveAnimatedVariables[i].lock();
|
||||
if (!PAV || !PAV->ok() || !PAV->isBeingAnimated())
|
||||
continue;
|
||||
|
||||
const auto SPENT = PAV->getPercent();
|
||||
const auto PBEZIER = getBezier(PAV->getBezierName());
|
||||
|
||||
if (SPENT >= 1.f || !PAV->enabled()) {
|
||||
PAV->warp(true, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto POINTY = PBEZIER->getYForPoint(SPENT);
|
||||
|
||||
switch (PAV->m_Type) {
|
||||
case eAVTypes::INT: {
|
||||
auto avInt = dc<CAnimatedVariable<int>*>(PAV.get());
|
||||
if (!avInt)
|
||||
std::cout << "Dynamic cast upcast failed\n";
|
||||
|
||||
const auto DELTA = avInt->goal() - avInt->value();
|
||||
avInt->value() = avInt->begun() + (DELTA * POINTY);
|
||||
} break;
|
||||
case eAVTypes::TEST: {
|
||||
auto avCustom = dc<CAnimatedVariable<SomeTestType>*>(PAV.get());
|
||||
if (!avCustom)
|
||||
std::cout << "Dynamic cast upcast failed\n";
|
||||
|
||||
if (SPENT >= 1.f)
|
||||
avCustom->value().done = true;
|
||||
} break;
|
||||
default: {
|
||||
std::cout << "What are we even doing?\n";
|
||||
} break;
|
||||
}
|
||||
|
||||
PAV->onUpdate();
|
||||
}
|
||||
|
||||
tickDone();
|
||||
}
|
||||
|
||||
template <typename VarType>
|
||||
void createAnimation(const VarType& v, PANIMVAR<VarType>& av, const std::string& animationConfigName) {
|
||||
constexpr const eAVTypes EAVTYPE = std::is_same_v<VarType, int> ? eAVTypes::INT : eAVTypes::TEST;
|
||||
const auto PAV = makeShared<CGenericAnimatedVariable<VarType, EmtpyContext>>();
|
||||
|
||||
PAV->create(EAVTYPE, sc<CAnimationManager*>(this), PAV, v);
|
||||
PAV->setConfig(animationTree.getConfig(animationConfigName));
|
||||
av = std::move(PAV);
|
||||
}
|
||||
|
||||
virtual void scheduleTick() {
|
||||
;
|
||||
}
|
||||
|
||||
virtual void onTicked() {
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
UP<CMyAnimationManager> pAnimationManager;
|
||||
|
||||
class Subject {
|
||||
public:
|
||||
Subject(const int& a, const int& b) {
|
||||
pAnimationManager->createAnimation(a, m_iA, "default");
|
||||
pAnimationManager->createAnimation(b, m_iB, "internal");
|
||||
pAnimationManager->createAnimation({}, m_iC, "default");
|
||||
}
|
||||
PANIMVAR<int> m_iA;
|
||||
PANIMVAR<int> m_iB;
|
||||
PANIMVAR<SomeTestType> m_iC;
|
||||
};
|
||||
|
||||
static int config() {
|
||||
pAnimationManager = makeUnique<CMyAnimationManager>();
|
||||
|
||||
int ret = 0;
|
||||
|
||||
animationTree.createNode("global");
|
||||
animationTree.createNode("internal");
|
||||
|
||||
animationTree.createNode("foo", "internal");
|
||||
animationTree.createNode("default", "global");
|
||||
animationTree.createNode("bar", "default");
|
||||
|
||||
/*
|
||||
internal
|
||||
↳ foo
|
||||
global
|
||||
↳ default
|
||||
↳ bar
|
||||
*/
|
||||
|
||||
auto barCfg = animationTree.getConfig("bar");
|
||||
auto internalCfg = animationTree.getConfig("internal");
|
||||
|
||||
// internal is a root node and should point to itself
|
||||
EXPECT_EQ(internalCfg->pParentAnimation.get(), internalCfg.get());
|
||||
EXPECT_EQ(internalCfg->pValues.get(), internalCfg.get());
|
||||
|
||||
animationTree.setConfigForNode("global", 1, 4.0, "default", "asdf");
|
||||
|
||||
EXPECT_EQ(barCfg->internalEnabled, -1);
|
||||
{
|
||||
const auto PVALUES = barCfg->pValues.lock();
|
||||
EXPECT_EQ(PVALUES->internalEnabled, 1);
|
||||
EXPECT_EQ(PVALUES->internalBezier, "default");
|
||||
EXPECT_EQ(PVALUES->internalStyle, "asdf");
|
||||
EXPECT_EQ(PVALUES->internalSpeed, 4.0);
|
||||
}
|
||||
EXPECT_EQ(barCfg->pParentAnimation.get(), animationTree.getConfig("default").get());
|
||||
|
||||
// Overwrite our own values
|
||||
animationTree.setConfigForNode("bar", 1, 4.2, "test", "qwer");
|
||||
|
||||
{
|
||||
const auto PVALUES = barCfg->pValues.lock();
|
||||
EXPECT_EQ(PVALUES->internalEnabled, 1);
|
||||
EXPECT_EQ(PVALUES->internalBezier, "test");
|
||||
EXPECT_EQ(PVALUES->internalStyle, "qwer");
|
||||
EXPECT_EQ(PVALUES->internalSpeed, 4.2f);
|
||||
}
|
||||
|
||||
// Now overwrite the parent
|
||||
animationTree.setConfigForNode("default", 0, 0.0, "zxcv", "foo");
|
||||
|
||||
{
|
||||
// Expecting no change
|
||||
const auto PVALUES = barCfg->pValues.lock();
|
||||
EXPECT_EQ(PVALUES->internalEnabled, 1);
|
||||
EXPECT_EQ(PVALUES->internalBezier, "test");
|
||||
EXPECT_EQ(PVALUES->internalStyle, "qwer");
|
||||
EXPECT_EQ(PVALUES->internalSpeed, 4.2f);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
TEST(Animation, animation) {
|
||||
config();
|
||||
|
||||
animationTree.createNode("global");
|
||||
animationTree.createNode("internal");
|
||||
|
||||
animationTree.createNode("default", "global");
|
||||
animationTree.setConfigForNode("global", 1, 4.0, "default", "asdf");
|
||||
|
||||
Subject s(0, 0);
|
||||
|
||||
EXPECT_EQ(s.m_iA->value(), 0);
|
||||
EXPECT_EQ(s.m_iB->value(), 0);
|
||||
|
||||
// Test destruction of a CAnimatedVariable
|
||||
{
|
||||
Subject s2(10, 10);
|
||||
// Adds them to active
|
||||
*s2.m_iA = 1;
|
||||
*s2.m_iB = 2;
|
||||
// We deliberately do not tick here, to make sure the destructor removes active animated variables
|
||||
}
|
||||
|
||||
EXPECT_EQ(pAnimationManager->shouldTickForNext(), false);
|
||||
EXPECT_EQ(s.m_iC->value().done, false);
|
||||
|
||||
*s.m_iA = 10;
|
||||
*s.m_iB = 100;
|
||||
*s.m_iC = SomeTestType(true);
|
||||
|
||||
EXPECT_EQ(s.m_iC->value().done, false);
|
||||
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT_EQ(s.m_iA->value(), 10);
|
||||
EXPECT_EQ(s.m_iB->value(), 100);
|
||||
EXPECT_EQ(s.m_iC->value().done, true);
|
||||
|
||||
s.m_iA->setValue(0);
|
||||
s.m_iB->setValue(0);
|
||||
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT_EQ(s.m_iA->value(), 10);
|
||||
EXPECT_EQ(s.m_iB->value(), 100);
|
||||
|
||||
// Test config stuff
|
||||
EXPECT_EQ(s.m_iA->getBezierName(), "default");
|
||||
EXPECT_EQ(s.m_iA->getStyle(), "asdf");
|
||||
EXPECT_EQ(s.m_iA->enabled(), true);
|
||||
|
||||
animationTree.getConfig("global")->internalEnabled = 0;
|
||||
|
||||
EXPECT_EQ(s.m_iA->enabled(), false);
|
||||
|
||||
*s.m_iA = 50;
|
||||
pAnimationManager->tick(); // Expecting a warp
|
||||
EXPECT_EQ(s.m_iA->value(), 50);
|
||||
|
||||
// Test missing pValues
|
||||
animationTree.getConfig("global")->internalEnabled = 0;
|
||||
animationTree.getConfig("default")->pValues.reset();
|
||||
|
||||
EXPECT_EQ(s.m_iA->enabled(), false);
|
||||
EXPECT_EQ(s.m_iA->getBezierName(), "default");
|
||||
EXPECT_EQ(s.m_iA->getStyle(), "");
|
||||
EXPECT_EQ(s.m_iA->getPercent(), 1.f);
|
||||
|
||||
// Reset
|
||||
animationTree.setConfigForNode("default", 1, 1, "default");
|
||||
|
||||
//
|
||||
// Test callbacks
|
||||
//
|
||||
int beginCallbackRan = 0;
|
||||
int updateCallbackRan = 0;
|
||||
int endCallbackRan = 0;
|
||||
s.m_iA->setCallbackOnBegin([&beginCallbackRan](WP<CBaseAnimatedVariable> pav) { beginCallbackRan++; });
|
||||
s.m_iA->setUpdateCallback([&updateCallbackRan](WP<CBaseAnimatedVariable> pav) { updateCallbackRan++; });
|
||||
s.m_iA->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> pav) { endCallbackRan++; }, false);
|
||||
|
||||
s.m_iA->setValueAndWarp(42);
|
||||
|
||||
EXPECT_EQ(beginCallbackRan, 0);
|
||||
EXPECT_EQ(updateCallbackRan, 1);
|
||||
EXPECT_EQ(endCallbackRan, 2); // first called when setting the callback, then when warping.
|
||||
|
||||
*s.m_iA = 1337;
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT_EQ(beginCallbackRan, 1);
|
||||
EXPECT_EQ(updateCallbackRan > 2, true);
|
||||
EXPECT_EQ(endCallbackRan, 3);
|
||||
|
||||
std::vector<PANIMVAR<int>> vars;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
vars.resize(vars.size() + 1);
|
||||
pAnimationManager->createAnimation(1, vars.back(), "default");
|
||||
*vars.back() = 1337;
|
||||
}
|
||||
|
||||
// test adding / removing vars during a tick
|
||||
s.m_iA->resetAllCallbacks();
|
||||
s.m_iA->setUpdateCallback([&vars](WP<CBaseAnimatedVariable> v) {
|
||||
if (v.lock() != vars.back())
|
||||
vars.back()->warp();
|
||||
});
|
||||
s.m_iA->setCallbackOnEnd([&s, &vars](auto) {
|
||||
vars.resize(vars.size() + 1);
|
||||
pAnimationManager->createAnimation(1, vars.back(), "default");
|
||||
*vars.back() = 1337;
|
||||
});
|
||||
|
||||
*s.m_iA = 1000000;
|
||||
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT_EQ(s.m_iA->value(), 1000000);
|
||||
// all vars should be set to 1337
|
||||
EXPECT_EQ(std::find_if(vars.begin(), vars.end(), [](const auto& v) { return v->value() != 1337; }) == vars.end(), true);
|
||||
|
||||
// test one-time callbacks
|
||||
s.m_iA->resetAllCallbacks();
|
||||
s.m_iA->setCallbackOnEnd([&endCallbackRan](auto) { endCallbackRan++; }, true);
|
||||
|
||||
EXPECT_EQ(endCallbackRan, 4);
|
||||
|
||||
s.m_iA->setValueAndWarp(10);
|
||||
|
||||
EXPECT_EQ(endCallbackRan, 4);
|
||||
EXPECT_EQ(s.m_iA->value(), 10);
|
||||
|
||||
// test warp
|
||||
*s.m_iA = 3;
|
||||
s.m_iA->setCallbackOnEnd([&endCallbackRan](auto) { endCallbackRan++; }, false);
|
||||
|
||||
s.m_iA->warp(false);
|
||||
EXPECT_EQ(endCallbackRan, 4);
|
||||
|
||||
*s.m_iA = 4;
|
||||
s.m_iA->warp(true);
|
||||
EXPECT_EQ(endCallbackRan, 5);
|
||||
|
||||
// test getCurveValue
|
||||
*s.m_iA = 0;
|
||||
EXPECT_EQ(s.m_iA->getCurveValue(), 0.f);
|
||||
s.m_iA->warp();
|
||||
EXPECT_EQ(s.m_iA->getCurveValue(), 1.f);
|
||||
EXPECT_EQ(endCallbackRan, 6);
|
||||
|
||||
// test end callback readding the var
|
||||
*s.m_iA = 5;
|
||||
s.m_iA->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> v) {
|
||||
endCallbackRan++;
|
||||
const auto PAV = dc<CAnimatedVariable<int>*>(v.lock().get());
|
||||
|
||||
*PAV = 10;
|
||||
PAV->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> v) { endCallbackRan++; });
|
||||
});
|
||||
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT_EQ(endCallbackRan, 8);
|
||||
EXPECT_EQ(s.m_iA->value(), 10);
|
||||
|
||||
// Test duplicate active anim vars are not allowed
|
||||
{
|
||||
EXPECT_EQ(pAnimationManager->m_vActiveAnimatedVariables.size(), 0);
|
||||
PANIMVAR<int> a;
|
||||
pAnimationManager->createAnimation(1, a, "default");
|
||||
EXPECT_EQ(pAnimationManager->m_vActiveAnimatedVariables.size(), 0);
|
||||
*a = 10;
|
||||
EXPECT_EQ(pAnimationManager->m_vActiveAnimatedVariables.size(), 1);
|
||||
*a = 20;
|
||||
EXPECT_EQ(pAnimationManager->m_vActiveAnimatedVariables.size(), 1);
|
||||
a->warp();
|
||||
EXPECT_EQ(pAnimationManager->m_vActiveAnimatedVariables.size(), 0);
|
||||
EXPECT_EQ(a->value(), 20);
|
||||
}
|
||||
|
||||
// Test no crash when animation manager gets destroyed
|
||||
{
|
||||
PANIMVAR<int> a;
|
||||
pAnimationManager->createAnimation(1, a, "default");
|
||||
*a = 10;
|
||||
pAnimationManager.reset();
|
||||
EXPECT_EQ(a->isAnimationManagerDead(), true);
|
||||
a->setValueAndWarp(11);
|
||||
EXPECT_EQ(a->value(), 11);
|
||||
*a = 12;
|
||||
a->warp();
|
||||
EXPECT_EQ(a->value(), 12);
|
||||
*a = 13;
|
||||
} // a gets destroyed
|
||||
|
||||
EXPECT_EQ(pAnimationManager.get(), nullptr);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -26,20 +26,15 @@ void CBezierCurve::setup4(const std::array<Vector2D, 4>& pVec) {
|
|||
pVec[3],
|
||||
};
|
||||
|
||||
if (m_vPoints.size() != 4)
|
||||
std::abort();
|
||||
|
||||
// bake BAKEDPOINTS points for faster lookups
|
||||
// T -> X ( / BAKEDPOINTS )
|
||||
// Pre-bake curve
|
||||
//
|
||||
// We start baking at t=(i+1)/n not at t=0
|
||||
// That means the first baked x can be > 0 if curve itself starts at x>0
|
||||
for (int i = 0; i < BAKEDPOINTS; ++i) {
|
||||
float const t = (i + 1) / sc<float>(BAKEDPOINTS);
|
||||
// When i=0 -> t=1/255
|
||||
const float t = (i + 1) * INVBAKEDPOINTS;
|
||||
m_aPointsBaked[i] = Vector2D(getXForT(t), getYForT(t));
|
||||
}
|
||||
|
||||
for (int j = 1; j < 10; ++j) {
|
||||
float i = j / 10.0f;
|
||||
getYForPoint(i);
|
||||
}
|
||||
}
|
||||
|
||||
float CBezierCurve::getXForT(float const& t) const {
|
||||
|
|
@ -53,7 +48,7 @@ float CBezierCurve::getYForT(float const& t) const {
|
|||
float t2 = t * t;
|
||||
float t3 = t2 * t;
|
||||
|
||||
return ((1 - t) * (1 - t) * (1 - t) * m_vPoints[0].y) +(3 * t * (1 - t) * (1 - t) * m_vPoints[1].y) + (3 * t2 * (1 - t) * m_vPoints[2].y) + (t3 * m_vPoints[3].y);
|
||||
return ((1 - t) * (1 - t) * (1 - t) * m_vPoints[0].y) + (3 * t * (1 - t) * (1 - t) * m_vPoints[1].y) + (3 * t2 * (1 - t) * m_vPoints[2].y) + (t3 * m_vPoints[3].y);
|
||||
}
|
||||
|
||||
// Todo: this probably can be done better and faster
|
||||
|
|
@ -71,23 +66,123 @@ float CBezierCurve::getYForPoint(float const& x) const {
|
|||
else
|
||||
index -= step;
|
||||
|
||||
// Clamp to avoid index walking off
|
||||
if (index < 0)
|
||||
index = 0;
|
||||
else if (index > BAKEDPOINTS - 1)
|
||||
index = BAKEDPOINTS - 1;
|
||||
|
||||
below = m_aPointsBaked[index].x < x;
|
||||
}
|
||||
|
||||
int lowerIndex = index - (!below || index == BAKEDPOINTS - 1);
|
||||
|
||||
// in the name of performance i shall make a hack
|
||||
const auto LOWERPOINT = &m_aPointsBaked[lowerIndex];
|
||||
const auto UPPERPOINT = &m_aPointsBaked[lowerIndex + 1];
|
||||
// Clamp final indices
|
||||
if (lowerIndex < 0)
|
||||
lowerIndex = 0;
|
||||
else if (lowerIndex > BAKEDPOINTS - 2)
|
||||
lowerIndex = BAKEDPOINTS - 2;
|
||||
|
||||
const auto PERCINDELTA = (x - LOWERPOINT->x) / (UPPERPOINT->x - LOWERPOINT->x);
|
||||
// In the name of performance I shall make a hack
|
||||
const auto& LOWERPOINT = m_aPointsBaked[lowerIndex];
|
||||
const auto& UPPERPOINT = m_aPointsBaked[lowerIndex + 1];
|
||||
|
||||
if (std::isnan(PERCINDELTA) || std::isinf(PERCINDELTA)) // can sometimes happen for VERY small x
|
||||
return 0.f;
|
||||
const float dx = (UPPERPOINT.x - LOWERPOINT.x);
|
||||
// If two baked points have almost the same x
|
||||
// just return the lower one
|
||||
if (dx <= 1e-6f)
|
||||
return LOWERPOINT.y;
|
||||
|
||||
return LOWERPOINT->y + ((UPPERPOINT->y - LOWERPOINT->y) * PERCINDELTA);
|
||||
const auto PERCINDELTA = (x - LOWERPOINT.x) / dx;
|
||||
|
||||
// Can sometimes happen for VERY small x
|
||||
if (std::isnan(PERCINDELTA) || std::isinf(PERCINDELTA))
|
||||
return LOWERPOINT.y;
|
||||
|
||||
return LOWERPOINT.y + ((UPPERPOINT.y - LOWERPOINT.y) * PERCINDELTA);
|
||||
}
|
||||
|
||||
const std::vector<Hyprutils::Math::Vector2D>& CBezierCurve::getControlPoints() const {
|
||||
return m_vPoints;
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using Hyprutils::Animation::CBezierCurve;
|
||||
using Hyprutils::Math::Vector2D;
|
||||
|
||||
static void test_nonmonotonic4_clamps_out_of_range() {
|
||||
// Non-monotonic curve in X
|
||||
// This used to drive the step-halving search to OOB. It should now clamp
|
||||
CBezierCurve curve;
|
||||
std::array<Vector2D, 4> pts = {
|
||||
Vector2D{0.5f, 1.0f}, // P0
|
||||
Vector2D{1.0f, 1.0f}, // P1
|
||||
Vector2D{0.0f, 0.0f}, // P2
|
||||
Vector2D{0.5f, 0.0f} // P3
|
||||
};
|
||||
curve.setup4(pts);
|
||||
|
||||
// x > last baked x
|
||||
EXPECT_EQ(std::isfinite(curve.getYForPoint(0.6f)), true);
|
||||
// Far beyond range
|
||||
EXPECT_EQ(std::isfinite(curve.getYForPoint(std::numeric_limits<float>::max())), true);
|
||||
EXPECT_EQ(std::isfinite(curve.getYForPoint(-std::numeric_limits<float>::max())), true);
|
||||
}
|
||||
|
||||
static void test_adjacent_baked_x_equal() {
|
||||
// Curve with flat tail (X=1, Y=1)
|
||||
CBezierCurve curve;
|
||||
std::array<Vector2D, 4> pts = {
|
||||
Vector2D{0.0f, 0.0f}, // P0
|
||||
Vector2D{0.2f, 0.2f}, // P1
|
||||
Vector2D{1.0f, 1.0f}, // P2
|
||||
Vector2D{1.0f, 1.0f} // P3
|
||||
};
|
||||
curve.setup4(pts);
|
||||
|
||||
// Exactly at last baked X
|
||||
const float y_at_end = curve.getYForPoint(1.0f);
|
||||
// Slightly beyond last baked X
|
||||
const float y_past_end = curve.getYForPoint(1.0001f);
|
||||
|
||||
EXPECT_EQ(y_at_end, 1.0f);
|
||||
EXPECT_EQ(y_past_end, y_at_end);
|
||||
}
|
||||
|
||||
static void test_all_baked_x_equal() {
|
||||
// Extreme case: X is constant along the whole curve
|
||||
CBezierCurve curve;
|
||||
std::array<Vector2D, 4> pts = {
|
||||
Vector2D{0.0f, 0.0f}, // P0
|
||||
Vector2D{0.0f, 0.3f}, // P1
|
||||
Vector2D{0.0f, 0.7f}, // P2
|
||||
Vector2D{0.0f, 1.0f} // P3
|
||||
};
|
||||
curve.setup4(pts);
|
||||
|
||||
// Below any baked X
|
||||
const float y_lo = curve.getYForPoint(-100.0f);
|
||||
const float y_0 = curve.getYForPoint(0.0f);
|
||||
// Above any baked X
|
||||
const float y_hi = curve.getYForPoint(100.0f);
|
||||
|
||||
EXPECT_EQ(std::isfinite(y_lo), true);
|
||||
EXPECT_EQ(std::isfinite(y_0), true);
|
||||
EXPECT_EQ(std::isfinite(y_hi), true);
|
||||
|
||||
// For this curve Y should stay within [0,1]
|
||||
EXPECT_EQ((y_lo >= 0.0f && y_lo <= 1.0f), true);
|
||||
EXPECT_EQ((y_0 >= 0.0f && y_0 <= 1.0f), true);
|
||||
EXPECT_EQ((y_hi >= 0.0f && y_hi <= 1.0f), true);
|
||||
}
|
||||
|
||||
TEST(Animation, beziercurve) {
|
||||
test_nonmonotonic4_clamps_out_of_range();
|
||||
test_adjacent_baked_x_equal();
|
||||
test_all_baked_x_equal();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,174 @@
|
|||
#include "I18nEngine.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
#include <locale>
|
||||
#include <hyprutils/string/String.hpp>
|
||||
|
||||
using namespace Hyprutils::I18n;
|
||||
using namespace Hyprutils;
|
||||
using namespace Hyprutils::String;
|
||||
|
||||
CI18nEngine::CI18nEngine() : m_impl(Memory::makeUnique<SI18nEngineImpl>()) {
|
||||
;
|
||||
}
|
||||
CI18nEngine::~CI18nEngine() = default;
|
||||
|
||||
void CI18nEngine::registerEntry(const std::string& locale, uint64_t key, std::string&& translationUTF8) {
|
||||
auto& entryVec = m_impl->entries[locale];
|
||||
|
||||
if (entryVec.size() <= key)
|
||||
entryVec.resize(key + 1);
|
||||
|
||||
entryVec[key].entry = std::move(translationUTF8);
|
||||
entryVec[key].exists = true;
|
||||
}
|
||||
|
||||
void CI18nEngine::registerEntry(const std::string& locale, uint64_t key, translationFn&& translationFn) {
|
||||
auto& entryVec = m_impl->entries[locale];
|
||||
|
||||
if (entryVec.size() <= key)
|
||||
entryVec.resize(key + 1);
|
||||
|
||||
entryVec[key].fn = std::move(translationFn);
|
||||
entryVec[key].exists = true;
|
||||
}
|
||||
|
||||
void CI18nEngine::setFallbackLocale(const std::string& locale) {
|
||||
m_impl->fallbackLocale = locale;
|
||||
}
|
||||
|
||||
std::string CI18nEngine::localizeEntry(const std::string& locale, uint64_t key, const translationVarMap& map) {
|
||||
SI18nTranslationEntry* entry = nullptr;
|
||||
|
||||
if (m_impl->entries.contains(locale) && m_impl->entries[locale].size() > key)
|
||||
entry = &m_impl->entries[locale][key];
|
||||
|
||||
if (locale.contains('_')) {
|
||||
|
||||
if (!entry || !entry->exists) {
|
||||
// try to fall back to lang_LANG
|
||||
auto stem = locale.substr(0, locale.find('_'));
|
||||
auto stemUpper = stem;
|
||||
std::ranges::transform(stemUpper, stemUpper.begin(), ::toupper);
|
||||
auto newLocale = std::format("{}_{}", stem, stemUpper);
|
||||
if (m_impl->entries.contains(newLocale) && m_impl->entries[newLocale].size() > key)
|
||||
entry = &m_impl->entries[newLocale][key];
|
||||
}
|
||||
|
||||
if (!entry || !entry->exists) {
|
||||
// try to fall back to any lang prefixed with our prefix
|
||||
|
||||
auto stem = locale.substr(0, locale.find('_') + 1);
|
||||
for (const auto& [k, v] : m_impl->entries) {
|
||||
if (k.starts_with(stem) || k == stem) {
|
||||
if (m_impl->entries.contains(k) && m_impl->entries[k].size() > key)
|
||||
entry = &m_impl->entries[k][key];
|
||||
|
||||
if (entry && entry->exists)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// locale doesn't have a _, e.g. pl
|
||||
// find any locale that has the same stem
|
||||
for (const auto& [k, v] : m_impl->entries) {
|
||||
if (k.starts_with(locale + "_") || k == locale) {
|
||||
if (m_impl->entries.contains(k) && m_impl->entries[k].size() > key)
|
||||
entry = &m_impl->entries[k][key];
|
||||
|
||||
if (entry && entry->exists)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!entry || !entry->exists) {
|
||||
// fall back to general fallback
|
||||
if (m_impl->entries.contains(m_impl->fallbackLocale) && m_impl->entries[m_impl->fallbackLocale].size() > key)
|
||||
entry = &m_impl->entries[m_impl->fallbackLocale][key];
|
||||
}
|
||||
|
||||
if (!entry || !entry->exists)
|
||||
return "";
|
||||
|
||||
std::string rawStr = entry->entry;
|
||||
|
||||
if (entry->fn)
|
||||
rawStr = entry->fn(map);
|
||||
|
||||
for (const auto& e : map) {
|
||||
replaceInString(rawStr, "{" + e.first + "}", e.second);
|
||||
}
|
||||
|
||||
return rawStr;
|
||||
}
|
||||
|
||||
CI18nLocale CI18nEngine::getSystemLocale() {
|
||||
try {
|
||||
return CI18nLocale(std::locale("").name());
|
||||
} catch (...) { return CI18nLocale("en_US.UTF-8"); }
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
enum eTxtKeys : uint64_t {
|
||||
TXT_KEY_HELLO,
|
||||
TXT_KEY_I_HAVE_APPLES,
|
||||
TXT_KEY_FALLBACK,
|
||||
};
|
||||
|
||||
TEST(I18n, Engine) {
|
||||
CI18nEngine engine;
|
||||
|
||||
engine.setFallbackLocale("en_US");
|
||||
|
||||
engine.registerEntry("en_US", TXT_KEY_HELLO, "Hello World!");
|
||||
engine.registerEntry("en_US", TXT_KEY_I_HAVE_APPLES, [](const translationVarMap& m) {
|
||||
if (std::stoi(m.at("count")) == 1)
|
||||
return "I have {count} apple.";
|
||||
else
|
||||
return "I have {count} apples.";
|
||||
});
|
||||
engine.registerEntry("en_US", TXT_KEY_FALLBACK, "Fallback string!");
|
||||
|
||||
engine.registerEntry("pl_PL", TXT_KEY_HELLO, "Witaj świecie!");
|
||||
engine.registerEntry("pl_PL", TXT_KEY_I_HAVE_APPLES, [](const translationVarMap& m) {
|
||||
const auto COUNT = std::stoi(m.at("count"));
|
||||
if (COUNT == 1)
|
||||
return "Mam {count} jabłko.";
|
||||
else if (COUNT < 5)
|
||||
return "Mam {count} jabłka.";
|
||||
else
|
||||
return "Mam {count} jabłek.";
|
||||
});
|
||||
|
||||
engine.registerEntry("es_XX", TXT_KEY_FALLBACK, "I don't speak spanish");
|
||||
engine.registerEntry("es_ES", TXT_KEY_FALLBACK, "I don't speak spanish here either");
|
||||
|
||||
EXPECT_EQ(engine.localizeEntry("en_US", TXT_KEY_HELLO, {}), "Hello World!");
|
||||
EXPECT_EQ(engine.localizeEntry("pl_PL", TXT_KEY_HELLO, {}), "Witaj świecie!");
|
||||
EXPECT_EQ(engine.localizeEntry("de_DE", TXT_KEY_HELLO, {}), "Hello World!");
|
||||
|
||||
EXPECT_EQ(engine.localizeEntry("en_US", TXT_KEY_I_HAVE_APPLES, {{"count", "1"}}), "I have 1 apple.");
|
||||
EXPECT_EQ(engine.localizeEntry("en_US", TXT_KEY_I_HAVE_APPLES, {{"count", "2"}}), "I have 2 apples.");
|
||||
|
||||
EXPECT_EQ(engine.localizeEntry("pl_PL", TXT_KEY_I_HAVE_APPLES, {{"count", "1"}}), "Mam 1 jabłko.");
|
||||
EXPECT_EQ(engine.localizeEntry("pl_PL", TXT_KEY_I_HAVE_APPLES, {{"count", "2"}}), "Mam 2 jabłka.");
|
||||
EXPECT_EQ(engine.localizeEntry("pl_PL", TXT_KEY_I_HAVE_APPLES, {{"count", "5"}}), "Mam 5 jabłek.");
|
||||
|
||||
EXPECT_EQ(engine.localizeEntry("pl", TXT_KEY_I_HAVE_APPLES, {{"count", "5"}}), "Mam 5 jabłek.");
|
||||
|
||||
EXPECT_EQ(engine.localizeEntry("pl_XX", TXT_KEY_I_HAVE_APPLES, {{"count", "5"}}), "Mam 5 jabłek.");
|
||||
EXPECT_EQ(engine.localizeEntry("en_XX", TXT_KEY_I_HAVE_APPLES, {{"count", "2"}}), "I have 2 apples.");
|
||||
|
||||
EXPECT_EQ(engine.localizeEntry("es_YY", TXT_KEY_FALLBACK, {}), "I don't speak spanish here either");
|
||||
EXPECT_EQ(engine.localizeEntry("es_XX", TXT_KEY_FALLBACK, {}), "I don't speak spanish");
|
||||
|
||||
EXPECT_EQ(engine.localizeEntry("pl_PL", TXT_KEY_FALLBACK, {}), "Fallback string!");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include <hyprutils/i18n/I18nEngine.hpp>
|
||||
|
||||
namespace Hyprutils::I18n {
|
||||
struct SI18nTranslationEntry {
|
||||
bool exists = false;
|
||||
std::string entry = "";
|
||||
translationFn fn = nullptr;
|
||||
};
|
||||
|
||||
struct SI18nEngineImpl {
|
||||
std::unordered_map<std::string, std::vector<SI18nTranslationEntry>> entries;
|
||||
std::string fallbackLocale = "en_US";
|
||||
};
|
||||
};
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
#include <hyprutils/i18n/I18nEngine.hpp>
|
||||
|
||||
using namespace Hyprutils::I18n;
|
||||
|
||||
static std::string extractLocale(std::string locale) {
|
||||
// localeStr is very arbitrary... from my testing, it can be:
|
||||
// en_US.UTF-8
|
||||
// LC_CTYPE=en_US
|
||||
// POSIX
|
||||
// *
|
||||
//
|
||||
// We only return e.g. en_US or pl_PL, or pl
|
||||
|
||||
if (locale == "POSIX")
|
||||
return "en_US";
|
||||
if (locale == "*")
|
||||
return "en_US";
|
||||
|
||||
if (locale.contains('='))
|
||||
locale = locale.substr(locale.find('=') + 1);
|
||||
|
||||
if (locale.contains('.'))
|
||||
locale = locale.substr(0, locale.find('.'));
|
||||
|
||||
return locale;
|
||||
}
|
||||
|
||||
CI18nLocale::CI18nLocale(std::string fullLocale) : m_rawFullLocale(std::move(fullLocale)) {
|
||||
m_locale = extractLocale(m_rawFullLocale);
|
||||
}
|
||||
|
||||
std::string CI18nLocale::locale() {
|
||||
return m_locale;
|
||||
}
|
||||
|
||||
std::string CI18nLocale::stem() {
|
||||
if (m_locale.contains('_'))
|
||||
return m_locale.substr(0, m_locale.find('_'));
|
||||
return m_locale;
|
||||
}
|
||||
|
||||
std::string CI18nLocale::full() {
|
||||
return m_rawFullLocale;
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <hyprutils/i18n/I18nEngine.hpp>
|
||||
|
||||
TEST(I18n, Locale) {
|
||||
CI18nEngine engine;
|
||||
|
||||
EXPECT_EQ(extractLocale("pl_PL.UTF-8"), "pl_PL");
|
||||
EXPECT_EQ(extractLocale("POSIX"), "en_US");
|
||||
EXPECT_EQ(extractLocale("*"), "en_US");
|
||||
EXPECT_EQ(extractLocale("LC_CTYPE=pl_PL.UTF-8"), "pl_PL");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -235,3 +235,78 @@ Vector2D Hyprutils::Math::CBox::closestPoint(const Vector2D& vec) const {
|
|||
SBoxExtents Hyprutils::Math::CBox::extentsFrom(const CBox& small) {
|
||||
return {.topLeft = {small.x - x, small.y - y}, .bottomRight = {w - small.w - (small.x - x), h - small.h - (small.y - y)}};
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Math, box) {
|
||||
// Test default constructor and accessors
|
||||
{
|
||||
CBox box1;
|
||||
EXPECT_EQ(box1.x, 0);
|
||||
EXPECT_EQ(box1.y, 0);
|
||||
EXPECT_EQ(box1.width, 0);
|
||||
EXPECT_EQ(box1.height, 0);
|
||||
|
||||
// Test parameterized constructor and accessors
|
||||
CBox box2(10, 20, 30, 40);
|
||||
EXPECT_EQ(box2.x, 10);
|
||||
EXPECT_EQ(box2.y, 20);
|
||||
EXPECT_EQ(box2.width, 30);
|
||||
EXPECT_EQ(box2.height, 40);
|
||||
|
||||
// Test setters and getters
|
||||
box2.translate(Vector2D(5, -5));
|
||||
EXPECT_EQ(box2.pos(), Vector2D(15, 15));
|
||||
}
|
||||
|
||||
//Test Scaling and Transformation
|
||||
{
|
||||
CBox box(10, 10, 20, 30);
|
||||
|
||||
// Test scaling
|
||||
box.scale(2.0);
|
||||
EXPECT_EQ(box.size(), Vector2D(40, 60));
|
||||
EXPECT_EQ(box.pos(), Vector2D(20, 20));
|
||||
|
||||
// Test scaling from center
|
||||
box.scaleFromCenter(0.5);
|
||||
EXPECT_EQ(box.size(), Vector2D(20, 30));
|
||||
EXPECT_EQ(box.pos(), Vector2D(30, 35));
|
||||
|
||||
// Test transformation
|
||||
box.transform(HYPRUTILS_TRANSFORM_90, 100, 200);
|
||||
EXPECT_EQ(box.pos(), Vector2D(135, 30));
|
||||
EXPECT_EQ(box.size(), Vector2D(30, 20));
|
||||
|
||||
// Test Intersection and Extents
|
||||
}
|
||||
|
||||
{
|
||||
CBox box1(0, 0, 100, 100);
|
||||
CBox box2(50, 50, 100, 100);
|
||||
|
||||
CBox intersection = box1.intersection(box2);
|
||||
EXPECT_EQ(intersection.pos(), Vector2D(50, 50));
|
||||
EXPECT_EQ(intersection.size(), Vector2D(50, 50));
|
||||
|
||||
SBoxExtents extents = box1.extentsFrom(box2);
|
||||
EXPECT_EQ(extents.topLeft, Vector2D(50, 50));
|
||||
EXPECT_EQ(extents.bottomRight, Vector2D(-50, -50));
|
||||
}
|
||||
|
||||
// Test Boundary Conditions and Special Cases
|
||||
{
|
||||
CBox box(0, 0, 50, 50);
|
||||
|
||||
EXPECT_EQ(box.empty(), false);
|
||||
|
||||
EXPECT_EQ(box.containsPoint(Vector2D(25, 25)), true);
|
||||
EXPECT_EQ(box.containsPoint(Vector2D(60, 60)), false);
|
||||
EXPECT_EQ(box.overlaps(CBox(25, 25, 50, 50)), true);
|
||||
EXPECT_EQ(box.inside(CBox(0, 0, 100, 100)), false);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -153,3 +153,26 @@ std::string Mat3x3::toString() const {
|
|||
return std::format("[mat3x3: {}, {}, {}, {}, {}, {}, {}, {}, {}]", matrix.at(0), matrix.at(1), matrix.at(2), matrix.at(3), matrix.at(4), matrix.at(5), matrix.at(6),
|
||||
matrix.at(7), matrix.at(8));
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Math, mat3x3) {
|
||||
Mat3x3 jeremy = Mat3x3::outputProjection({1920, 1080}, HYPRUTILS_TRANSFORM_FLIPPED_90);
|
||||
Mat3x3 matrixBox = jeremy.projectBox(CBox{10, 10, 200, 200}, HYPRUTILS_TRANSFORM_NORMAL).translate({100, 100}).scale({1.25F, 1.5F}).transpose();
|
||||
|
||||
Mat3x3 expected = std::array<float, 9>{0, 0.46296296, 0, 0.3125, 0, 0, 19.84375, 36.055557, 1};
|
||||
// we need to do this to avoid precision errors on 32-bit archs
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(0) - matrixBox.getMatrix().at(0)) < 0.1, true);
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(1) - matrixBox.getMatrix().at(1)) < 0.1, true);
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(2) - matrixBox.getMatrix().at(2)) < 0.1, true);
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(3) - matrixBox.getMatrix().at(3)) < 0.1, true);
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(4) - matrixBox.getMatrix().at(4)) < 0.1, true);
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(5) - matrixBox.getMatrix().at(5)) < 0.1, true);
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(6) - matrixBox.getMatrix().at(6)) < 0.1, true);
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(7) - matrixBox.getMatrix().at(7)) < 0.1, true);
|
||||
EXPECT_EQ(std::abs(expected.getMatrix().at(8) - matrixBox.getMatrix().at(8)) < 0.1, true);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -220,3 +220,22 @@ Vector2D Hyprutils::Math::CRegion::closestPoint(const Vector2D& vec) const {
|
|||
|
||||
return leader;
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Math, region) {
|
||||
CRegion rg(CBox{{20, 20}, {40, 40}});
|
||||
|
||||
auto extents = rg.getExtents();
|
||||
EXPECT_EQ(extents.pos(), Vector2D(20, 20));
|
||||
EXPECT_EQ(extents.size(), Vector2D(40, 40));
|
||||
|
||||
rg.scale(2);
|
||||
extents = rg.getExtents();
|
||||
EXPECT_EQ(extents.pos(), Vector2D(40, 40));
|
||||
EXPECT_EQ(extents.size(), Vector2D(80, 80));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -57,3 +57,23 @@ Vector2D Hyprutils::Math::Vector2D::transform(eTransform transform, const Vector
|
|||
default: return *this;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Math, vector2d) {
|
||||
Vector2D original(30, 40);
|
||||
Vector2D monitorSize(100, 200);
|
||||
|
||||
EXPECT_EQ(original.transform(HYPRUTILS_TRANSFORM_NORMAL, monitorSize), Vector2D(30, 40));
|
||||
EXPECT_EQ(original.transform(HYPRUTILS_TRANSFORM_90, monitorSize), Vector2D(40, 200 - 30));
|
||||
EXPECT_EQ(original.transform(HYPRUTILS_TRANSFORM_180, monitorSize), Vector2D(100 - 30, 200 - 40));
|
||||
EXPECT_EQ(original.transform(HYPRUTILS_TRANSFORM_270, monitorSize), Vector2D(100 - 40, 30));
|
||||
EXPECT_EQ(original.transform(HYPRUTILS_TRANSFORM_FLIPPED, monitorSize), Vector2D(100 - 30, 40));
|
||||
EXPECT_EQ(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_90, monitorSize), Vector2D(40, 30));
|
||||
EXPECT_EQ(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_180, monitorSize), Vector2D(30, 200 - 40));
|
||||
EXPECT_EQ(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_270, monitorSize), Vector2D(100 - 40, 200 - 30));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
#include <hyprutils/memory/UniquePtr.hpp>
|
||||
#include <hyprutils/memory/SharedPtr.hpp>
|
||||
#include <hyprutils/memory/WeakPtr.hpp>
|
||||
#include <hyprutils/memory/Atomic.hpp>
|
||||
#include "shared.hpp"
|
||||
#include <chrono>
|
||||
#include <print>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <hyprutils/memory/Casts.hpp>
|
||||
|
||||
using namespace Hyprutils::Memory;
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#define SP CSharedPointer
|
||||
#define WP CWeakPointer
|
||||
#define UP CUniquePointer
|
||||
|
|
@ -19,9 +17,7 @@ using namespace Hyprutils::Memory;
|
|||
#define NTHREADS 8
|
||||
#define ITERATIONS 10000
|
||||
|
||||
static int testAtomicImpl() {
|
||||
int ret = 0;
|
||||
|
||||
static void testAtomicImpl() {
|
||||
{
|
||||
// Using makeShared here could lead to invalid refcounts.
|
||||
ASP<int> shared = makeAtomicShared<int>(0);
|
||||
|
|
@ -45,7 +41,7 @@ static int testAtomicImpl() {
|
|||
// Actual count is not incremented in a thread-safe manner here, so we can't check it.
|
||||
// We just want to check that the concurent refcounting doesn't cause any memory corruption.
|
||||
shared.reset();
|
||||
EXPECT(shared, false);
|
||||
EXPECT_EQ(shared, false);
|
||||
}
|
||||
|
||||
{
|
||||
|
|
@ -71,15 +67,15 @@ static int testAtomicImpl() {
|
|||
thread.join();
|
||||
}
|
||||
|
||||
EXPECT(shared.strongRef(), 0);
|
||||
EXPECT(weak.valid(), false);
|
||||
EXPECT_EQ(shared.strongRef(), 0);
|
||||
EXPECT_EQ(weak.valid(), false);
|
||||
|
||||
auto shared2 = weak.lock();
|
||||
EXPECT(shared, false);
|
||||
EXPECT(shared2.get(), nullptr);
|
||||
EXPECT(shared.strongRef(), 0);
|
||||
EXPECT(weak.valid(), false);
|
||||
EXPECT(weak.expired(), true);
|
||||
EXPECT_EQ(shared, false);
|
||||
EXPECT_EQ(shared2.get(), nullptr);
|
||||
EXPECT_EQ(shared.strongRef(), 0);
|
||||
EXPECT_EQ(weak.valid(), false);
|
||||
EXPECT_EQ(weak.expired(), true);
|
||||
}
|
||||
|
||||
{ // This tests recursive deletion. When foo will be deleted, bar will be deleted within the foo dtor.
|
||||
|
|
@ -120,38 +116,34 @@ static int testAtomicImpl() {
|
|||
genericNormal = derivedNormal;
|
||||
}
|
||||
|
||||
EXPECT(!!genericAtomic, true);
|
||||
EXPECT(!!genericNormal, true);
|
||||
EXPECT_EQ(!!genericAtomic, true);
|
||||
EXPECT_EQ(!!genericNormal, true);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
TEST(Memory, memory) {
|
||||
SP<int> intPtr = makeShared<int>(10);
|
||||
SP<int> intPtr2 = makeShared<int>(-1337);
|
||||
UP<int> intUnique = makeUnique<int>(420);
|
||||
|
||||
int ret = 0;
|
||||
|
||||
EXPECT(*intPtr, 10);
|
||||
EXPECT(intPtr.strongRef(), 1);
|
||||
EXPECT(*intUnique, 420);
|
||||
EXPECT_EQ(*intPtr, 10);
|
||||
EXPECT_EQ(intPtr.strongRef(), 1);
|
||||
EXPECT_EQ(*intUnique, 420);
|
||||
|
||||
WP<int> weak = intPtr;
|
||||
WP<int> weakUnique = intUnique;
|
||||
|
||||
EXPECT(*intPtr, 10);
|
||||
EXPECT(intPtr.strongRef(), 1);
|
||||
EXPECT(*weak, 10);
|
||||
EXPECT(weak.expired(), false);
|
||||
EXPECT(!!weak.lock(), true);
|
||||
EXPECT(*weakUnique, 420);
|
||||
EXPECT(weakUnique.expired(), false);
|
||||
EXPECT(intUnique.impl_->wref(), 1);
|
||||
EXPECT_EQ(*intPtr, 10);
|
||||
EXPECT_EQ(intPtr.strongRef(), 1);
|
||||
EXPECT_EQ(*weak, 10);
|
||||
EXPECT_EQ(weak.expired(), false);
|
||||
EXPECT_EQ(!!weak.lock(), true);
|
||||
EXPECT_EQ(*weakUnique, 420);
|
||||
EXPECT_EQ(weakUnique.expired(), false);
|
||||
EXPECT_EQ(intUnique.impl_->wref(), 1);
|
||||
|
||||
SP<int> sharedFromUnique = weakUnique.lock();
|
||||
EXPECT(sharedFromUnique, nullptr);
|
||||
EXPECT_EQ(sharedFromUnique, nullptr);
|
||||
|
||||
std::vector<SP<int>> sps;
|
||||
sps.push_back(intPtr);
|
||||
|
|
@ -163,24 +155,25 @@ int main(int argc, char** argv, char** envp) {
|
|||
intPtr.reset();
|
||||
intUnique.reset();
|
||||
|
||||
EXPECT(weak.impl_->ref(), 0);
|
||||
EXPECT(weakUnique.impl_->ref(), 0);
|
||||
EXPECT(weakUnique.impl_->wref(), 1);
|
||||
EXPECT(intPtr2.strongRef(), 3);
|
||||
EXPECT_EQ(weak.impl_->ref(), 0);
|
||||
EXPECT_EQ(weakUnique.impl_->ref(), 0);
|
||||
EXPECT_EQ(weakUnique.impl_->wref(), 1);
|
||||
EXPECT_EQ(intPtr2.strongRef(), 3);
|
||||
|
||||
EXPECT(weak.expired(), true);
|
||||
EXPECT(weakUnique.expired(), true);
|
||||
EXPECT_EQ(weak.expired(), true);
|
||||
EXPECT_EQ(weakUnique.expired(), true);
|
||||
|
||||
auto intPtr2AsUint = reinterpretPointerCast<unsigned int>(intPtr2);
|
||||
EXPECT(intPtr2.strongRef(), 4);
|
||||
EXPECT(intPtr2AsUint.strongRef(), 4);
|
||||
EXPECT_EQ(intPtr2.strongRef(), 4);
|
||||
EXPECT_EQ(intPtr2AsUint.strongRef(), 4);
|
||||
|
||||
EXPECT(*intPtr2AsUint > 0, true);
|
||||
EXPECT(*intPtr2AsUint, (unsigned int)(int)-1337);
|
||||
EXPECT_EQ(*intPtr2AsUint > 0, true);
|
||||
EXPECT_EQ(*intPtr2AsUint, (unsigned int)(int)-1337);
|
||||
*intPtr2AsUint = 10;
|
||||
EXPECT(*intPtr2AsUint, 10);
|
||||
EXPECT(*intPtr2, 10);
|
||||
EXPECT_EQ(*intPtr2AsUint, 10);
|
||||
EXPECT_EQ(*intPtr2, 10);
|
||||
|
||||
EXPECT(testAtomicImpl(), 0);
|
||||
return ret;
|
||||
testAtomicImpl();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -84,3 +84,49 @@ bool CFileDescriptor::isReadable(int fd) {
|
|||
|
||||
return poll(&pfd, 1, 0) > 0 && (pfd.revents & POLLIN);
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
TEST(OS, fd) {
|
||||
std::string name = "/test_filedescriptors";
|
||||
CFileDescriptor fd(shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600));
|
||||
|
||||
EXPECT_EQ(fd.isValid(), true);
|
||||
EXPECT_EQ(fd.isReadable(), true);
|
||||
|
||||
int flags = fd.getFlags();
|
||||
EXPECT_EQ(fd.getFlags(), FD_CLOEXEC);
|
||||
flags &= ~FD_CLOEXEC;
|
||||
fd.setFlags(flags);
|
||||
EXPECT_EQ(fd.getFlags(), !FD_CLOEXEC);
|
||||
|
||||
CFileDescriptor fd2 = fd.duplicate();
|
||||
EXPECT_EQ(fd.isValid(), true);
|
||||
EXPECT_EQ(fd.isReadable(), true);
|
||||
EXPECT_EQ(fd2.isValid(), true);
|
||||
EXPECT_EQ(fd2.isReadable(), true);
|
||||
|
||||
CFileDescriptor fd3(fd2.take());
|
||||
EXPECT_EQ(fd.isValid(), true);
|
||||
EXPECT_EQ(fd.isReadable(), true);
|
||||
EXPECT_EQ(fd2.isValid(), false);
|
||||
EXPECT_EQ(fd2.isReadable(), false);
|
||||
|
||||
// .duplicate default flags is FD_CLOEXEC
|
||||
EXPECT_EQ(fd3.getFlags(), FD_CLOEXEC);
|
||||
|
||||
fd.reset();
|
||||
fd2.reset();
|
||||
fd3.reset();
|
||||
|
||||
EXPECT_EQ(fd.isReadable(), false);
|
||||
EXPECT_EQ(fd2.isReadable(), false);
|
||||
EXPECT_EQ(fd3.isReadable(), false);
|
||||
|
||||
shm_unlink(name.c_str());
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -283,3 +283,32 @@ void Hyprutils::OS::CProcess::setStdoutFD(int fd) {
|
|||
void Hyprutils::OS::CProcess::setStderrFD(int fd) {
|
||||
m_impl->stderrFD = fd;
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(OS, process) {
|
||||
CProcess process("sh", {"-c", "echo \"Hello $WORLD!\""});
|
||||
process.addEnv("WORLD", "World");
|
||||
|
||||
EXPECT_EQ(process.runAsync(), true);
|
||||
EXPECT_EQ(process.runSync(), true);
|
||||
|
||||
EXPECT_EQ(process.stdOut(), std::string{"Hello World!\n"});
|
||||
EXPECT_EQ(process.stdErr(), std::string{""});
|
||||
EXPECT_EQ(process.exitCode(), 0);
|
||||
|
||||
CProcess process2("sh", {"-c", "while true; do sleep 1; done;"});
|
||||
|
||||
EXPECT_EQ(process2.runAsync(), true);
|
||||
EXPECT_EQ(getpgid(process2.pid()) >= 0, true);
|
||||
|
||||
kill(process2.pid(), SIGKILL);
|
||||
|
||||
CProcess process3("sh", {"-c", "cat /geryueruggbuergheruger/reugiheruygyuerghuryeghyer/eruihgyuerguyerghyuerghuyergerguyer/NON_EXISTENT"});
|
||||
EXPECT_EQ(process3.runSync(), true);
|
||||
EXPECT_EQ(process3.exitCode(), 1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -58,3 +58,376 @@ void Hyprutils::Signal::CSignalBase::registerStaticListenerInternal(std::functio
|
|||
void Hyprutils::Signal::CSignal::emit(std::any data) {
|
||||
CSignalT::emit(data);
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <any>
|
||||
#include <hyprutils/signal/Signal.hpp>
|
||||
#include <hyprutils/signal/Listener.hpp>
|
||||
#include <hyprutils/memory/WeakPtr.hpp>
|
||||
#include <memory>
|
||||
|
||||
using namespace Hyprutils::Signal;
|
||||
using namespace Hyprutils::Memory;
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
//
|
||||
|
||||
static void legacy() {
|
||||
CSignal signal;
|
||||
int data = 0;
|
||||
auto listener = signal.registerListener([&]([[maybe_unused]] std::any d) { data = 1; });
|
||||
|
||||
signal.emit();
|
||||
|
||||
EXPECT_EQ(data, 1);
|
||||
|
||||
data = 0;
|
||||
|
||||
listener.reset();
|
||||
|
||||
signal.emit();
|
||||
|
||||
EXPECT_EQ(data, 0);
|
||||
}
|
||||
|
||||
static void legacyListenerEmit() {
|
||||
int data = 0;
|
||||
CSignal signal;
|
||||
auto listener = signal.registerListener([&](std::any d) { data = std::any_cast<int>(d); });
|
||||
|
||||
listener->emit(1); // not a typo
|
||||
EXPECT_EQ(data, 1);
|
||||
}
|
||||
|
||||
static void legacyListeners() {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<> signal0;
|
||||
CSignalT<int> signal1;
|
||||
|
||||
auto listener0 = signal0.registerListener([&](std::any d) { data += 1; });
|
||||
auto listener1 = signal1.registerListener([&](std::any d) { data += std::any_cast<int>(d); });
|
||||
|
||||
signal0.registerStaticListener([&](void* o, std::any d) { data += 10; }, nullptr);
|
||||
signal1.registerStaticListener([&](void* o, std::any d) { data += std::any_cast<int>(d) * 10; }, nullptr);
|
||||
|
||||
signal0.emit();
|
||||
signal1.emit(2);
|
||||
|
||||
EXPECT_EQ(data, 33);
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
//
|
||||
|
||||
static void empty() {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<> signal;
|
||||
auto listener = signal.listen([&] { data = 1; });
|
||||
|
||||
signal.emit();
|
||||
EXPECT_EQ(data, 1);
|
||||
|
||||
data = 0;
|
||||
listener.reset();
|
||||
signal.emit();
|
||||
EXPECT_EQ(data, 0);
|
||||
}
|
||||
|
||||
static void typed() {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<int> signal;
|
||||
auto listener = signal.listen([&](int newData) { data = newData; });
|
||||
|
||||
signal.emit(1);
|
||||
EXPECT_EQ(data, 1);
|
||||
}
|
||||
|
||||
static void ignoreParams() {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<int> signal;
|
||||
auto listener = signal.listen([&] { data += 1; });
|
||||
|
||||
signal.listenStatic([&] { data += 1; });
|
||||
|
||||
signal.emit(2);
|
||||
EXPECT_EQ(data, 2);
|
||||
}
|
||||
|
||||
static void typedMany() {
|
||||
int data1 = 0;
|
||||
int data2 = 0;
|
||||
int data3 = 0;
|
||||
|
||||
CSignalT<int, int, int> signal;
|
||||
auto listener = signal.listen([&](int d1, int d2, int d3) {
|
||||
data1 = d1;
|
||||
data2 = d2;
|
||||
data3 = d3;
|
||||
});
|
||||
|
||||
signal.emit(1, 2, 3);
|
||||
EXPECT_EQ(data1, 1);
|
||||
EXPECT_EQ(data2, 2);
|
||||
EXPECT_EQ(data3, 3);
|
||||
}
|
||||
|
||||
static void ref() {
|
||||
int count = 0;
|
||||
int data = 0;
|
||||
|
||||
CSignalT<int&> signal;
|
||||
auto l1 = signal.listen([&](int& v) { v += 1; });
|
||||
auto l2 = signal.listen([&](int v) { count += v; });
|
||||
signal.emit(data);
|
||||
|
||||
CSignalT<const int&> constSignal;
|
||||
auto l3 = constSignal.listen([&](const int& v) { count += v; });
|
||||
auto l4 = constSignal.listen([&](int v) { count += v; });
|
||||
constSignal.emit(data);
|
||||
|
||||
EXPECT_EQ(data, 1);
|
||||
EXPECT_EQ(count, 3);
|
||||
}
|
||||
|
||||
static void refMany() {
|
||||
int count = 0;
|
||||
int data1 = 0;
|
||||
int data2 = 10;
|
||||
|
||||
CSignalT<int&, const int&> signal;
|
||||
auto l1 = signal.listen([&](int& v, const int&) { v += 1; });
|
||||
auto l2 = signal.listen([&](int v1, int v2) { count += v1 + v2; });
|
||||
|
||||
signal.emit(data1, data2);
|
||||
EXPECT_EQ(data1, 1);
|
||||
EXPECT_EQ(count, 11);
|
||||
}
|
||||
|
||||
static void autoRefTypes() {
|
||||
class CCopyCounter {
|
||||
public:
|
||||
CCopyCounter(int& createCount, int& destroyCount) : createCount(createCount), destroyCount(destroyCount) {
|
||||
createCount += 1;
|
||||
}
|
||||
|
||||
CCopyCounter(CCopyCounter&& other) noexcept : CCopyCounter(other.createCount, other.destroyCount) {}
|
||||
CCopyCounter(const CCopyCounter& other) noexcept : CCopyCounter(other.createCount, other.destroyCount) {}
|
||||
|
||||
~CCopyCounter() {
|
||||
destroyCount += 1;
|
||||
}
|
||||
|
||||
private:
|
||||
int& createCount;
|
||||
int& destroyCount;
|
||||
};
|
||||
|
||||
auto createCount = 0;
|
||||
auto destroyCount = 0;
|
||||
|
||||
CSignalT<CCopyCounter> signal;
|
||||
auto listener = signal.listen([](const CCopyCounter& counter) {});
|
||||
|
||||
signal.emit(CCopyCounter(createCount, destroyCount));
|
||||
EXPECT_EQ(createCount, 1);
|
||||
EXPECT_EQ(destroyCount, 1);
|
||||
}
|
||||
|
||||
static void forward() {
|
||||
int count = 0;
|
||||
|
||||
CSignalT<int> sig;
|
||||
CSignalT<int> connected1;
|
||||
CSignalT<> connected2;
|
||||
|
||||
auto conn1 = sig.forward(connected1);
|
||||
auto conn2 = sig.forward(connected2);
|
||||
|
||||
auto listener1 = connected1.listen([&](int v) { count += v; });
|
||||
auto listener2 = connected2.listen([&] { count += 1; });
|
||||
|
||||
sig.emit(2);
|
||||
|
||||
EXPECT_EQ(count, 3);
|
||||
}
|
||||
|
||||
static void listenerAdded() {
|
||||
int count = 0;
|
||||
|
||||
CSignalT<> signal;
|
||||
CHyprSignalListener secondListener;
|
||||
|
||||
auto listener = signal.listen([&] {
|
||||
count += 1;
|
||||
|
||||
if (!secondListener)
|
||||
secondListener = signal.listen([&] { count += 1; });
|
||||
});
|
||||
|
||||
signal.emit();
|
||||
EXPECT_EQ(count, 1); // second should NOT be invoked as it was registed during emit
|
||||
|
||||
signal.emit();
|
||||
EXPECT_EQ(count, 3); // second should be invoked
|
||||
}
|
||||
|
||||
static void lastListenerSwapped() {
|
||||
int count = 0;
|
||||
|
||||
CSignalT<> signal;
|
||||
CHyprSignalListener removedListener;
|
||||
CHyprSignalListener addedListener;
|
||||
|
||||
auto firstListener = signal.listen([&] {
|
||||
removedListener.reset(); // dropped and should NOT be invoked
|
||||
|
||||
if (!addedListener)
|
||||
addedListener = signal.listen([&] { count += 2; });
|
||||
});
|
||||
|
||||
removedListener = signal.listen([&] { count += 1; });
|
||||
|
||||
signal.emit();
|
||||
EXPECT_EQ(count, 0); // neither the removed nor added listeners should fire
|
||||
|
||||
signal.emit();
|
||||
EXPECT_EQ(count, 2); // only the new listener should fire
|
||||
}
|
||||
|
||||
static void signalDestroyed() {
|
||||
int count = 0;
|
||||
|
||||
auto signal = std::make_unique<CSignalT<>>();
|
||||
|
||||
// This ensures a destructor of a listener called before signal reset is safe.
|
||||
auto preListener = signal->listen([&] { count += 1; });
|
||||
|
||||
auto listener = signal->listen([&] { signal.reset(); });
|
||||
|
||||
// This ensures a destructor of a listener called after signal reset is safe
|
||||
// and gets called.
|
||||
auto postListener = signal->listen([&] { count += 1; });
|
||||
|
||||
signal->emit();
|
||||
EXPECT_EQ(count, 2); // all listeners should fire regardless of signal deletion
|
||||
}
|
||||
|
||||
// purely an asan test
|
||||
static void signalDestroyedBeforeListener() {
|
||||
CHyprSignalListener listener1;
|
||||
CHyprSignalListener listener2;
|
||||
|
||||
CSignalT<> signal;
|
||||
|
||||
listener1 = signal.listen([] {});
|
||||
listener2 = signal.listen([] {});
|
||||
}
|
||||
|
||||
static void signalDestroyedWithAddedListener() {
|
||||
int count = 0;
|
||||
|
||||
auto signal = std::make_unique<CSignalT<>>();
|
||||
CHyprSignalListener shouldNotRun;
|
||||
|
||||
auto listener = signal->listen([&] {
|
||||
shouldNotRun = signal->listen([&] { count += 2; });
|
||||
signal.reset();
|
||||
});
|
||||
|
||||
signal->emit();
|
||||
EXPECT_EQ(count, 0);
|
||||
}
|
||||
|
||||
static void signalDestroyedWithRemovedAndAddedListener() {
|
||||
int count = 0;
|
||||
|
||||
auto signal = std::make_unique<CSignalT<>>();
|
||||
CHyprSignalListener removed;
|
||||
CHyprSignalListener shouldNotRun;
|
||||
|
||||
auto listener = signal->listen([&] {
|
||||
removed.reset();
|
||||
shouldNotRun = signal->listen([&] { count += 2; });
|
||||
signal.reset();
|
||||
});
|
||||
|
||||
removed = signal->listen([&] { count += 1; });
|
||||
|
||||
signal->emit();
|
||||
EXPECT_EQ(count, 0);
|
||||
}
|
||||
|
||||
static void staticListener() {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<int> signal;
|
||||
signal.listenStatic([&](int newData) { data = newData; });
|
||||
|
||||
signal.emit(1);
|
||||
EXPECT_EQ(data, 1);
|
||||
}
|
||||
|
||||
static void staticListenerDestroy() {
|
||||
int count = 0;
|
||||
|
||||
auto signal = makeShared<CSignalT<>>();
|
||||
signal->listenStatic([&] { count += 1; });
|
||||
|
||||
signal->listenStatic([&] {
|
||||
// should not fire but SHOULD be freed
|
||||
signal->listenStatic([&] { count += 3; });
|
||||
|
||||
signal.reset();
|
||||
});
|
||||
|
||||
signal->listenStatic([&] { count += 1; });
|
||||
|
||||
signal->emit();
|
||||
EXPECT_EQ(count, 2);
|
||||
}
|
||||
|
||||
// purely an asan test
|
||||
static void listenerDestroysSelf() {
|
||||
CSignalT<> signal;
|
||||
|
||||
CHyprSignalListener listener;
|
||||
listener = signal.listen([&] { listener.reset(); });
|
||||
|
||||
// the static signal case is taken care of above
|
||||
|
||||
signal.emit();
|
||||
}
|
||||
|
||||
TEST(Signal, signal) {
|
||||
legacy();
|
||||
legacyListenerEmit();
|
||||
legacyListeners();
|
||||
empty();
|
||||
typed();
|
||||
ignoreParams();
|
||||
typedMany();
|
||||
ref();
|
||||
refMany();
|
||||
autoRefTypes();
|
||||
forward();
|
||||
listenerAdded();
|
||||
lastListenerSwapped();
|
||||
signalDestroyed();
|
||||
signalDestroyedBeforeListener();
|
||||
signalDestroyedWithAddedListener();
|
||||
signalDestroyedWithRemovedAndAddedListener();
|
||||
staticListener();
|
||||
staticListenerDestroy();
|
||||
signalDestroyed();
|
||||
listenerDestroysSelf();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,26 +1,10 @@
|
|||
#include <ranges>
|
||||
#include <algorithm>
|
||||
#include <hyprutils/string/String.hpp>
|
||||
#include <hyprutils/string/ConstVarList.hpp>
|
||||
|
||||
using namespace Hyprutils::String;
|
||||
|
||||
static std::string_view trim(const std::string_view& sv) {
|
||||
if (sv.empty())
|
||||
return sv;
|
||||
|
||||
size_t countBefore = 0;
|
||||
while (countBefore < sv.length() && std::isspace(sv.at(countBefore))) {
|
||||
countBefore++;
|
||||
}
|
||||
|
||||
size_t countAfter = 0;
|
||||
while (countAfter < sv.length() - countBefore && std::isspace(sv.at(sv.length() - countAfter - 1))) {
|
||||
countAfter++;
|
||||
}
|
||||
|
||||
return sv.substr(countBefore, sv.length() - countBefore - countAfter);
|
||||
}
|
||||
|
||||
CConstVarList::CConstVarList(const std::string& in, const size_t lastArgNo, const char delim, const bool removeEmpty) : m_str(in) {
|
||||
if (in.empty())
|
||||
return;
|
||||
|
|
@ -33,11 +17,11 @@ CConstVarList::CConstVarList(const std::string& in, const size_t lastArgNo, cons
|
|||
if (removeEmpty && s.empty())
|
||||
continue;
|
||||
if (++idx == lastArgNo) {
|
||||
m_args.emplace_back(trim(in.substr(pos)));
|
||||
m_args.emplace_back(trim(std::string_view{m_str}.substr(pos)));
|
||||
break;
|
||||
}
|
||||
pos += s.size() + 1;
|
||||
m_args.emplace_back(trim(s.data()));
|
||||
m_args.emplace_back(trim(std::string_view{s.data()}));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -52,3 +36,19 @@ std::string CConstVarList::join(const std::string& joiner, size_t from, size_t t
|
|||
|
||||
return rolling;
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(String, constvarlist) {
|
||||
CConstVarList listConst("hello world!", 0, 's', true);
|
||||
EXPECT_EQ(listConst[0], "hello");
|
||||
EXPECT_EQ(listConst[1], "world!");
|
||||
|
||||
CConstVarList listConst2("0 set", 2, ' ');
|
||||
EXPECT_EQ(listConst2[0], "0");
|
||||
EXPECT_EQ(listConst2[1], "set");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -22,6 +22,27 @@ std::string Hyprutils::String::trim(const std::string& in) {
|
|||
return result;
|
||||
}
|
||||
|
||||
std::string_view Hyprutils::String::trim(const std::string_view& sv) {
|
||||
if (sv.empty())
|
||||
return sv;
|
||||
|
||||
size_t countBefore = 0;
|
||||
while (countBefore < sv.length() && std::isspace(sv.at(countBefore))) {
|
||||
countBefore++;
|
||||
}
|
||||
|
||||
size_t countAfter = 0;
|
||||
while (countAfter < sv.length() - countBefore && std::isspace(sv.at(sv.length() - countAfter - 1))) {
|
||||
countAfter++;
|
||||
}
|
||||
|
||||
return sv.substr(countBefore, sv.length() - countBefore - countAfter);
|
||||
}
|
||||
|
||||
std::string Hyprutils::String::trim(const char* in) {
|
||||
return trim(std::string{in});
|
||||
}
|
||||
|
||||
bool Hyprutils::String::isNumber(const std::string& str, bool allowfloat) {
|
||||
if (str.empty())
|
||||
return false;
|
||||
|
|
@ -67,3 +88,38 @@ void Hyprutils::String::replaceInString(std::string& string, const std::string&
|
|||
pos += to.length();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(String, string) {
|
||||
EXPECT_EQ(trim(" a "), "a");
|
||||
EXPECT_EQ(trim(" a a "), "a a");
|
||||
EXPECT_EQ(trim("a"), "a");
|
||||
EXPECT_EQ(trim(" "), "");
|
||||
|
||||
EXPECT_EQ(isNumber("99214123434"), true);
|
||||
EXPECT_EQ(isNumber("-35252345234"), true);
|
||||
EXPECT_EQ(isNumber("---3423--432"), false);
|
||||
EXPECT_EQ(isNumber("s---3423--432"), false);
|
||||
EXPECT_EQ(isNumber("---3423--432s"), false);
|
||||
EXPECT_EQ(isNumber("1s"), false);
|
||||
EXPECT_EQ(isNumber(""), false);
|
||||
EXPECT_EQ(isNumber("-"), false);
|
||||
EXPECT_EQ(isNumber("--0"), false);
|
||||
EXPECT_EQ(isNumber("abc"), false);
|
||||
EXPECT_EQ(isNumber("0.0", true), true);
|
||||
EXPECT_EQ(isNumber("0.2", true), true);
|
||||
EXPECT_EQ(isNumber("0.", true), false);
|
||||
EXPECT_EQ(isNumber(".0", true), false);
|
||||
EXPECT_EQ(isNumber("", true), false);
|
||||
EXPECT_EQ(isNumber("vvss", true), false);
|
||||
EXPECT_EQ(isNumber("0.9999s", true), false);
|
||||
EXPECT_EQ(isNumber("s0.9999", true), false);
|
||||
EXPECT_EQ(isNumber("-1.0", true), true);
|
||||
EXPECT_EQ(isNumber("-1..0", true), false);
|
||||
EXPECT_EQ(isNumber("-10.0000000001", true), true);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -22,7 +22,7 @@ Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastAr
|
|||
break;
|
||||
}
|
||||
pos += s.size() + 1;
|
||||
m_vArgs.emplace_back(trim(s.data()));
|
||||
m_vArgs.emplace_back(trim(std::string{s.data()}));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -36,3 +36,15 @@ std::string Hyprutils::String::CVarList::join(const std::string& joiner, size_t
|
|||
|
||||
return rolling;
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(String, varlist) {
|
||||
CVarList list("hello world!", 0, 's', true);
|
||||
EXPECT_EQ(list[0], "hello");
|
||||
EXPECT_EQ(list[1], "world!");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -0,0 +1,181 @@
|
|||
#include <algorithm>
|
||||
|
||||
#include <hyprutils/string/VarList2.hpp>
|
||||
#include <hyprutils/string/String.hpp>
|
||||
|
||||
using namespace Hyprutils::String;
|
||||
|
||||
CVarList2::CVarList2(std::string&& in, const size_t lastArgNo, const char delim, const bool removeEmpty, const bool allowEscape) : m_inString(std::move(in)) {
|
||||
if (m_inString.empty())
|
||||
return;
|
||||
|
||||
auto isDelimiter = [&delim](const char& c) { return delim == 's' ? std::isspace(c) : delim == c; };
|
||||
|
||||
size_t argBegin = 0;
|
||||
std::vector<size_t> escapedIndices; // local to the current arg
|
||||
for (size_t i = 0; i < m_inString.size(); ++i) {
|
||||
const char& c = m_inString[i];
|
||||
|
||||
if (!isDelimiter(c))
|
||||
continue;
|
||||
|
||||
if (allowEscape) {
|
||||
// we allow escape, so this might be escaped. Check first
|
||||
if (i - argBegin != 0) {
|
||||
const char& previousC = m_inString[i - 1];
|
||||
if (i - argBegin == 1) {
|
||||
if (previousC == '\\') {
|
||||
escapedIndices.emplace_back(i - argBegin - 1);
|
||||
continue; // escaped
|
||||
}
|
||||
// fall to breaking, not escaped
|
||||
} else {
|
||||
const char& prevPreviousC = m_inString[i - 2];
|
||||
if (previousC == '\\') {
|
||||
// whether or not escaped, pop char
|
||||
escapedIndices.emplace_back(i - argBegin - 1);
|
||||
|
||||
if (prevPreviousC != '\\') {
|
||||
// escaped
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// fall to breaking, not escaped, but mark the \\ to be popped
|
||||
}
|
||||
// fall to breaking, couldn't be escaped
|
||||
}
|
||||
}
|
||||
|
||||
// here we found a delimiter and need to break up the string (not escaped)
|
||||
|
||||
if (escapedIndices.empty()) {
|
||||
// we didn't escape anything, so we can use inString
|
||||
const auto ARG = trim(std::string_view{m_inString}.substr(argBegin, i - argBegin));
|
||||
|
||||
if (!ARG.empty() || !removeEmpty)
|
||||
m_args.emplace_back(ARG);
|
||||
} else {
|
||||
// we escaped something, fixup the string, add to copies, then emplace
|
||||
std::string cpy = m_inString.substr(argBegin, i - argBegin);
|
||||
for (size_t i = 0; i < escapedIndices.size(); ++i) {
|
||||
cpy = cpy.substr(0, escapedIndices[i] - i) + cpy.substr(escapedIndices[i] - i + 1);
|
||||
}
|
||||
m_copyStrings.emplace_back(std::move(cpy));
|
||||
m_args.emplace_back(trim(std::string_view{m_copyStrings.back()}));
|
||||
}
|
||||
|
||||
// update next argBegin
|
||||
argBegin = i + 1;
|
||||
escapedIndices.clear();
|
||||
}
|
||||
|
||||
// append anything left
|
||||
if (argBegin < m_inString.size()) {
|
||||
if (escapedIndices.empty()) {
|
||||
// we didn't escape anything, so we can use inString
|
||||
const auto ARG = trim(std::string_view{m_inString}.substr(argBegin, m_inString.size() - argBegin));
|
||||
|
||||
if (!ARG.empty() || !removeEmpty)
|
||||
m_args.emplace_back(ARG);
|
||||
} else {
|
||||
// we escaped something, fixup the string, add to copies, then emplace
|
||||
std::string cpy = m_inString.substr(argBegin, m_inString.size() - argBegin);
|
||||
for (size_t i = 0; i < escapedIndices.size(); ++i) {
|
||||
cpy = cpy.substr(0, escapedIndices[i] - i) + cpy.substr(escapedIndices[i] - i + 1);
|
||||
}
|
||||
m_copyStrings.emplace_back(std::move(cpy));
|
||||
m_args.emplace_back(trim(std::string_view{m_copyStrings.back()}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string CVarList2::join(const std::string& joiner, size_t from, size_t to) const {
|
||||
if (to == 0 || to <= from)
|
||||
to = m_args.size();
|
||||
|
||||
std::string roll;
|
||||
for (size_t i = from; i < to && i < m_args.size(); ++i) {
|
||||
roll += m_args[i];
|
||||
if (i + 1 < to && i + 1 < m_args.size())
|
||||
roll += joiner;
|
||||
}
|
||||
return roll;
|
||||
}
|
||||
|
||||
void CVarList2::append(std::string&& arg) {
|
||||
m_copyStrings.emplace_back(std::move(arg));
|
||||
m_args.emplace_back(m_copyStrings.back());
|
||||
}
|
||||
|
||||
bool CVarList2::contains(const std::string& el) {
|
||||
return std::ranges::any_of(m_args, [&el](const auto& e) { return e == el; });
|
||||
}
|
||||
|
||||
#ifdef HU_UNIT_TESTS
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(String, varlist2) {
|
||||
CVarList2 varList2("0 set", 2, ' ');
|
||||
EXPECT_EQ(varList2[0], "0");
|
||||
EXPECT_EQ(varList2[1], "set");
|
||||
|
||||
varList2.append("Hello");
|
||||
|
||||
EXPECT_EQ(varList2[1], "set");
|
||||
EXPECT_EQ(varList2[2], "Hello");
|
||||
EXPECT_EQ(varList2[3], "");
|
||||
EXPECT_EQ(varList2.contains("set"), true);
|
||||
EXPECT_EQ(varList2.contains("sett"), false);
|
||||
EXPECT_EQ(varList2.contains(""), false);
|
||||
EXPECT_EQ(varList2.size(), 3);
|
||||
|
||||
CVarList2 varList2B("hello, world\\, ok?", 0, ',', true, true);
|
||||
EXPECT_EQ(varList2B[0], "hello");
|
||||
EXPECT_EQ(varList2B[1], "world, ok?");
|
||||
|
||||
CVarList2 varList2C("hello, , ok?", 0, ',', true, true);
|
||||
EXPECT_EQ(varList2C[0], "hello");
|
||||
EXPECT_EQ(varList2C[1], "ok?");
|
||||
|
||||
CVarList2 varList2D("\\\\, , ok?", 0, ',', true, true);
|
||||
EXPECT_EQ(varList2D[0], "\\");
|
||||
EXPECT_EQ(varList2D[1], "ok?");
|
||||
|
||||
CVarList2 varList2E("\\, , ok?", 0, ',', true, true);
|
||||
EXPECT_EQ(varList2E[0], ",");
|
||||
EXPECT_EQ(varList2E[1], "ok?");
|
||||
|
||||
CVarList2 varList2F("Hello, world\\\\, ok?", 0, ',', true, true);
|
||||
EXPECT_EQ(varList2F[0], "Hello");
|
||||
EXPECT_EQ(varList2F[1], "world\\");
|
||||
EXPECT_EQ(varList2F[2], "ok?");
|
||||
|
||||
CVarList2 varList2G("Hello,\\, ok?", 0, ',', true, true);
|
||||
EXPECT_EQ(varList2G[0], "Hello");
|
||||
EXPECT_EQ(varList2G[1], ", ok?");
|
||||
|
||||
CVarList2 varList2H("Hello,\\\\, ok?", 0, ',', true, true);
|
||||
EXPECT_EQ(varList2H[0], "Hello");
|
||||
EXPECT_EQ(varList2H[1], "\\");
|
||||
EXPECT_EQ(varList2H[2], "ok?");
|
||||
|
||||
CVarList2 varList2I("Hello,\\, ok?", 0, ',', true, false);
|
||||
EXPECT_EQ(varList2I[0], "Hello");
|
||||
EXPECT_EQ(varList2I[1], "\\");
|
||||
EXPECT_EQ(varList2I[2], "ok?");
|
||||
|
||||
CVarList2 varList2J("", 0, ',', true, false);
|
||||
EXPECT_EQ(varList2J[0], "");
|
||||
|
||||
CVarList2 varList2K(",\\, ok?", 0, ',', true);
|
||||
EXPECT_EQ(varList2K[0], ", ok?");
|
||||
|
||||
CVarList2 varList2L("Hello, world", 0, ',', true);
|
||||
EXPECT_EQ(varList2L.join(" "), "Hello world");
|
||||
EXPECT_EQ(varList2L.join(" ", 0, 1000), "Hello world");
|
||||
EXPECT_EQ(varList2L.join(" ", 0, 1), "Hello");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -1,397 +0,0 @@
|
|||
#include <hyprutils/animation/AnimationConfig.hpp>
|
||||
#include <hyprutils/animation/AnimationManager.hpp>
|
||||
#include <hyprutils/animation/AnimatedVariable.hpp>
|
||||
#include <hyprutils/memory/WeakPtr.hpp>
|
||||
#include <hyprutils/memory/UniquePtr.hpp>
|
||||
#include "shared.hpp"
|
||||
|
||||
#define SP CSharedPointer
|
||||
#define WP CWeakPointer
|
||||
#define UP CUniquePointer
|
||||
|
||||
using namespace Hyprutils::Animation;
|
||||
using namespace Hyprutils::Math;
|
||||
using namespace Hyprutils::Memory;
|
||||
|
||||
class EmtpyContext {};
|
||||
|
||||
template <typename VarType>
|
||||
using CAnimatedVariable = CGenericAnimatedVariable<VarType, EmtpyContext>;
|
||||
|
||||
template <typename VarType>
|
||||
using PANIMVAR = SP<CAnimatedVariable<VarType>>;
|
||||
|
||||
template <typename VarType>
|
||||
using PANIMVARREF = WP<CAnimatedVariable<VarType>>;
|
||||
|
||||
enum eAVTypes {
|
||||
INT = 1,
|
||||
TEST,
|
||||
};
|
||||
|
||||
struct SomeTestType {
|
||||
bool done = false;
|
||||
bool operator==(const SomeTestType& other) const {
|
||||
return done == other.done;
|
||||
}
|
||||
SomeTestType& operator=(const SomeTestType& other) {
|
||||
done = other.done;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
CAnimationConfigTree animationTree;
|
||||
|
||||
class CMyAnimationManager : public CAnimationManager {
|
||||
public:
|
||||
void tick() {
|
||||
for (size_t i = 0; i < m_vActiveAnimatedVariables.size(); i++) {
|
||||
const auto PAV = m_vActiveAnimatedVariables[i].lock();
|
||||
if (!PAV || !PAV->ok() || !PAV->isBeingAnimated())
|
||||
continue;
|
||||
|
||||
const auto SPENT = PAV->getPercent();
|
||||
const auto PBEZIER = getBezier(PAV->getBezierName());
|
||||
|
||||
if (SPENT >= 1.f || !PAV->enabled()) {
|
||||
PAV->warp(true, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto POINTY = PBEZIER->getYForPoint(SPENT);
|
||||
|
||||
switch (PAV->m_Type) {
|
||||
case eAVTypes::INT: {
|
||||
auto avInt = dc<CAnimatedVariable<int>*>(PAV.get());
|
||||
if (!avInt)
|
||||
std::cout << Colors::RED << "Dynamic cast upcast failed" << Colors::RESET;
|
||||
|
||||
const auto DELTA = avInt->goal() - avInt->value();
|
||||
avInt->value() = avInt->begun() + (DELTA * POINTY);
|
||||
} break;
|
||||
case eAVTypes::TEST: {
|
||||
auto avCustom = dc<CAnimatedVariable<SomeTestType>*>(PAV.get());
|
||||
if (!avCustom)
|
||||
std::cout << Colors::RED << "Dynamic cast upcast failed" << Colors::RESET;
|
||||
|
||||
if (SPENT >= 1.f)
|
||||
avCustom->value().done = true;
|
||||
} break;
|
||||
default: {
|
||||
std::cout << Colors::RED << "What are we even doing?" << Colors::RESET;
|
||||
} break;
|
||||
}
|
||||
|
||||
PAV->onUpdate();
|
||||
}
|
||||
|
||||
tickDone();
|
||||
}
|
||||
|
||||
template <typename VarType>
|
||||
void createAnimation(const VarType& v, PANIMVAR<VarType>& av, const std::string& animationConfigName) {
|
||||
constexpr const eAVTypes EAVTYPE = std::is_same_v<VarType, int> ? eAVTypes::INT : eAVTypes::TEST;
|
||||
const auto PAV = makeShared<CGenericAnimatedVariable<VarType, EmtpyContext>>();
|
||||
|
||||
PAV->create(EAVTYPE, sc<CAnimationManager*>(this), PAV, v);
|
||||
PAV->setConfig(animationTree.getConfig(animationConfigName));
|
||||
av = std::move(PAV);
|
||||
}
|
||||
|
||||
virtual void scheduleTick() {
|
||||
;
|
||||
}
|
||||
|
||||
virtual void onTicked() {
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
UP<CMyAnimationManager> pAnimationManager;
|
||||
|
||||
class Subject {
|
||||
public:
|
||||
Subject(const int& a, const int& b) {
|
||||
pAnimationManager->createAnimation(a, m_iA, "default");
|
||||
pAnimationManager->createAnimation(b, m_iB, "internal");
|
||||
pAnimationManager->createAnimation({}, m_iC, "default");
|
||||
}
|
||||
PANIMVAR<int> m_iA;
|
||||
PANIMVAR<int> m_iB;
|
||||
PANIMVAR<SomeTestType> m_iC;
|
||||
};
|
||||
|
||||
int config() {
|
||||
pAnimationManager = makeUnique<CMyAnimationManager>();
|
||||
|
||||
int ret = 0;
|
||||
|
||||
animationTree.createNode("global");
|
||||
animationTree.createNode("internal");
|
||||
|
||||
animationTree.createNode("foo", "internal");
|
||||
animationTree.createNode("default", "global");
|
||||
animationTree.createNode("bar", "default");
|
||||
|
||||
/*
|
||||
internal
|
||||
↳ foo
|
||||
global
|
||||
↳ default
|
||||
↳ bar
|
||||
*/
|
||||
|
||||
auto barCfg = animationTree.getConfig("bar");
|
||||
auto internalCfg = animationTree.getConfig("internal");
|
||||
|
||||
// internal is a root node and should point to itself
|
||||
EXPECT(internalCfg->pParentAnimation.get(), internalCfg.get());
|
||||
EXPECT(internalCfg->pValues.get(), internalCfg.get());
|
||||
|
||||
animationTree.setConfigForNode("global", 1, 4.0, "default", "asdf");
|
||||
|
||||
EXPECT(barCfg->internalEnabled, -1);
|
||||
{
|
||||
const auto PVALUES = barCfg->pValues.lock();
|
||||
EXPECT(PVALUES->internalEnabled, 1);
|
||||
EXPECT(PVALUES->internalBezier, "default");
|
||||
EXPECT(PVALUES->internalStyle, "asdf");
|
||||
EXPECT(PVALUES->internalSpeed, 4.0);
|
||||
}
|
||||
EXPECT(barCfg->pParentAnimation.get(), animationTree.getConfig("default").get());
|
||||
|
||||
// Overwrite our own values
|
||||
animationTree.setConfigForNode("bar", 1, 4.2, "test", "qwer");
|
||||
|
||||
{
|
||||
const auto PVALUES = barCfg->pValues.lock();
|
||||
EXPECT(PVALUES->internalEnabled, 1);
|
||||
EXPECT(PVALUES->internalBezier, "test");
|
||||
EXPECT(PVALUES->internalStyle, "qwer");
|
||||
EXPECT(PVALUES->internalSpeed, 4.2f);
|
||||
}
|
||||
|
||||
// Now overwrite the parent
|
||||
animationTree.setConfigForNode("default", 0, 0.0, "zxcv", "foo");
|
||||
|
||||
{
|
||||
// Expecting no change
|
||||
const auto PVALUES = barCfg->pValues.lock();
|
||||
EXPECT(PVALUES->internalEnabled, 1);
|
||||
EXPECT(PVALUES->internalBezier, "test");
|
||||
EXPECT(PVALUES->internalStyle, "qwer");
|
||||
EXPECT(PVALUES->internalSpeed, 4.2f);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
int ret = config();
|
||||
|
||||
animationTree.createNode("global");
|
||||
animationTree.createNode("internal");
|
||||
|
||||
animationTree.createNode("default", "global");
|
||||
animationTree.setConfigForNode("global", 1, 4.0, "default", "asdf");
|
||||
|
||||
Subject s(0, 0);
|
||||
|
||||
EXPECT(s.m_iA->value(), 0);
|
||||
EXPECT(s.m_iB->value(), 0);
|
||||
|
||||
// Test destruction of a CAnimatedVariable
|
||||
{
|
||||
Subject s2(10, 10);
|
||||
// Adds them to active
|
||||
*s2.m_iA = 1;
|
||||
*s2.m_iB = 2;
|
||||
// We deliberately do not tick here, to make sure the destructor removes active animated variables
|
||||
}
|
||||
|
||||
EXPECT(pAnimationManager->shouldTickForNext(), false);
|
||||
EXPECT(s.m_iC->value().done, false);
|
||||
|
||||
*s.m_iA = 10;
|
||||
*s.m_iB = 100;
|
||||
*s.m_iC = SomeTestType(true);
|
||||
|
||||
EXPECT(s.m_iC->value().done, false);
|
||||
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT(s.m_iA->value(), 10);
|
||||
EXPECT(s.m_iB->value(), 100);
|
||||
EXPECT(s.m_iC->value().done, true);
|
||||
|
||||
s.m_iA->setValue(0);
|
||||
s.m_iB->setValue(0);
|
||||
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT(s.m_iA->value(), 10);
|
||||
EXPECT(s.m_iB->value(), 100);
|
||||
|
||||
// Test config stuff
|
||||
EXPECT(s.m_iA->getBezierName(), "default");
|
||||
EXPECT(s.m_iA->getStyle(), "asdf");
|
||||
EXPECT(s.m_iA->enabled(), true);
|
||||
|
||||
animationTree.getConfig("global")->internalEnabled = 0;
|
||||
|
||||
EXPECT(s.m_iA->enabled(), false);
|
||||
|
||||
*s.m_iA = 50;
|
||||
pAnimationManager->tick(); // Expecting a warp
|
||||
EXPECT(s.m_iA->value(), 50);
|
||||
|
||||
// Test missing pValues
|
||||
animationTree.getConfig("global")->internalEnabled = 0;
|
||||
animationTree.getConfig("default")->pValues.reset();
|
||||
|
||||
EXPECT(s.m_iA->enabled(), false);
|
||||
EXPECT(s.m_iA->getBezierName(), "default");
|
||||
EXPECT(s.m_iA->getStyle(), "");
|
||||
EXPECT(s.m_iA->getPercent(), 1.f);
|
||||
|
||||
// Reset
|
||||
animationTree.setConfigForNode("default", 1, 1, "default");
|
||||
|
||||
//
|
||||
// Test callbacks
|
||||
//
|
||||
int beginCallbackRan = 0;
|
||||
int updateCallbackRan = 0;
|
||||
int endCallbackRan = 0;
|
||||
s.m_iA->setCallbackOnBegin([&beginCallbackRan](WP<CBaseAnimatedVariable> pav) { beginCallbackRan++; });
|
||||
s.m_iA->setUpdateCallback([&updateCallbackRan](WP<CBaseAnimatedVariable> pav) { updateCallbackRan++; });
|
||||
s.m_iA->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> pav) { endCallbackRan++; }, false);
|
||||
|
||||
s.m_iA->setValueAndWarp(42);
|
||||
|
||||
EXPECT(beginCallbackRan, 0);
|
||||
EXPECT(updateCallbackRan, 1);
|
||||
EXPECT(endCallbackRan, 2); // first called when setting the callback, then when warping.
|
||||
|
||||
*s.m_iA = 1337;
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT(beginCallbackRan, 1);
|
||||
EXPECT(updateCallbackRan > 2, true);
|
||||
EXPECT(endCallbackRan, 3);
|
||||
|
||||
std::vector<PANIMVAR<int>> vars;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
vars.resize(vars.size() + 1);
|
||||
pAnimationManager->createAnimation(1, vars.back(), "default");
|
||||
*vars.back() = 1337;
|
||||
}
|
||||
|
||||
// test adding / removing vars during a tick
|
||||
s.m_iA->resetAllCallbacks();
|
||||
s.m_iA->setUpdateCallback([&vars](WP<CBaseAnimatedVariable> v) {
|
||||
if (v.lock() != vars.back())
|
||||
vars.back()->warp();
|
||||
});
|
||||
s.m_iA->setCallbackOnEnd([&s, &vars](auto) {
|
||||
vars.resize(vars.size() + 1);
|
||||
pAnimationManager->createAnimation(1, vars.back(), "default");
|
||||
*vars.back() = 1337;
|
||||
});
|
||||
|
||||
*s.m_iA = 1000000;
|
||||
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT(s.m_iA->value(), 1000000);
|
||||
// all vars should be set to 1337
|
||||
EXPECT(std::find_if(vars.begin(), vars.end(), [](const auto& v) { return v->value() != 1337; }) == vars.end(), true);
|
||||
|
||||
// test one-time callbacks
|
||||
s.m_iA->resetAllCallbacks();
|
||||
s.m_iA->setCallbackOnEnd([&endCallbackRan](auto) { endCallbackRan++; }, true);
|
||||
|
||||
EXPECT(endCallbackRan, 4);
|
||||
|
||||
s.m_iA->setValueAndWarp(10);
|
||||
|
||||
EXPECT(endCallbackRan, 4);
|
||||
EXPECT(s.m_iA->value(), 10);
|
||||
|
||||
// test warp
|
||||
*s.m_iA = 3;
|
||||
s.m_iA->setCallbackOnEnd([&endCallbackRan](auto) { endCallbackRan++; }, false);
|
||||
|
||||
s.m_iA->warp(false);
|
||||
EXPECT(endCallbackRan, 4);
|
||||
|
||||
*s.m_iA = 4;
|
||||
s.m_iA->warp(true);
|
||||
EXPECT(endCallbackRan, 5);
|
||||
|
||||
// test getCurveValue
|
||||
*s.m_iA = 0;
|
||||
EXPECT(s.m_iA->getCurveValue(), 0.f);
|
||||
s.m_iA->warp();
|
||||
EXPECT(s.m_iA->getCurveValue(), 1.f);
|
||||
EXPECT(endCallbackRan, 6);
|
||||
|
||||
// test end callback readding the var
|
||||
*s.m_iA = 5;
|
||||
s.m_iA->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> v) {
|
||||
endCallbackRan++;
|
||||
const auto PAV = dc<CAnimatedVariable<int>*>(v.lock().get());
|
||||
|
||||
*PAV = 10;
|
||||
PAV->setCallbackOnEnd([&endCallbackRan](WP<CBaseAnimatedVariable> v) { endCallbackRan++; });
|
||||
});
|
||||
|
||||
while (pAnimationManager->shouldTickForNext()) {
|
||||
pAnimationManager->tick();
|
||||
}
|
||||
|
||||
EXPECT(endCallbackRan, 8);
|
||||
EXPECT(s.m_iA->value(), 10);
|
||||
|
||||
// Test duplicate active anim vars are not allowed
|
||||
{
|
||||
EXPECT(pAnimationManager->m_vActiveAnimatedVariables.size(), 0);
|
||||
PANIMVAR<int> a;
|
||||
pAnimationManager->createAnimation(1, a, "default");
|
||||
EXPECT(pAnimationManager->m_vActiveAnimatedVariables.size(), 0);
|
||||
*a = 10;
|
||||
EXPECT(pAnimationManager->m_vActiveAnimatedVariables.size(), 1);
|
||||
*a = 20;
|
||||
EXPECT(pAnimationManager->m_vActiveAnimatedVariables.size(), 1);
|
||||
a->warp();
|
||||
EXPECT(pAnimationManager->m_vActiveAnimatedVariables.size(), 0);
|
||||
EXPECT(a->value(), 20);
|
||||
}
|
||||
|
||||
// Test no crash when animation manager gets destroyed
|
||||
{
|
||||
PANIMVAR<int> a;
|
||||
pAnimationManager->createAnimation(1, a, "default");
|
||||
*a = 10;
|
||||
pAnimationManager.reset();
|
||||
EXPECT(a->isAnimationManagerDead(), true);
|
||||
a->setValueAndWarp(11);
|
||||
EXPECT(a->value(), 11);
|
||||
*a = 12;
|
||||
a->warp();
|
||||
EXPECT(a->value(), 12);
|
||||
*a = 13;
|
||||
} // a gets destroyed
|
||||
|
||||
EXPECT(pAnimationManager.get(), nullptr);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -1,49 +0,0 @@
|
|||
#include <hyprutils/os/FileDescriptor.hpp>
|
||||
#include "shared.hpp"
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace Hyprutils::OS;
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
std::string name = "/test_filedescriptors";
|
||||
CFileDescriptor fd(shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600));
|
||||
|
||||
int ret = 0;
|
||||
EXPECT(fd.isValid(), true);
|
||||
EXPECT(fd.isReadable(), true);
|
||||
|
||||
int flags = fd.getFlags();
|
||||
EXPECT(fd.getFlags(), FD_CLOEXEC);
|
||||
flags &= ~FD_CLOEXEC;
|
||||
fd.setFlags(flags);
|
||||
EXPECT(fd.getFlags(), !FD_CLOEXEC);
|
||||
|
||||
CFileDescriptor fd2 = fd.duplicate();
|
||||
EXPECT(fd.isValid(), true);
|
||||
EXPECT(fd.isReadable(), true);
|
||||
EXPECT(fd2.isValid(), true);
|
||||
EXPECT(fd2.isReadable(), true);
|
||||
|
||||
CFileDescriptor fd3(fd2.take());
|
||||
EXPECT(fd.isValid(), true);
|
||||
EXPECT(fd.isReadable(), true);
|
||||
EXPECT(fd2.isValid(), false);
|
||||
EXPECT(fd2.isReadable(), false);
|
||||
|
||||
// .duplicate default flags is FD_CLOEXEC
|
||||
EXPECT(fd3.getFlags(), FD_CLOEXEC);
|
||||
|
||||
fd.reset();
|
||||
fd2.reset();
|
||||
fd3.reset();
|
||||
|
||||
EXPECT(fd.isReadable(), false);
|
||||
EXPECT(fd2.isReadable(), false);
|
||||
EXPECT(fd3.isReadable(), false);
|
||||
|
||||
shm_unlink(name.c_str());
|
||||
|
||||
return ret;
|
||||
}
|
||||
136
tests/math.cpp
136
tests/math.cpp
|
|
@ -1,136 +0,0 @@
|
|||
#include <hyprutils/math/Region.hpp>
|
||||
#include <hyprutils/math/Mat3x3.hpp>
|
||||
#include "shared.hpp"
|
||||
|
||||
using namespace Hyprutils::Math;
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
CRegion rg = {0, 0, 100, 100};
|
||||
rg.add(CBox{{}, {20, 200}});
|
||||
|
||||
int ret = 0;
|
||||
|
||||
EXPECT(rg.getExtents().height, 200);
|
||||
EXPECT(rg.getExtents().width, 100);
|
||||
|
||||
rg.intersect(CBox{10, 10, 300, 300});
|
||||
|
||||
EXPECT(rg.getExtents().width, 90);
|
||||
EXPECT(rg.getExtents().height, 190);
|
||||
|
||||
/*Box.cpp test cases*/
|
||||
// Test default constructor and accessors
|
||||
{
|
||||
CBox box1;
|
||||
EXPECT(box1.x, 0);
|
||||
EXPECT(box1.y, 0);
|
||||
EXPECT(box1.width, 0);
|
||||
EXPECT(box1.height, 0);
|
||||
|
||||
// Test parameterized constructor and accessors
|
||||
CBox box2(10, 20, 30, 40);
|
||||
EXPECT(box2.x, 10);
|
||||
EXPECT(box2.y, 20);
|
||||
EXPECT(box2.width, 30);
|
||||
EXPECT(box2.height, 40);
|
||||
|
||||
// Test setters and getters
|
||||
box2.translate(Vector2D(5, -5));
|
||||
EXPECT_VECTOR2D(box2.pos(), Vector2D(15, 15));
|
||||
}
|
||||
|
||||
//Test Scaling and Transformation
|
||||
{
|
||||
CBox box(10, 10, 20, 30);
|
||||
|
||||
// Test scaling
|
||||
box.scale(2.0);
|
||||
EXPECT_VECTOR2D(box.size(), Vector2D(40, 60));
|
||||
EXPECT_VECTOR2D(box.pos(), Vector2D(20, 20));
|
||||
|
||||
// Test scaling from center
|
||||
box.scaleFromCenter(0.5);
|
||||
EXPECT_VECTOR2D(box.size(), Vector2D(20, 30));
|
||||
EXPECT_VECTOR2D(box.pos(), Vector2D(30, 35));
|
||||
|
||||
// Test transformation
|
||||
box.transform(HYPRUTILS_TRANSFORM_90, 100, 200);
|
||||
EXPECT_VECTOR2D(box.pos(), Vector2D(135, 30));
|
||||
EXPECT_VECTOR2D(box.size(), Vector2D(30, 20));
|
||||
|
||||
// Test Intersection and Extents
|
||||
}
|
||||
|
||||
{
|
||||
CBox box1(0, 0, 100, 100);
|
||||
CBox box2(50, 50, 100, 100);
|
||||
|
||||
CBox intersection = box1.intersection(box2);
|
||||
EXPECT_VECTOR2D(intersection.pos(), Vector2D(50, 50));
|
||||
EXPECT_VECTOR2D(intersection.size(), Vector2D(50, 50));
|
||||
|
||||
SBoxExtents extents = box1.extentsFrom(box2);
|
||||
EXPECT_VECTOR2D(extents.topLeft, Vector2D(50, 50));
|
||||
EXPECT_VECTOR2D(extents.bottomRight, Vector2D(-50, -50));
|
||||
}
|
||||
|
||||
// Test Boundary Conditions and Special Cases
|
||||
{
|
||||
CBox box(0, 0, 50, 50);
|
||||
|
||||
EXPECT(box.empty(), false);
|
||||
|
||||
EXPECT(box.containsPoint(Vector2D(25, 25)), true);
|
||||
EXPECT(box.containsPoint(Vector2D(60, 60)), false);
|
||||
EXPECT(box.overlaps(CBox(25, 25, 50, 50)), true);
|
||||
EXPECT(box.inside(CBox(0, 0, 100, 100)), false);
|
||||
}
|
||||
|
||||
// Test matrices
|
||||
{
|
||||
Mat3x3 jeremy = Mat3x3::outputProjection({1920, 1080}, HYPRUTILS_TRANSFORM_FLIPPED_90);
|
||||
Mat3x3 matrixBox = jeremy.projectBox(CBox{10, 10, 200, 200}, HYPRUTILS_TRANSFORM_NORMAL).translate({100, 100}).scale({1.25F, 1.5F}).transpose();
|
||||
|
||||
Mat3x3 expected = std::array<float, 9>{0, 0.46296296, 0, 0.3125, 0, 0, 19.84375, 36.055557, 1};
|
||||
// we need to do this to avoid precision errors on 32-bit archs
|
||||
EXPECT(std::abs(expected.getMatrix().at(0) - matrixBox.getMatrix().at(0)) < 0.1, true);
|
||||
EXPECT(std::abs(expected.getMatrix().at(1) - matrixBox.getMatrix().at(1)) < 0.1, true);
|
||||
EXPECT(std::abs(expected.getMatrix().at(2) - matrixBox.getMatrix().at(2)) < 0.1, true);
|
||||
EXPECT(std::abs(expected.getMatrix().at(3) - matrixBox.getMatrix().at(3)) < 0.1, true);
|
||||
EXPECT(std::abs(expected.getMatrix().at(4) - matrixBox.getMatrix().at(4)) < 0.1, true);
|
||||
EXPECT(std::abs(expected.getMatrix().at(5) - matrixBox.getMatrix().at(5)) < 0.1, true);
|
||||
EXPECT(std::abs(expected.getMatrix().at(6) - matrixBox.getMatrix().at(6)) < 0.1, true);
|
||||
EXPECT(std::abs(expected.getMatrix().at(7) - matrixBox.getMatrix().at(7)) < 0.1, true);
|
||||
EXPECT(std::abs(expected.getMatrix().at(8) - matrixBox.getMatrix().at(8)) < 0.1, true);
|
||||
}
|
||||
|
||||
// Test Region Scaling
|
||||
{
|
||||
CRegion rg(CBox{{20, 20}, {40, 40}});
|
||||
|
||||
auto extents = rg.getExtents();
|
||||
EXPECT_VECTOR2D(extents.pos(), Vector2D(20, 20));
|
||||
EXPECT_VECTOR2D(extents.size(), Vector2D(40, 40));
|
||||
|
||||
rg.scale(2);
|
||||
extents = rg.getExtents();
|
||||
EXPECT_VECTOR2D(extents.pos(), Vector2D(40, 40));
|
||||
EXPECT_VECTOR2D(extents.size(), Vector2D(80, 80));
|
||||
}
|
||||
|
||||
{
|
||||
Vector2D original(30, 40);
|
||||
Vector2D monitorSize(100, 200);
|
||||
|
||||
EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_NORMAL, monitorSize), Vector2D(30, 40 ));
|
||||
EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_90, monitorSize), Vector2D(40, 200 - 30));
|
||||
EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_180, monitorSize), Vector2D(100 - 30, 200 - 40));
|
||||
EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_270, monitorSize), Vector2D(100 - 40, 30 ));
|
||||
EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_FLIPPED, monitorSize), Vector2D(100 - 30, 40 ));
|
||||
EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_90, monitorSize), Vector2D(40, 30 ));
|
||||
EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_180, monitorSize), Vector2D(30, 200 - 40));
|
||||
EXPECT_VECTOR2D(original.transform(HYPRUTILS_TRANSFORM_FLIPPED_270, monitorSize), Vector2D(100 - 40, 200 - 30));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
35
tests/os.cpp
35
tests/os.cpp
|
|
@ -1,35 +0,0 @@
|
|||
#include <csignal>
|
||||
#include <cerrno>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <hyprutils/os/Process.hpp>
|
||||
#include "shared.hpp"
|
||||
|
||||
using namespace Hyprutils::OS;
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
int ret = 0;
|
||||
|
||||
CProcess process("sh", {"-c", "echo \"Hello $WORLD!\""});
|
||||
process.addEnv("WORLD", "World");
|
||||
|
||||
EXPECT(process.runAsync(), true);
|
||||
EXPECT(process.runSync(), true);
|
||||
|
||||
EXPECT(process.stdOut(), std::string{"Hello World!\n"});
|
||||
EXPECT(process.stdErr(), std::string{""});
|
||||
EXPECT(process.exitCode(), 0);
|
||||
|
||||
CProcess process2("sh", {"-c", "while true; do sleep 1; done;"});
|
||||
|
||||
EXPECT(process2.runAsync(), true);
|
||||
EXPECT(getpgid(process2.pid()) >= 0, true);
|
||||
|
||||
kill(process2.pid(), SIGKILL);
|
||||
|
||||
CProcess process3("sh", {"-c", "cat /geryueruggbuergheruger/reugiheruygyuerghuryeghyer/eruihgyuerguyerghyuerghuyergerguyer/NON_EXISTENT"});
|
||||
EXPECT(process3.runSync(), true);
|
||||
EXPECT(process3.exitCode(), 1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
namespace Colors {
|
||||
constexpr const char* RED = "\x1b[31m";
|
||||
constexpr const char* GREEN = "\x1b[32m";
|
||||
constexpr const char* YELLOW = "\x1b[33m";
|
||||
constexpr const char* BLUE = "\x1b[34m";
|
||||
constexpr const char* MAGENTA = "\x1b[35m";
|
||||
constexpr const char* CYAN = "\x1b[36m";
|
||||
constexpr const char* RESET = "\x1b[0m";
|
||||
};
|
||||
|
||||
#define EXPECT(expr, val) \
|
||||
if (const auto RESULT = expr; RESULT != (val)) { \
|
||||
std::cout << Colors::RED << "Failed: " << Colors::RESET << #expr << ", expected " << val << " but got " << RESULT << "\n"; \
|
||||
ret = 1; \
|
||||
} else { \
|
||||
std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got " << val << "\n"; \
|
||||
}
|
||||
#define EXPECT_VECTOR2D(expr, val) \
|
||||
do { \
|
||||
const auto& RESULT = expr; \
|
||||
const auto& EXPECTED = val; \
|
||||
if (!(std::abs(RESULT.x - EXPECTED.x) < 1e-6 && std::abs(RESULT.y - EXPECTED.y) < 1e-6)) { \
|
||||
std::cout << Colors::RED << "Failed: " << Colors::RESET << #expr << ", expected (" << EXPECTED.x << ", " << EXPECTED.y << ") but got (" << RESULT.x << ", " \
|
||||
<< RESULT.y << ")\n"; \
|
||||
ret = 1; \
|
||||
} else { \
|
||||
std::cout << Colors::GREEN << "Passed " << Colors::RESET << #expr << ". Got (" << RESULT.x << ", " << RESULT.y << ")\n"; \
|
||||
} \
|
||||
} while (0)
|
||||
370
tests/signal.cpp
370
tests/signal.cpp
|
|
@ -1,370 +0,0 @@
|
|||
#include <any>
|
||||
#include <hyprutils/signal/Signal.hpp>
|
||||
#include <hyprutils/memory/WeakPtr.hpp>
|
||||
#include <memory>
|
||||
#include "hyprutils/memory/SharedPtr.hpp"
|
||||
#include "hyprutils/signal/Listener.hpp"
|
||||
#include "shared.hpp"
|
||||
|
||||
using namespace Hyprutils::Signal;
|
||||
using namespace Hyprutils::Memory;
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
//
|
||||
|
||||
void legacy(int& ret) {
|
||||
CSignal signal;
|
||||
int data = 0;
|
||||
auto listener = signal.registerListener([&]([[maybe_unused]] std::any d) { data = 1; });
|
||||
|
||||
signal.emit();
|
||||
|
||||
EXPECT(data, 1);
|
||||
|
||||
data = 0;
|
||||
|
||||
listener.reset();
|
||||
|
||||
signal.emit();
|
||||
|
||||
EXPECT(data, 0);
|
||||
}
|
||||
|
||||
void legacyListenerEmit(int& ret) {
|
||||
int data = 0;
|
||||
CSignal signal;
|
||||
auto listener = signal.registerListener([&](std::any d) { data = std::any_cast<int>(d); });
|
||||
|
||||
listener->emit(1); // not a typo
|
||||
EXPECT(data, 1);
|
||||
}
|
||||
|
||||
void legacyListeners(int& ret) {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<> signal0;
|
||||
CSignalT<int> signal1;
|
||||
|
||||
auto listener0 = signal0.registerListener([&](std::any d) { data += 1; });
|
||||
auto listener1 = signal1.registerListener([&](std::any d) { data += std::any_cast<int>(d); });
|
||||
|
||||
signal0.registerStaticListener([&](void* o, std::any d) { data += 10; }, nullptr);
|
||||
signal1.registerStaticListener([&](void* o, std::any d) { data += std::any_cast<int>(d) * 10; }, nullptr);
|
||||
|
||||
signal0.emit();
|
||||
signal1.emit(2);
|
||||
|
||||
EXPECT(data, 33);
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
//
|
||||
|
||||
void empty(int& ret) {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<> signal;
|
||||
auto listener = signal.listen([&] { data = 1; });
|
||||
|
||||
signal.emit();
|
||||
EXPECT(data, 1);
|
||||
|
||||
data = 0;
|
||||
listener.reset();
|
||||
signal.emit();
|
||||
EXPECT(data, 0);
|
||||
}
|
||||
|
||||
void typed(int& ret) {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<int> signal;
|
||||
auto listener = signal.listen([&](int newData) { data = newData; });
|
||||
|
||||
signal.emit(1);
|
||||
EXPECT(data, 1);
|
||||
}
|
||||
|
||||
void ignoreParams(int& ret) {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<int> signal;
|
||||
auto listener = signal.listen([&] { data += 1; });
|
||||
|
||||
signal.listenStatic([&] { data += 1; });
|
||||
|
||||
signal.emit(2);
|
||||
EXPECT(data, 2);
|
||||
}
|
||||
|
||||
void typedMany(int& ret) {
|
||||
int data1 = 0;
|
||||
int data2 = 0;
|
||||
int data3 = 0;
|
||||
|
||||
CSignalT<int, int, int> signal;
|
||||
auto listener = signal.listen([&](int d1, int d2, int d3) {
|
||||
data1 = d1;
|
||||
data2 = d2;
|
||||
data3 = d3;
|
||||
});
|
||||
|
||||
signal.emit(1, 2, 3);
|
||||
EXPECT(data1, 1);
|
||||
EXPECT(data2, 2);
|
||||
EXPECT(data3, 3);
|
||||
}
|
||||
|
||||
void ref(int& ret) {
|
||||
int count = 0;
|
||||
int data = 0;
|
||||
|
||||
CSignalT<int&> signal;
|
||||
auto l1 = signal.listen([&](int& v) { v += 1; });
|
||||
auto l2 = signal.listen([&](int v) { count += v; });
|
||||
signal.emit(data);
|
||||
|
||||
CSignalT<const int&> constSignal;
|
||||
auto l3 = constSignal.listen([&](const int& v) { count += v; });
|
||||
auto l4 = constSignal.listen([&](int v) { count += v; });
|
||||
constSignal.emit(data);
|
||||
|
||||
EXPECT(data, 1);
|
||||
EXPECT(count, 3);
|
||||
}
|
||||
|
||||
void refMany(int& ret) {
|
||||
int count = 0;
|
||||
int data1 = 0;
|
||||
int data2 = 10;
|
||||
|
||||
CSignalT<int&, const int&> signal;
|
||||
auto l1 = signal.listen([&](int& v, const int&) { v += 1; });
|
||||
auto l2 = signal.listen([&](int v1, int v2) { count += v1 + v2; });
|
||||
|
||||
signal.emit(data1, data2);
|
||||
EXPECT(data1, 1);
|
||||
EXPECT(count, 11);
|
||||
}
|
||||
|
||||
void autoRefTypes(int& ret) {
|
||||
class CCopyCounter {
|
||||
public:
|
||||
CCopyCounter(int& createCount, int& destroyCount) : createCount(createCount), destroyCount(destroyCount) {
|
||||
createCount += 1;
|
||||
}
|
||||
|
||||
CCopyCounter(CCopyCounter&& other) noexcept : CCopyCounter(other.createCount, other.destroyCount) {}
|
||||
CCopyCounter(const CCopyCounter& other) noexcept : CCopyCounter(other.createCount, other.destroyCount) {}
|
||||
|
||||
~CCopyCounter() {
|
||||
destroyCount += 1;
|
||||
}
|
||||
|
||||
private:
|
||||
int& createCount;
|
||||
int& destroyCount;
|
||||
};
|
||||
|
||||
auto createCount = 0;
|
||||
auto destroyCount = 0;
|
||||
|
||||
CSignalT<CCopyCounter> signal;
|
||||
auto listener = signal.listen([](const CCopyCounter& counter) {});
|
||||
|
||||
signal.emit(CCopyCounter(createCount, destroyCount));
|
||||
EXPECT(createCount, 1);
|
||||
EXPECT(destroyCount, 1);
|
||||
}
|
||||
|
||||
void forward(int& ret) {
|
||||
int count = 0;
|
||||
|
||||
CSignalT<int> sig;
|
||||
CSignalT<int> connected1;
|
||||
CSignalT<> connected2;
|
||||
|
||||
auto conn1 = sig.forward(connected1);
|
||||
auto conn2 = sig.forward(connected2);
|
||||
|
||||
auto listener1 = connected1.listen([&](int v) { count += v; });
|
||||
auto listener2 = connected2.listen([&] { count += 1; });
|
||||
|
||||
sig.emit(2);
|
||||
|
||||
EXPECT(count, 3);
|
||||
}
|
||||
|
||||
void listenerAdded(int& ret) {
|
||||
int count = 0;
|
||||
|
||||
CSignalT<> signal;
|
||||
CHyprSignalListener secondListener;
|
||||
|
||||
auto listener = signal.listen([&] {
|
||||
count += 1;
|
||||
|
||||
if (!secondListener)
|
||||
secondListener = signal.listen([&] { count += 1; });
|
||||
});
|
||||
|
||||
signal.emit();
|
||||
EXPECT(count, 1); // second should NOT be invoked as it was registed during emit
|
||||
|
||||
signal.emit();
|
||||
EXPECT(count, 3); // second should be invoked
|
||||
}
|
||||
|
||||
void lastListenerSwapped(int& ret) {
|
||||
int count = 0;
|
||||
|
||||
CSignalT<> signal;
|
||||
CHyprSignalListener removedListener;
|
||||
CHyprSignalListener addedListener;
|
||||
|
||||
auto firstListener = signal.listen([&] {
|
||||
removedListener.reset(); // dropped and should NOT be invoked
|
||||
|
||||
if (!addedListener)
|
||||
addedListener = signal.listen([&] { count += 2; });
|
||||
});
|
||||
|
||||
removedListener = signal.listen([&] { count += 1; });
|
||||
|
||||
signal.emit();
|
||||
EXPECT(count, 0); // neither the removed nor added listeners should fire
|
||||
|
||||
signal.emit();
|
||||
EXPECT(count, 2); // only the new listener should fire
|
||||
}
|
||||
|
||||
void signalDestroyed(int& ret) {
|
||||
int count = 0;
|
||||
|
||||
auto signal = std::make_unique<CSignalT<>>();
|
||||
|
||||
// This ensures a destructor of a listener called before signal reset is safe.
|
||||
auto preListener = signal->listen([&] { count += 1; });
|
||||
|
||||
auto listener = signal->listen([&] { signal.reset(); });
|
||||
|
||||
// This ensures a destructor of a listener called after signal reset is safe
|
||||
// and gets called.
|
||||
auto postListener = signal->listen([&] { count += 1; });
|
||||
|
||||
signal->emit();
|
||||
EXPECT(count, 2); // all listeners should fire regardless of signal deletion
|
||||
}
|
||||
|
||||
// purely an asan test
|
||||
void signalDestroyedBeforeListener() {
|
||||
CHyprSignalListener listener1;
|
||||
CHyprSignalListener listener2;
|
||||
|
||||
CSignalT<> signal;
|
||||
|
||||
listener1 = signal.listen([] {});
|
||||
listener2 = signal.listen([] {});
|
||||
}
|
||||
|
||||
void signalDestroyedWithAddedListener(int& ret) {
|
||||
int count = 0;
|
||||
|
||||
auto signal = std::make_unique<CSignalT<>>();
|
||||
CHyprSignalListener shouldNotRun;
|
||||
|
||||
auto listener = signal->listen([&] {
|
||||
shouldNotRun = signal->listen([&] { count += 2; });
|
||||
signal.reset();
|
||||
});
|
||||
|
||||
signal->emit();
|
||||
EXPECT(count, 0);
|
||||
}
|
||||
|
||||
void signalDestroyedWithRemovedAndAddedListener(int& ret) {
|
||||
int count = 0;
|
||||
|
||||
auto signal = std::make_unique<CSignalT<>>();
|
||||
CHyprSignalListener removed;
|
||||
CHyprSignalListener shouldNotRun;
|
||||
|
||||
auto listener = signal->listen([&] {
|
||||
removed.reset();
|
||||
shouldNotRun = signal->listen([&] { count += 2; });
|
||||
signal.reset();
|
||||
});
|
||||
|
||||
removed = signal->listen([&] { count += 1; });
|
||||
|
||||
signal->emit();
|
||||
EXPECT(count, 0);
|
||||
}
|
||||
|
||||
void staticListener(int& ret) {
|
||||
int data = 0;
|
||||
|
||||
CSignalT<int> signal;
|
||||
signal.listenStatic([&](int newData) { data = newData; });
|
||||
|
||||
signal.emit(1);
|
||||
EXPECT(data, 1);
|
||||
}
|
||||
|
||||
void staticListenerDestroy(int& ret) {
|
||||
int count = 0;
|
||||
|
||||
auto signal = makeShared<CSignalT<>>();
|
||||
signal->listenStatic([&] { count += 1; });
|
||||
|
||||
signal->listenStatic([&] {
|
||||
// should not fire but SHOULD be freed
|
||||
signal->listenStatic([&] { count += 3; });
|
||||
|
||||
signal.reset();
|
||||
});
|
||||
|
||||
signal->listenStatic([&] { count += 1; });
|
||||
|
||||
signal->emit();
|
||||
EXPECT(count, 2);
|
||||
}
|
||||
|
||||
// purely an asan test
|
||||
void listenerDestroysSelf() {
|
||||
CSignalT<> signal;
|
||||
|
||||
CHyprSignalListener listener;
|
||||
listener = signal.listen([&] { listener.reset(); });
|
||||
|
||||
// the static signal case is taken care of above
|
||||
|
||||
signal.emit();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
int ret = 0;
|
||||
legacy(ret);
|
||||
legacyListenerEmit(ret);
|
||||
legacyListeners(ret);
|
||||
empty(ret);
|
||||
typed(ret);
|
||||
ignoreParams(ret);
|
||||
typedMany(ret);
|
||||
ref(ret);
|
||||
refMany(ret);
|
||||
autoRefTypes(ret);
|
||||
forward(ret);
|
||||
listenerAdded(ret);
|
||||
lastListenerSwapped(ret);
|
||||
signalDestroyed(ret);
|
||||
signalDestroyedBeforeListener();
|
||||
signalDestroyedWithAddedListener(ret);
|
||||
signalDestroyedWithRemovedAndAddedListener(ret);
|
||||
staticListener(ret);
|
||||
staticListenerDestroy(ret);
|
||||
signalDestroyed(ret);
|
||||
listenerDestroysSelf();
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
#include <hyprutils/string/String.hpp>
|
||||
#include <hyprutils/string/VarList.hpp>
|
||||
#include <hyprutils/string/ConstVarList.hpp>
|
||||
#include "shared.hpp"
|
||||
|
||||
using namespace Hyprutils::String;
|
||||
|
||||
int main(int argc, char** argv, char** envp) {
|
||||
int ret = 0;
|
||||
|
||||
EXPECT(trim(" a "), "a");
|
||||
EXPECT(trim(" a a "), "a a");
|
||||
EXPECT(trim("a"), "a");
|
||||
EXPECT(trim(" "), "");
|
||||
|
||||
EXPECT(isNumber("99214123434"), true);
|
||||
EXPECT(isNumber("-35252345234"), true);
|
||||
EXPECT(isNumber("---3423--432"), false);
|
||||
EXPECT(isNumber("s---3423--432"), false);
|
||||
EXPECT(isNumber("---3423--432s"), false);
|
||||
EXPECT(isNumber("1s"), false);
|
||||
EXPECT(isNumber(""), false);
|
||||
EXPECT(isNumber("-"), false);
|
||||
EXPECT(isNumber("--0"), false);
|
||||
EXPECT(isNumber("abc"), false);
|
||||
EXPECT(isNumber("0.0", true), true);
|
||||
EXPECT(isNumber("0.2", true), true);
|
||||
EXPECT(isNumber("0.", true), false);
|
||||
EXPECT(isNumber(".0", true), false);
|
||||
EXPECT(isNumber("", true), false);
|
||||
EXPECT(isNumber("vvss", true), false);
|
||||
EXPECT(isNumber("0.9999s", true), false);
|
||||
EXPECT(isNumber("s0.9999", true), false);
|
||||
EXPECT(isNumber("-1.0", true), true);
|
||||
EXPECT(isNumber("-1..0", true), false);
|
||||
EXPECT(isNumber("-10.0000000001", true), true);
|
||||
|
||||
CVarList list("hello world!", 0, 's', true);
|
||||
EXPECT(list[0], "hello");
|
||||
EXPECT(list[1], "world!");
|
||||
|
||||
CConstVarList listConst("hello world!", 0, 's', true);
|
||||
EXPECT(listConst[0], "hello");
|
||||
EXPECT(listConst[1], "world!");
|
||||
|
||||
std::string hello = "hello world!";
|
||||
replaceInString(hello, "hello", "hi");
|
||||
EXPECT(hello, "hi world!");
|
||||
|
||||
return ret;
|
||||
}
|
||||
Loading…
Reference in New Issue