Tests 1/2
This commit is contained in:
13
.clang-format
Normal file
13
.clang-format
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
---
|
||||||
|
BasedOnStyle: Microsoft
|
||||||
|
IndentWidth: 4
|
||||||
|
SortIncludes: false
|
||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
IndentPPDirectives: AfterHash
|
||||||
|
FixNamespaceComments: true
|
||||||
|
NamespaceIndentation: All
|
||||||
|
SeparateDefinitionBlocks: Always
|
||||||
|
SpaceAfterCStyleCast: true
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: false
|
||||||
50
.gitignore
vendored
Normal file
50
.gitignore
vendored
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# ---> C++
|
||||||
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Compiled Object files
|
||||||
|
*.slo
|
||||||
|
*.lo
|
||||||
|
*.o
|
||||||
|
*.obj
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Compiled Dynamic libraries
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
*.dll
|
||||||
|
|
||||||
|
# Fortran module files
|
||||||
|
*.mod
|
||||||
|
*.smod
|
||||||
|
|
||||||
|
# Compiled Static libraries
|
||||||
|
*.lai
|
||||||
|
*.la
|
||||||
|
*.a
|
||||||
|
*.lib
|
||||||
|
|
||||||
|
# Executables
|
||||||
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
|
||||||
|
# ---> VisualStudioCode
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/settings.json
|
||||||
|
!.vscode/tasks.json
|
||||||
|
!.vscode/launch.json
|
||||||
|
!.vscode/extensions.json
|
||||||
|
!.vscode/*.code-snippets
|
||||||
|
|
||||||
|
# Local History for Visual Studio Code
|
||||||
|
.history/
|
||||||
|
|
||||||
|
# Built Visual Studio Code Extensions
|
||||||
|
*.vsix
|
||||||
|
|
||||||
|
.cache/
|
||||||
|
Build/
|
||||||
9
.gitmodules
vendored
Normal file
9
.gitmodules
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[submodule "Vendor/mimalloc"]
|
||||||
|
path = Vendor/mimalloc
|
||||||
|
url = https://github.com/microsoft/mimalloc
|
||||||
|
[submodule "Vendor/expected"]
|
||||||
|
path = Vendor/expected
|
||||||
|
url = https://github.com/TartanLlama/expected
|
||||||
|
[submodule "Vendor/unordered_dense"]
|
||||||
|
path = Vendor/unordered_dense
|
||||||
|
url = https://github.com/martinus/unordered_dense
|
||||||
13
.vscode/tasks.json
vendored
Normal file
13
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "build",
|
||||||
|
"type": "shell",
|
||||||
|
"command": "python3 Tools/Builder/Build.py",
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
47
CMakeLists.txt
Normal file
47
CMakeLists.txt
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.28 FATAL_ERROR)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
|
||||||
|
project(IACore)
|
||||||
|
|
||||||
|
message(STATUS "Detected Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
|
||||||
|
# Check if the compiler is MSVC (cl.exe), but allow Clang acting like MSVC (clang-cl)
|
||||||
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||||
|
# Note: clang-cl usually reports itself as "Clang" in newer CMake versions,
|
||||||
|
# but if it reports MSVC with a Clang simulation, we want to allow it.
|
||||||
|
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||||
|
message(FATAL_ERROR
|
||||||
|
"\n\n"
|
||||||
|
"-------------------------------------------------------------\n"
|
||||||
|
"CRITICAL ERROR: Unsupported Compiler Detected (MSVC/cl.exe)\n"
|
||||||
|
"-------------------------------------------------------------\n"
|
||||||
|
"IACore requires GCC or Clang to compile.\n"
|
||||||
|
"For compiling with IACore on Windows, please use Clang Tools for MSVC.\n"
|
||||||
|
"-------------------------------------------------------------\n"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
||||||
|
add_compile_options(
|
||||||
|
-Wall -Wextra -Wpedantic
|
||||||
|
# Suppress warning for the statement expression macro if -pedantic is on
|
||||||
|
-Wno-language-extension-token
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
||||||
|
|
||||||
|
add_subdirectory(Vendor/)
|
||||||
|
|
||||||
|
add_subdirectory(Src/)
|
||||||
|
|
||||||
|
if(IA_BUILD_TESTS)
|
||||||
|
add_subdirectory(Tests/)
|
||||||
|
endif()
|
||||||
232
LICENSE
Normal file
232
LICENSE
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU General Public License is a free, copyleft license for software and other kinds of works.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights.
|
||||||
|
|
||||||
|
Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it.
|
||||||
|
|
||||||
|
For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions.
|
||||||
|
|
||||||
|
Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users.
|
||||||
|
|
||||||
|
Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
“This License” refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
“Copyright” also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
|
||||||
|
|
||||||
|
“The Program” refers to any copyrightable work licensed under this License. Each licensee is addressed as “you”. “Licensees” and “recipients” may be individuals or organizations.
|
||||||
|
|
||||||
|
To “modify” a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a “modified version” of the earlier work or a work “based on” the earlier work.
|
||||||
|
|
||||||
|
A “covered work” means either the unmodified Program or a work based on the Program.
|
||||||
|
|
||||||
|
To “propagate” a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To “convey” a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays “Appropriate Legal Notices” to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.
|
||||||
|
|
||||||
|
A “Standard Interface” means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The “System Libraries” of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A “Major Component”, in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The “Corresponding Source” for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to “keep intact all notices”.
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an “aggregate” if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work.
|
||||||
|
|
||||||
|
A “User Product” is either (1) a “consumer product”, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, “normally used” refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product.
|
||||||
|
|
||||||
|
“Installation Information” for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
“Additional permissions” are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered “further restrictions” within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An “entity transaction” is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
A “contributor” is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's “contributor version”.
|
||||||
|
|
||||||
|
A contributor's “essential patent claims” are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, “control” includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a “patent license” is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To “grant” such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. “Knowingly relying” means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it.
|
||||||
|
|
||||||
|
A patent license is “discriminatory” if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Use with the GNU Affero General Public License.
|
||||||
|
Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
IACore
|
||||||
|
Copyright (C) 2025 dev0
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
|
IACore Copyright (C) 2025 dev0
|
||||||
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an “about box”.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school, if any, to sign a “copyright disclaimer” for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read <https://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||||
2
Src/CMakeLists.txt
Normal file
2
Src/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
add_subdirectory(IACore/)
|
||||||
29
Src/IACore/CMakeLists.txt
Normal file
29
Src/IACore/CMakeLists.txt
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
set(SRC_FILES
|
||||||
|
"imp/cpp/IACore.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
add_library(IACore STATIC ${SRC_FILES})
|
||||||
|
|
||||||
|
target_include_directories(IACore PUBLIC inc/)
|
||||||
|
target_include_directories(IACore PRIVATE imp/hpp/)
|
||||||
|
|
||||||
|
target_link_libraries(IACore PUBLIC tl::expected unordered_dense::unordered_dense)
|
||||||
|
|
||||||
|
if(WIN32)
|
||||||
|
target_link_libraries(IACore PUBLIC mimalloc-static)
|
||||||
|
target_link_options(IACore PUBLIC "/INCLUDE:mi_version")
|
||||||
|
else()
|
||||||
|
target_link_libraries(IACore PUBLIC mimalloc)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
target_precompile_headers(IACore PUBLIC inc/IACore/PCH.hpp)
|
||||||
|
|
||||||
|
target_compile_options(IACore PRIVATE -fno-exceptions)
|
||||||
|
target_compile_options(IACore INTERFACE
|
||||||
|
$<$<NOT:$<BOOL:$<TARGET_PROPERTY:USE_EXCEPTIONS>>>:-fno-exceptions>
|
||||||
|
)
|
||||||
|
|
||||||
|
define_property(TARGET PROPERTY USE_EXCEPTIONS
|
||||||
|
BRIEF_DOCS "If ON, this target is allowed to use C++ exceptions."
|
||||||
|
FULL_DOCS "Prevents IACore from propagating -fno-exceptions to this target."
|
||||||
|
)
|
||||||
24
Src/IACore/imp/cpp/IACore.cpp
Normal file
24
Src/IACore/imp/cpp/IACore.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <IACore/IACore.hpp>
|
||||||
|
|
||||||
|
#include <mimalloc-new-delete.h>
|
||||||
|
|
||||||
|
namespace IACore
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
101
Src/IACore/inc/IACore/BinaryReader.hpp
Normal file
101
Src/IACore/inc/IACore/BinaryReader.hpp
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
namespace IACore {
|
||||||
|
|
||||||
|
class BinaryReader {
|
||||||
|
public:
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Construction (Zero Copy)
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Accepts Vector<UINT8>, std::array, or C-arrays automatically
|
||||||
|
BinaryReader(std::span<const UINT8> data)
|
||||||
|
: m_span(data), m_cursor(0) {}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Core API
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Generic Primitive Reader (Read<UINT32>(), Read<FLOAT32>(), etc.)
|
||||||
|
template <typename T>
|
||||||
|
NO_DISCARD("Check for EOF")
|
||||||
|
tl::expected<T, String> Read() {
|
||||||
|
constexpr SIZE_T size = sizeof(T);
|
||||||
|
|
||||||
|
if B_UNLIKELY((m_cursor + size > m_span.size())) {
|
||||||
|
return tl::make_unexpected(String("Unexpected EOF reading type"));
|
||||||
|
}
|
||||||
|
|
||||||
|
T value;
|
||||||
|
// SAFE: memcpy handles alignment issues on ARM/Android automatically.
|
||||||
|
// Modern compilers optimize this into a single register load instruction.
|
||||||
|
std::memcpy(&value, &m_span[m_cursor], size);
|
||||||
|
|
||||||
|
m_cursor += size;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Batch Read (Copy to external buffer)
|
||||||
|
tl::expected<void, String> Read(PVOID buffer, SIZE_T size) {
|
||||||
|
if B_UNLIKELY((m_cursor + size > m_span.size())) {
|
||||||
|
return tl::make_unexpected(String("Unexpected EOF reading buffer"));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::memcpy(buffer, &m_span[m_cursor], size);
|
||||||
|
m_cursor += size;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// String Reader (Null Terminated or Length Prefixed)
|
||||||
|
tl::expected<String, String> ReadString(SIZE_T length) {
|
||||||
|
if B_UNLIKELY((m_cursor + length > m_span.size())) {
|
||||||
|
return tl::make_unexpected(String("Unexpected EOF reading string"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create string from current pointer
|
||||||
|
String str(REINTERPRET(&m_span[m_cursor], const char*), length);
|
||||||
|
m_cursor += length;
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Navigation
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
VOID Skip(SIZE_T amount) {
|
||||||
|
// Clamp to end
|
||||||
|
m_cursor = std::min(m_cursor + amount, m_span.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
VOID Seek(SIZE_T pos) {
|
||||||
|
if (pos > m_span.size()) m_cursor = m_span.size();
|
||||||
|
else m_cursor = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
SIZE_T Cursor() CONST { return m_cursor; }
|
||||||
|
SIZE_T Remaining() CONST { return m_span.size() - m_cursor; }
|
||||||
|
BOOL IsEOF() CONST { return m_cursor >= m_span.size(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::span<const UINT8> m_span;
|
||||||
|
SIZE_T m_cursor;
|
||||||
|
};
|
||||||
|
}
|
||||||
100
Src/IACore/inc/IACore/BinaryWriter.hpp
Normal file
100
Src/IACore/inc/IACore/BinaryWriter.hpp
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
namespace IACore {
|
||||||
|
|
||||||
|
class BinaryWriter {
|
||||||
|
public:
|
||||||
|
// Mode 1: Append to a Vector (Growing)
|
||||||
|
BinaryWriter(Vector<UINT8>& target) : m_buffer(&target), m_span({}), m_cursor(target.size()), m_isVector(true) {}
|
||||||
|
|
||||||
|
// Mode 2: Write into existing fixed memory (No allocs)
|
||||||
|
BinaryWriter(std::span<UINT8> target) : m_buffer(nullptr), m_span(target), m_cursor(0), m_isVector(false) {}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Core API
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Append T (Handling Endianness automatically if needed)
|
||||||
|
template <typename T>
|
||||||
|
VOID Write(T value) {
|
||||||
|
// If we are Little Endian (x86/ARM), this compiles to a raw MOV/memcpy.
|
||||||
|
// If we need specific Endianness (Network Byte Order), use std::byteswap (C++23)
|
||||||
|
// or a simple swap helper.
|
||||||
|
// For a game engine, we usually stick to Native Endian (LE) for speed.
|
||||||
|
|
||||||
|
CONST SIZE_T size = sizeof(T);
|
||||||
|
|
||||||
|
if (m_isVector) {
|
||||||
|
// Vector guarantees contiguous memory, but push_back is slow for primitives.
|
||||||
|
// We resize and memcpy.
|
||||||
|
SIZE_T currentPos = m_buffer->size();
|
||||||
|
m_buffer->resize(currentPos + size);
|
||||||
|
std::memcpy(m_buffer->data() + currentPos, &value, size);
|
||||||
|
} else {
|
||||||
|
// Fixed Buffer Safety Check
|
||||||
|
if B_UNLIKELY((m_cursor + size > m_span.size())) {
|
||||||
|
IA_PANIC("BinaryWriter Overflow");
|
||||||
|
}
|
||||||
|
std::memcpy(m_span.data() + m_cursor, &value, size);
|
||||||
|
m_cursor += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Random Access Write (Replaces put32, put16, etc.)
|
||||||
|
template <typename T>
|
||||||
|
VOID WriteAt(SIZE_T position, T value) {
|
||||||
|
PUINT8 ptr = GetPtrAt(position);
|
||||||
|
if (ptr) {
|
||||||
|
std::memcpy(ptr, &value, sizeof(T));
|
||||||
|
} else {
|
||||||
|
IA_PANIC("BinaryWriter Out of Bounds Write");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VOID WriteBytes(CONST PVOID data, SIZE_T size) {
|
||||||
|
if (m_isVector) {
|
||||||
|
SIZE_T currentPos = m_buffer->size();
|
||||||
|
m_buffer->resize(currentPos + size);
|
||||||
|
std::memcpy(m_buffer->data() + currentPos, data, size);
|
||||||
|
} else {
|
||||||
|
if B_UNLIKELY((m_cursor + size > m_span.size())) IA_PANIC("Overflow");
|
||||||
|
std::memcpy(m_span.data() + m_cursor, data, size);
|
||||||
|
m_cursor += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PUINT8 GetPtrAt(SIZE_T pos) {
|
||||||
|
if (m_isVector) {
|
||||||
|
if (pos >= m_buffer->size()) return nullptr;
|
||||||
|
return m_buffer->data() + pos;
|
||||||
|
} else {
|
||||||
|
if (pos >= m_span.size()) return nullptr;
|
||||||
|
return m_span.data() + pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector<UINT8>* m_buffer;
|
||||||
|
std::span<UINT8> m_span;
|
||||||
|
SIZE_T m_cursor;
|
||||||
|
BOOL m_isVector;
|
||||||
|
};
|
||||||
|
}
|
||||||
184
Src/IACore/inc/IACore/DynamicLib.hpp
Normal file
184
Src/IACore/inc/IACore/DynamicLib.hpp
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
#include <libloaderapi.h>
|
||||||
|
#include <errhandlingapi.h>
|
||||||
|
#else
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace IACore {
|
||||||
|
|
||||||
|
class DynamicLib {
|
||||||
|
public:
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Constructors / Destructors (Move Only)
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
DynamicLib() : m_handle(nullptr) {}
|
||||||
|
|
||||||
|
// Move Constructor: Steal ownership, nullify source
|
||||||
|
DynamicLib(DynamicLib&& other) NOEXCEPT : m_handle(other.m_handle) {
|
||||||
|
other.m_handle = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move Assignment
|
||||||
|
DynamicLib& operator=(DynamicLib&& other) NOEXCEPT {
|
||||||
|
if (this != &other) {
|
||||||
|
Unload(); // Free current if exists
|
||||||
|
m_handle = other.m_handle;
|
||||||
|
other.m_handle = nullptr;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No Copying allowed (Library handles are unique resources)
|
||||||
|
DynamicLib(CONST DynamicLib&) = delete;
|
||||||
|
DynamicLib& operator=(CONST DynamicLib&) = delete;
|
||||||
|
|
||||||
|
~DynamicLib() {
|
||||||
|
Unload();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Static Loader
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Automatically detects extension (.dll/.so) if not provided
|
||||||
|
NO_DISCARD("Check for load errors")
|
||||||
|
STATIC tl::expected<DynamicLib, String> Load(CONST String& searchPath, CONST String& name) {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
// 1. Build Path safely
|
||||||
|
fs::path fullPath = fs::path(searchPath) / name;
|
||||||
|
|
||||||
|
// 2. Auto-append extension if missing
|
||||||
|
if (!fullPath.has_extension()) {
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
fullPath += ".dll";
|
||||||
|
#elif IA_PLATFORM_MAC
|
||||||
|
fullPath += ".dylib";
|
||||||
|
#else
|
||||||
|
fullPath += ".so";
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
DynamicLib lib;
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
// Use LoadLibraryA (ANSI/UTF-8) assuming manifest is set for UTF-8
|
||||||
|
HMODULE h = LoadLibraryA(fullPath.string().c_str());
|
||||||
|
if (!h) {
|
||||||
|
return tl::make_unexpected(GetWindowsError());
|
||||||
|
}
|
||||||
|
lib.m_handle = CAST(h, PVOID);
|
||||||
|
#else
|
||||||
|
// RTLD_LAZY: Resolve symbols only as code executes (Standard for plugins)
|
||||||
|
// RTLD_NOW: Resolve all immediately (Safer for strict engines)
|
||||||
|
void* h = dlopen(fullPath.c_str(), RTLD_LAZY | RTLD_LOCAL);
|
||||||
|
if (!h) {
|
||||||
|
// dlerror returns a string describing the last error
|
||||||
|
const char* err = dlerror();
|
||||||
|
return tl::make_unexpected(String(err ? err : "Unknown dlopen error"));
|
||||||
|
}
|
||||||
|
lib.m_handle = h;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return IA_MOVE(lib);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Symbol Access
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
NO_DISCARD("Check if symbol exists")
|
||||||
|
tl::expected<PVOID, String> GetSymbol(CONST String& name) CONST {
|
||||||
|
if (!m_handle) return tl::make_unexpected(String("Library not loaded"));
|
||||||
|
|
||||||
|
PVOID sym = nullptr;
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
sym = CAST(GetProcAddress(CAST(m_handle, HMODULE), name.c_str()), PVOID);
|
||||||
|
if (!sym) return tl::make_unexpected(GetWindowsError());
|
||||||
|
#else
|
||||||
|
// Clear any previous error
|
||||||
|
dlerror();
|
||||||
|
sym = dlsym(m_handle, name.c_str());
|
||||||
|
const char* err = dlerror();
|
||||||
|
if (err) return tl::make_unexpected(String(err));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return sym;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Template helper for casting
|
||||||
|
template<typename FuncT>
|
||||||
|
tl::expected<FuncT, String> GetFunction(CONST String& name) CONST {
|
||||||
|
auto res = GetSymbol(name);
|
||||||
|
if (!res) return tl::make_unexpected(res.error());
|
||||||
|
return REINTERPRET(*res, FuncT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// State Management
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
VOID Unload() {
|
||||||
|
if (m_handle) {
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
FreeLibrary(CAST(m_handle, HMODULE));
|
||||||
|
#else
|
||||||
|
dlclose(m_handle);
|
||||||
|
#endif
|
||||||
|
m_handle = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL IsLoaded() CONST {
|
||||||
|
return m_handle != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
PVOID m_handle;
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Private Helpers
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
STATIC String GetWindowsError() {
|
||||||
|
DWORD errorID = ::GetLastError();
|
||||||
|
if(errorID == 0) return String();
|
||||||
|
|
||||||
|
LPSTR messageBuffer = nullptr;
|
||||||
|
size_t size = FormatMessageA(
|
||||||
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL, errorID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||||
|
(LPSTR)&messageBuffer, 0, NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
String message(messageBuffer, size);
|
||||||
|
LocalFree(messageBuffer);
|
||||||
|
return String("Win32 Error: ") + message;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
}
|
||||||
123
Src/IACore/inc/IACore/Environment.hpp
Normal file
123
Src/IACore/inc/IACore/Environment.hpp
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
namespace IACore
|
||||||
|
{
|
||||||
|
class Environment
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Getters
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Modern approach: Returns nullopt if variable doesn't exist
|
||||||
|
STATIC std::optional<String> Find(CONST String &name)
|
||||||
|
{
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
// 1. Get required size (result includes null terminator if buffer is null/too small)
|
||||||
|
DWORD bufferSize = GetEnvironmentVariableA(name.c_str(), nullptr, 0);
|
||||||
|
|
||||||
|
if (bufferSize == 0)
|
||||||
|
{
|
||||||
|
// DWORD 0 means failed (usually ERROR_ENVVAR_NOT_FOUND)
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Allocate (bufferSize includes the null terminator request)
|
||||||
|
String result;
|
||||||
|
result.resize(bufferSize);
|
||||||
|
|
||||||
|
// 3. Fetch
|
||||||
|
// Returns num chars written EXCLUDING null terminator
|
||||||
|
DWORD actualSize = GetEnvironmentVariableA(name.c_str(), result.data(), bufferSize);
|
||||||
|
|
||||||
|
if (actualSize == 0 || actualSize > bufferSize)
|
||||||
|
{
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resize down to exclude the null terminator and any slack
|
||||||
|
result.resize(actualSize);
|
||||||
|
return result;
|
||||||
|
|
||||||
|
#else
|
||||||
|
// POSIX (Linux/Mac)
|
||||||
|
// getenv returns a pointer to the environment area. Do NOT free it.
|
||||||
|
const char *val = std::getenv(name.c_str());
|
||||||
|
if (val == nullptr)
|
||||||
|
{
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return String(val);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Classic approach: Returns value or default (defaulting to "")
|
||||||
|
// Matches your old API behavior but is safer
|
||||||
|
STATIC String Get(CONST String &name, CONST String &defaultValue = "")
|
||||||
|
{
|
||||||
|
return Find(name).value_or(defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Setters
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
STATIC BOOL Set(CONST String &name, CONST String &value)
|
||||||
|
{
|
||||||
|
if (name.empty())
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
return SetEnvironmentVariableA(name.c_str(), value.c_str()) != 0;
|
||||||
|
#else
|
||||||
|
// setenv(name, value, overwrite)
|
||||||
|
// Returns 0 on success, -1 on error
|
||||||
|
return setenv(name.c_str(), value.c_str(), 1) == 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC BOOL Unset(CONST String &name)
|
||||||
|
{
|
||||||
|
if (name.empty())
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
// Windows unsets a variable by setting it to NULL
|
||||||
|
return SetEnvironmentVariableA(name.c_str(), nullptr) != 0;
|
||||||
|
#else
|
||||||
|
return unsetenv(name.c_str()) == 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Utilities
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
STATIC BOOL Exists(CONST String &name)
|
||||||
|
{
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
return GetEnvironmentVariableA(name.c_str(), nullptr, 0) > 0;
|
||||||
|
#else
|
||||||
|
return std::getenv(name.c_str()) != nullptr;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace IACore
|
||||||
174
Src/IACore/inc/IACore/File.hpp
Normal file
174
Src/IACore/inc/IACore/File.hpp
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
namespace IACore {
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
class File {
|
||||||
|
public:
|
||||||
|
// Modern mapping of flags to standard IO streams
|
||||||
|
enum class EOpenFlags : UINT32 {
|
||||||
|
Read = 1 << 0, // std::ios::in
|
||||||
|
Write = 1 << 1, // std::ios::out
|
||||||
|
Binary = 1 << 2, // std::ios::binary
|
||||||
|
Append = 1 << 3, // std::ios::app
|
||||||
|
Trunc = 1 << 4 // std::ios::trunc
|
||||||
|
};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Static Helper API (The "One-Liners")
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Reads entire file into a binary vector
|
||||||
|
NO_DISCARD("Handle the error")
|
||||||
|
STATIC tl::expected<Vector<UINT8>, String> ReadToVector(CONST String& path) {
|
||||||
|
// 1. Check File Existence & Size
|
||||||
|
std::error_code ec;
|
||||||
|
auto fileSize = fs::file_size(path, ec);
|
||||||
|
|
||||||
|
if (ec) {
|
||||||
|
return tl::make_unexpected(String("File not found or inaccessible: ") + path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Open Stream
|
||||||
|
std::ifstream file(path, std::ios::binary);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return tl::make_unexpected(String("Failed to open file handle: ") + path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Read
|
||||||
|
Vector<UINT8> buffer;
|
||||||
|
buffer.resize(CAST(fileSize, SIZE_T));
|
||||||
|
file.read(REINTERPRET(buffer.data(), char*), fileSize);
|
||||||
|
if(file.fail())
|
||||||
|
return tl::make_unexpected(String("Read error: ") + path);
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads entire file into a String
|
||||||
|
NO_DISCARD("Handle the error")
|
||||||
|
STATIC tl::expected<String, String> ReadToString(CONST String& path) {
|
||||||
|
// Reuse the binary logic to avoid code duplication, then cast
|
||||||
|
auto result = ReadToVector(path);
|
||||||
|
if (!result) {
|
||||||
|
return tl::make_unexpected(result.error());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Efficient move into string (reinterpret_cast approach or move)
|
||||||
|
// Since Vector<UINT8> and String memory layout isn't guaranteed identical, we copy.
|
||||||
|
// (Though in many STL implementations you could theoretically steal the buffer, copying is safer)
|
||||||
|
String str(result->begin(), result->end());
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Path Utilities (Replaces manual parsing)
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Old: ExtractFilenameFromPath<true/false>
|
||||||
|
// New: true -> filename(), false -> stem()
|
||||||
|
template<BOOL includeExtension = true>
|
||||||
|
STATIC String ExtractFilename(CONST String& pathStr) {
|
||||||
|
fs::path p(pathStr);
|
||||||
|
if CONSTEXPR (includeExtension) {
|
||||||
|
return p.filename().string(); // "sprite.png"
|
||||||
|
} else {
|
||||||
|
return p.stem().string(); // "sprite"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Old: ExtractDirectoryFromPath
|
||||||
|
STATIC String ExtractDirectory(CONST String& pathStr) {
|
||||||
|
fs::path p(pathStr);
|
||||||
|
return p.parent_path().string(); // "assets/textures"
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC BOOL Exists(CONST String& pathStr) {
|
||||||
|
std::error_code ec;
|
||||||
|
return fs::exists(pathStr, ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Instance API (For streaming/partial reads)
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
File() = default;
|
||||||
|
|
||||||
|
// RAII Constructor
|
||||||
|
File(CONST String& path, UINT32 flags) {
|
||||||
|
UNUSED(Open(path, flags));
|
||||||
|
}
|
||||||
|
|
||||||
|
~File() {
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns void (success) or error String
|
||||||
|
tl::expected<void, String> Open(CONST String& path, UINT32 flags) {
|
||||||
|
Close(); // Ensure previous handle is closed
|
||||||
|
|
||||||
|
std::ios_base::openmode mode = static_cast<std::ios_base::openmode>(0);
|
||||||
|
if (flags & (UINT32)EOpenFlags::Read) mode |= std::ios::in;
|
||||||
|
if (flags & (UINT32)EOpenFlags::Write) mode |= std::ios::out;
|
||||||
|
if (flags & (UINT32)EOpenFlags::Binary) mode |= std::ios::binary;
|
||||||
|
if (flags & (UINT32)EOpenFlags::Append) mode |= std::ios::app;
|
||||||
|
if (flags & (UINT32)EOpenFlags::Trunc) mode |= std::ios::trunc;
|
||||||
|
|
||||||
|
m_fs.open(path, mode);
|
||||||
|
|
||||||
|
if (!m_fs.is_open()) {
|
||||||
|
return tl::make_unexpected(String("Failed to open file: ") + path);
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
VOID Close() {
|
||||||
|
if (m_fs.is_open()) {
|
||||||
|
m_fs.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL IsOpen() CONST {
|
||||||
|
return m_fs.is_open();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns number of bytes read
|
||||||
|
SIZE_T Read(PVOID buffer, SIZE_T size) {
|
||||||
|
if (!m_fs.is_open()) return 0;
|
||||||
|
m_fs.read(REINTERPRET(buffer, char*), size);
|
||||||
|
return static_cast<SIZE_T>(m_fs.gcount());
|
||||||
|
}
|
||||||
|
|
||||||
|
VOID Write(CONST PVOID buffer, SIZE_T size) {
|
||||||
|
if (m_fs.is_open()) {
|
||||||
|
m_fs.write(REINTERPRET(buffer, const char*), size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::fstream m_fs;
|
||||||
|
};
|
||||||
|
}
|
||||||
28
Src/IACore/inc/IACore/IACore.hpp
Normal file
28
Src/IACore/inc/IACore/IACore.hpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
namespace IACore
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
261
Src/IACore/inc/IACore/IATest.hpp
Normal file
261
Src/IACore/inc/IACore/IATest.hpp
Normal file
@ -0,0 +1,261 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Macros
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define valid_iatest_runner(type) iatest::_valid_iatest_runner<type>::value_type
|
||||||
|
|
||||||
|
// Internal macro to handle the return logic
|
||||||
|
#define __iat_micro_test(call) \
|
||||||
|
if(!(call)) return FALSE
|
||||||
|
|
||||||
|
#define IAT_CHECK(v) __iat_micro_test(_test((v), #v))
|
||||||
|
#define IAT_CHECK_NOT(v) __iat_micro_test(_test_not((v), "NOT " #v))
|
||||||
|
#define IAT_CHECK_EQ(lhs, rhs) __iat_micro_test(_test_eq((lhs), (rhs), #lhs " == " #rhs))
|
||||||
|
#define IAT_CHECK_NEQ(lhs, rhs) __iat_micro_test(_test_neq((lhs), (rhs), #lhs " != " #rhs))
|
||||||
|
|
||||||
|
// Float specific checks (Game dev essential)
|
||||||
|
#define IAT_CHECK_APPROX(lhs, rhs) __iat_micro_test(_test_approx((lhs), (rhs), #lhs " ~= " #rhs))
|
||||||
|
|
||||||
|
#define IAT_UNIT(func) _test_unit([this](){ return this->func(); }, # func)
|
||||||
|
#define IAT_NAMED_UNIT(n, func) _test_unit([this](){ return this->func(); }, n)
|
||||||
|
|
||||||
|
#define IAT_BLOCK(name) class name: public ia::iatest::block
|
||||||
|
|
||||||
|
// Concatenation fix for macros
|
||||||
|
#define IAT_BEGIN_BLOCK(_group, _name) class _group##_##_name : public ia::iatest::block { \
|
||||||
|
public: PCCHAR name() CONST OVERRIDE { return #_group "::" #_name; } private:
|
||||||
|
|
||||||
|
#define IAT_END_BLOCK() };
|
||||||
|
|
||||||
|
#define IAT_BEGIN_TEST_LIST() public: VOID declareTests() OVERRIDE {
|
||||||
|
#define IAT_ADD_TEST(name) IAT_UNIT(name)
|
||||||
|
#define IAT_END_TEST_LIST() } private:
|
||||||
|
|
||||||
|
namespace ia::iatest
|
||||||
|
{
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Type Printing Helper (To show WHAT failed)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
template<typename T>
|
||||||
|
std::string ToString(CONST T& value) {
|
||||||
|
if constexpr (std::is_arithmetic_v<T>) {
|
||||||
|
return std::to_string(value);
|
||||||
|
} else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, const char*>) {
|
||||||
|
return std::string("\"") + value + "\"";
|
||||||
|
} else {
|
||||||
|
return "{Object}"; // Fallback for complex types
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Specialization for pointers
|
||||||
|
template<typename T>
|
||||||
|
std::string ToString(T* value) {
|
||||||
|
if (value == NULLPTR) return "nullptr";
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << "ptr(" << (void*)value << ")";
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Core Structures
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
DEFINE_TYPE(functor_t, std::function<BOOL()>);
|
||||||
|
|
||||||
|
struct unit_t {
|
||||||
|
std::string Name;
|
||||||
|
functor_t Functor;
|
||||||
|
};
|
||||||
|
|
||||||
|
class block
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~block() = default;
|
||||||
|
PURE_VIRTUAL(PCCHAR name() CONST);
|
||||||
|
PURE_VIRTUAL(VOID declareTests());
|
||||||
|
|
||||||
|
std::vector<unit_t>& units() { return m_units; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// Generic Equality
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
BOOL _test_eq(IN CONST T1& lhs, IN CONST T2& rhs, IN PCCHAR description)
|
||||||
|
{
|
||||||
|
if(lhs != rhs) {
|
||||||
|
print_fail(description, ToString(lhs), ToString(rhs));
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generic Inequality
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
BOOL _test_neq(IN CONST T1& lhs, IN CONST T2& rhs, IN PCCHAR description)
|
||||||
|
{
|
||||||
|
if(lhs == rhs) {
|
||||||
|
print_fail(description, ToString(lhs), "NOT " + ToString(rhs));
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Floating Point Approximation (Epsilon check)
|
||||||
|
template<typename T>
|
||||||
|
BOOL _test_approx(IN T lhs, IN T rhs, IN PCCHAR description)
|
||||||
|
{
|
||||||
|
static_assert(std::is_floating_point_v<T>, "Approx only works for floats/doubles");
|
||||||
|
T diff = std::abs(lhs - rhs);
|
||||||
|
if (diff > static_cast<T>(0.0001)) { // Default epsilon
|
||||||
|
print_fail(description, ToString(lhs), ToString(rhs));
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL _test(IN BOOL value, IN PCCHAR description)
|
||||||
|
{
|
||||||
|
if(!value) {
|
||||||
|
printf(__CC_BLUE " %s... " __CC_RED "FAILED" __CC_DEFAULT "\n", description);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL _test_not(IN BOOL value, IN PCCHAR description)
|
||||||
|
{
|
||||||
|
if(value) {
|
||||||
|
printf(__CC_BLUE " %s... " __CC_RED "FAILED" __CC_DEFAULT "\n", description);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
VOID _test_unit(IN functor_t functor, IN PCCHAR name)
|
||||||
|
{
|
||||||
|
m_units.push_back({name, functor});
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
VOID print_fail(PCCHAR desc, std::string v1, std::string v2) {
|
||||||
|
printf(__CC_BLUE " %s... " __CC_RED "FAILED" __CC_DEFAULT "\n", desc);
|
||||||
|
printf(__CC_RED " Expected: %s" __CC_DEFAULT "\n", v2.c_str());
|
||||||
|
printf(__CC_RED " Actual: %s" __CC_DEFAULT "\n", v1.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<unit_t> m_units;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename block_class>
|
||||||
|
concept valid_block_class = std::derived_from<block_class, block>;
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Runner
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
template<BOOL stopOnFail = false, BOOL isVerbose = false>
|
||||||
|
class runner
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
runner(){}
|
||||||
|
~runner() { summarize(); }
|
||||||
|
|
||||||
|
template<typename block_class>
|
||||||
|
requires valid_block_class<block_class>
|
||||||
|
VOID testBlock();
|
||||||
|
|
||||||
|
private:
|
||||||
|
VOID summarize();
|
||||||
|
|
||||||
|
private:
|
||||||
|
SIZE_T m_testCount { 0 };
|
||||||
|
SIZE_T m_failCount { 0 };
|
||||||
|
SIZE_T m_blockCount { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
template<BOOL stopOnFail, BOOL isVerbose>
|
||||||
|
template<typename block_class>
|
||||||
|
requires valid_block_class<block_class>
|
||||||
|
VOID runner<stopOnFail, isVerbose>::testBlock()
|
||||||
|
{
|
||||||
|
m_blockCount++;
|
||||||
|
block_class b;
|
||||||
|
b.declareTests();
|
||||||
|
|
||||||
|
printf(__CC_MAGENTA "Testing [%s]..." __CC_DEFAULT "\n", b.name());
|
||||||
|
|
||||||
|
for(auto& v: b.units())
|
||||||
|
{
|
||||||
|
m_testCount++;
|
||||||
|
if constexpr(isVerbose) {
|
||||||
|
printf(__CC_YELLOW " Testing %s...\n" __CC_DEFAULT, v.Name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL result = FALSE;
|
||||||
|
try {
|
||||||
|
// Execute the test function
|
||||||
|
result = v.Functor();
|
||||||
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
|
printf(__CC_RED " CRITICAL EXCEPTION in %s: %s\n" __CC_DEFAULT, v.Name.c_str(), e.what());
|
||||||
|
result = FALSE;
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
printf(__CC_RED " UNKNOWN CRITICAL EXCEPTION in %s\n" __CC_DEFAULT, v.Name.c_str());
|
||||||
|
result = FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!result)
|
||||||
|
{
|
||||||
|
m_failCount++;
|
||||||
|
if constexpr(stopOnFail) { summarize(); exit(-1); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fputs("\n", stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<BOOL stopOnFail, BOOL isVerbose>
|
||||||
|
VOID runner<stopOnFail, isVerbose>::summarize()
|
||||||
|
{
|
||||||
|
printf(__CC_GREEN "\n-----------------------------------\n\t SUMMARY\n-----------------------------------\n");
|
||||||
|
|
||||||
|
if(!m_failCount) {
|
||||||
|
printf("\n\tALL TESTS PASSED!\n\n");
|
||||||
|
} else {
|
||||||
|
FLOAT64 successRate = (100.0 * static_cast<FLOAT64>(m_testCount - m_failCount)/static_cast<FLOAT64>(m_testCount));
|
||||||
|
printf(__CC_RED "%zu OUT OF %zu TESTS FAILED\n" __CC_YELLOW "Success Rate: %.2f%%\n", m_failCount, m_testCount, successRate);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(__CC_MAGENTA "Ran %zu test(s) across %zu block(s)\n" __CC_GREEN "-----------------------------------" __CC_DEFAULT "\n", m_testCount, m_blockCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename>
|
||||||
|
struct _valid_iatest_runner : std::false_type {};
|
||||||
|
template<BOOL stopOnFail, BOOL isVerbose>
|
||||||
|
struct _valid_iatest_runner<runner<stopOnFail,isVerbose>> : std::true_type {};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // __cplusplus
|
||||||
527
Src/IACore/inc/IACore/PCH.hpp
Normal file
527
Src/IACore/inc/IACore/PCH.hpp
Normal file
@ -0,0 +1,527 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <memory.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# include <bit>
|
||||||
|
# include <new>
|
||||||
|
# include <span>
|
||||||
|
# include <limits>
|
||||||
|
# include <cstring>
|
||||||
|
# include <cstddef>
|
||||||
|
# include <concepts>
|
||||||
|
# include <functional>
|
||||||
|
# include <type_traits>
|
||||||
|
# include <initializer_list>
|
||||||
|
|
||||||
|
# include <array>
|
||||||
|
# include <string>
|
||||||
|
# include <vector>
|
||||||
|
# include <format>
|
||||||
|
# include <sstream>
|
||||||
|
# include <optional>
|
||||||
|
# include <string_view>
|
||||||
|
|
||||||
|
# include <tl/expected.hpp>
|
||||||
|
# include <ankerl/unordered_dense.h>
|
||||||
|
|
||||||
|
#else
|
||||||
|
# include <float.h>
|
||||||
|
# include <stdbool.h>
|
||||||
|
# include <stddef.h>
|
||||||
|
# include <string.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Configuration Macros
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define IA_CHECK(o) (o > 0)
|
||||||
|
|
||||||
|
#if defined(__IA_DEBUG) && __IA_DEBUG
|
||||||
|
# define __DEBUG_MODE__
|
||||||
|
# define __BUILD_MODE_NAME "debug"
|
||||||
|
# define DEBUG_ONLY(v) v
|
||||||
|
#else
|
||||||
|
# define __RELEASE_MODE__
|
||||||
|
# define __BUILD_MODE_NAME "release"
|
||||||
|
# ifndef NDEBUG
|
||||||
|
# define NDEBUG
|
||||||
|
# endif
|
||||||
|
# ifndef __OPTIMIZE__
|
||||||
|
# define __OPTIMIZE__
|
||||||
|
# endif
|
||||||
|
# define DEBUG_ONLY(f)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Security Macros
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define IA_PANIC(msg) \
|
||||||
|
{ \
|
||||||
|
fprintf(stderr, "PANIC: %s\n", msg); \
|
||||||
|
__builtin_trap(); \
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advanced Security features are not included in OSS builds
|
||||||
|
// (OSS version does not implement 'IAC_CHECK_*'s)
|
||||||
|
#define IACORE_SECURITY_LEVEL 0
|
||||||
|
|
||||||
|
#define IAC_SEC_LEVEL(v) (IACORE_SECURITY_LEVEL >= v)
|
||||||
|
|
||||||
|
#if __IA_DEBUG || IAC_SEC_LEVEL(1)
|
||||||
|
# define __IAC_OVERFLOW_CHECKS 1
|
||||||
|
#else
|
||||||
|
# define __IAC_OVERFLOW_CHECKS 0
|
||||||
|
#endif
|
||||||
|
#if __IA_DEBUG || IAC_SEC_LEVEL(2)
|
||||||
|
# define __IAC_SANITY_CHECKS 1
|
||||||
|
#else
|
||||||
|
# define __IAC_SANITY_CHECKS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Language Abstraction Macros
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define AUTO auto
|
||||||
|
#define CONST const
|
||||||
|
#define STATIC static
|
||||||
|
#define EXTERN extern
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# define VIRTUAL virtual
|
||||||
|
# define OVERRIDE override
|
||||||
|
# define CONSTEXPR constexpr
|
||||||
|
# define NOEXCEPT noexcept
|
||||||
|
# define NULLPTR nullptr
|
||||||
|
# define IA_MOVE(...) std::move(__VA_ARGS__)
|
||||||
|
# define DECONST(t, v) const_cast<t>(v)
|
||||||
|
# define NORETURN [[noreturn]]
|
||||||
|
#else
|
||||||
|
# define VIRTUAL
|
||||||
|
# define OVERRIDE
|
||||||
|
# define CONSTEXPR const
|
||||||
|
# define NOEXCEPT
|
||||||
|
# define NULLPTR NULL
|
||||||
|
# define IA_MOVE(...) (__VA_ARGS__)
|
||||||
|
# define DECONST(t, v) ((t) (v))
|
||||||
|
# define NORETURN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define DEFINE_TYPE(t, v) typedef v t
|
||||||
|
#define FORWARD_DECLARE(t, i) t i
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# define PURE_VIRTUAL(f) VIRTUAL f = 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Attributes & Compiler Intrinsics
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define INLINE inline
|
||||||
|
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
|
||||||
|
# define ALWAYS_INLINE __attribute__((always_inline)) inline
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
# define ALWAYS_INLINE __forceinline
|
||||||
|
#else
|
||||||
|
# define ALWAYS_INLINE inline
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define UNUSED(v) ((void) v);
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
# define NO_DISCARD(s) [[nodiscard(s)]]
|
||||||
|
# define B_LIKELY(cond) (cond) [[likely]]
|
||||||
|
# define B_UNLIKELY(cond) (cond) [[unlikely]]
|
||||||
|
#else
|
||||||
|
# define NO_DISCARD(s)
|
||||||
|
# define B_LIKELY(cond) (cond)
|
||||||
|
# define B_UNLIKELY(cond) (cond)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define __INTERNAL_IA_STRINGIFY(value) #value
|
||||||
|
#define IA_STRINGIFY(value) __INTERNAL_IA_STRINGIFY(value)
|
||||||
|
|
||||||
|
#define ALIGN(a) __attribute__((aligned(a)))
|
||||||
|
|
||||||
|
// Mark every explicit assembly instruction as volatile
|
||||||
|
#define ASM(...) __asm__ volatile(__VA_ARGS__)
|
||||||
|
|
||||||
|
#ifndef NULL
|
||||||
|
# define NULL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#undef TRUE
|
||||||
|
#undef FALSE
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# define FALSE false
|
||||||
|
# define TRUE true
|
||||||
|
#else
|
||||||
|
# define FALSE 0
|
||||||
|
# define TRUE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Parameter Annotations
|
||||||
|
#define IN
|
||||||
|
#define OUT
|
||||||
|
#define INOUT
|
||||||
|
|
||||||
|
#undef VOID
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Extern C Handling
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# define IA_EXTERN_C_BEGIN \
|
||||||
|
extern "C" \
|
||||||
|
{
|
||||||
|
# define IA_EXTERN_C_END }
|
||||||
|
# define C_DECL(f) extern "C" f
|
||||||
|
#else
|
||||||
|
# define IA_EXTERN_C_BEGIN
|
||||||
|
# define IA_EXTERN_C_END
|
||||||
|
# define C_DECL(f) f
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Utilities
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# define CAST(v, t) (static_cast<t>(v))
|
||||||
|
# define REINTERPRET(v, t) (reinterpret_cast<t>(v))
|
||||||
|
#else
|
||||||
|
# define CAST(v, t) ((t) (v))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Templates and Aliases
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# define ALIAS_FUNCTION(alias, function) \
|
||||||
|
template<typename... Args> auto alias(Args &&...args) -> decltype(f(std::forward<Args>(args)...)) \
|
||||||
|
{ \
|
||||||
|
return function(std::forward<Args>(args)...); \
|
||||||
|
}
|
||||||
|
|
||||||
|
# define ALIAS_TEMPLATE_FUNCTION(t, alias, function) \
|
||||||
|
template<typename t, typename... Args> \
|
||||||
|
auto alias(Args &&...args) -> decltype(function<t>(std::forward<Args>(args)...)) \
|
||||||
|
{ \
|
||||||
|
return function<t>(std::forward<Args>(args)...); \
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Assertions
|
||||||
|
#define IA_RELEASE_ASSERT(v) assert((v))
|
||||||
|
#define IA_RELEASE_ASSERT_MSG(v, m) assert((v) && m)
|
||||||
|
|
||||||
|
#if defined(__DEBUG_MODE__)
|
||||||
|
# define IA_ASSERT(v) IA_RELEASE_ASSERT(v)
|
||||||
|
# define IA_ASSERT_MSG(v, m) IA_RELEASE_ASSERT_MSG(v, m)
|
||||||
|
#else
|
||||||
|
# define IA_ASSERT(v)
|
||||||
|
# define IA_ASSERT_MSG(v, m)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define IA_ASSERT_EQ(a, b) IA_ASSERT((a) == (b))
|
||||||
|
#define IA_ASSERT_GE(a, b) IA_ASSERT((a) >= (b))
|
||||||
|
#define IA_ASSERT_LE(a, b) IA_ASSERT(a <= b)
|
||||||
|
#define IA_ASSERT_LT(a, b) IA_ASSERT(a < b)
|
||||||
|
#define IA_ASSERT_GT(a, b) IA_ASSERT(a > b)
|
||||||
|
#define IA_ASSERT_IMPLIES(a, b) IA_ASSERT(!(a) || (b))
|
||||||
|
#define IA_ASSERT_NOT_NULL(v) IA_ASSERT(((v) != NULLPTR))
|
||||||
|
|
||||||
|
#define IA_UNREACHABLE(msg) IA_RELEASE_ASSERT_MSG(FALSE, "Unreachable code: " msg)
|
||||||
|
|
||||||
|
#define IA_TRY(expr) \
|
||||||
|
__extension__({ \
|
||||||
|
auto _ia_res = (expr); \
|
||||||
|
if (!_ia_res) \
|
||||||
|
{ \
|
||||||
|
return tl::make_unexpected(std::move(_ia_res.error())); \
|
||||||
|
} \
|
||||||
|
std::move(*_ia_res); \
|
||||||
|
})
|
||||||
|
|
||||||
|
#define IA_CONCAT_IMPL(x, y) x##y
|
||||||
|
#define IA_CONCAT(x, y) IA_CONCAT_IMPL(x, y)
|
||||||
|
#define IA_UNIQUE_NAME(prefix) IA_CONCAT(prefix, __LINE__)
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Limits & Versioning
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
# define IA_MAX_POSSIBLE_SIZE (static_cast<SIZE_T>(0x7FFFFFFFFFFFF))
|
||||||
|
#else
|
||||||
|
# define IA_MAX_POSSIBLE_SIZE ((SIZE_T) (0x7FFFFFFFFFFFF))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define IA_MAX_PATH_LENGTH 4096
|
||||||
|
#define IA_MAX_STRING_LENGTH (IA_MAX_POSSIBLE_SIZE >> 8)
|
||||||
|
|
||||||
|
#define IA_VERSION_TYPE uint64_t
|
||||||
|
#define IA_MAKE_VERSION(major, minor, patch) \
|
||||||
|
((((uint64_t) (major) & 0xFFFFFF) << 40) | (((uint64_t) (minor) & 0xFFFFFF) << 16) | ((uint64_t) (patch) & 0xFFFF))
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// DLL Export/Import
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
# define IA_DLL_EXPORT __declspec(dllexport)
|
||||||
|
# define IA_DLL_IMPORT __declspec(dllimport)
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
# define IA_DLL_EXPORT __attribute__((visibility("default")))
|
||||||
|
# define IA_DLL_IMPORT
|
||||||
|
#else
|
||||||
|
# define IA_DLL_EXPORT
|
||||||
|
# define IA_DLL_IMPORT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Console Colors (ANSI Escape Codes)
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define __CC_BLACK "\033[30m"
|
||||||
|
#define __CC_RED "\033[31m"
|
||||||
|
#define __CC_GREEN "\033[32m"
|
||||||
|
#define __CC_YELLOW "\033[33m"
|
||||||
|
#define __CC_BLUE "\033[34m"
|
||||||
|
#define __CC_MAGENTA "\033[35m"
|
||||||
|
#define __CC_CYAN "\033[36m"
|
||||||
|
#define __CC_WHITE "\033[37m"
|
||||||
|
#define __CC_DEFAULT "\033[39m"
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Base Types
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
typedef void VOID;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
typedef bool BOOL;
|
||||||
|
#else
|
||||||
|
typedef _Bool BOOL;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef char CHAR;
|
||||||
|
typedef uint16_t CHAR16;
|
||||||
|
|
||||||
|
typedef int8_t INT8;
|
||||||
|
typedef int16_t INT16;
|
||||||
|
typedef int32_t INT32;
|
||||||
|
typedef int64_t INT64;
|
||||||
|
|
||||||
|
typedef uint8_t UINT8;
|
||||||
|
typedef uint16_t UINT16;
|
||||||
|
typedef uint32_t UINT32;
|
||||||
|
typedef uint64_t UINT64;
|
||||||
|
|
||||||
|
typedef float FLOAT32;
|
||||||
|
typedef double FLOAT64;
|
||||||
|
|
||||||
|
typedef INT32 INT;
|
||||||
|
typedef UINT32 UINT;
|
||||||
|
typedef size_t SIZE_T;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
typedef std::make_signed_t<size_t> SSIZE_T;
|
||||||
|
typedef std::align_val_t ALIGN_T;
|
||||||
|
#else
|
||||||
|
typedef ptrdiff_t SSIZE_T;
|
||||||
|
typedef size_t ALIGN_T;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Pointer Types
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
typedef VOID *PVOID;
|
||||||
|
typedef BOOL *PBOOL;
|
||||||
|
typedef CHAR *PCHAR;
|
||||||
|
typedef CHAR16 *PCHAR16;
|
||||||
|
|
||||||
|
typedef INT8 *PINT8;
|
||||||
|
typedef INT16 *PINT16;
|
||||||
|
typedef INT32 *PINT32;
|
||||||
|
typedef INT64 *PINT64;
|
||||||
|
|
||||||
|
typedef UINT8 *PUINT8;
|
||||||
|
typedef UINT16 *PUINT16;
|
||||||
|
typedef UINT32 *PUINT32;
|
||||||
|
typedef UINT64 *PUINT64;
|
||||||
|
|
||||||
|
typedef INT *PINT;
|
||||||
|
typedef UINT *PUINT;
|
||||||
|
typedef SIZE_T *PSIZE;
|
||||||
|
typedef SSIZE_T *PSSIZE;
|
||||||
|
|
||||||
|
typedef FLOAT32 *PFLOAT32;
|
||||||
|
typedef FLOAT64 *PFLOAT64;
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Const Pointer Types
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
typedef CONST VOID *PCVOID;
|
||||||
|
typedef CONST BOOL *PCBOOL;
|
||||||
|
typedef CONST CHAR *PCCHAR;
|
||||||
|
typedef CONST CHAR16 *PCCHAR16;
|
||||||
|
|
||||||
|
typedef CONST INT8 *PCINT8;
|
||||||
|
typedef CONST INT16 *PCINT16;
|
||||||
|
typedef CONST INT32 *PCINT32;
|
||||||
|
typedef CONST INT64 *PCINT64;
|
||||||
|
|
||||||
|
typedef CONST UINT8 *PCUINT8;
|
||||||
|
typedef CONST UINT16 *PCUINT16;
|
||||||
|
typedef CONST UINT32 *PCUINT32;
|
||||||
|
typedef CONST UINT64 *PCUINT64;
|
||||||
|
|
||||||
|
typedef CONST INT *PCINT;
|
||||||
|
typedef CONST UINT *PCUINT;
|
||||||
|
typedef CONST SIZE_T *PCSIZE;
|
||||||
|
typedef CONST SSIZE_T *PCSSIZE;
|
||||||
|
|
||||||
|
typedef CONST FLOAT32 *PCFLOAT32;
|
||||||
|
typedef CONST FLOAT64 *PCFLOAT64;
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// GUID Structure
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
typedef struct _IA_GUID
|
||||||
|
{
|
||||||
|
UINT32 Data1;
|
||||||
|
UINT16 Data2;
|
||||||
|
UINT16 Data3;
|
||||||
|
UINT8 Data4[8];
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
bool operator==(const _IA_GUID &other) const
|
||||||
|
{
|
||||||
|
return __builtin_memcmp(this, &other, sizeof(_IA_GUID)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const _IA_GUID &other) const
|
||||||
|
{
|
||||||
|
return !(*this == other);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
} GUID;
|
||||||
|
|
||||||
|
STATIC INLINE BOOL IA_GUID_Equals(CONST GUID *a, CONST GUID *b)
|
||||||
|
{
|
||||||
|
if (a == NULLPTR || b == NULLPTR)
|
||||||
|
return FALSE;
|
||||||
|
return memcmp(a, b, sizeof(GUID)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Numeric Constants
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
#ifdef __cplusplus
|
||||||
|
STATIC CONSTEXPR FLOAT32 FLOAT32_EPSILON = std::numeric_limits<FLOAT32>::epsilon();
|
||||||
|
STATIC CONSTEXPR FLOAT64 FLOAT64_EPSILON = std::numeric_limits<FLOAT64>::epsilon();
|
||||||
|
#else
|
||||||
|
STATIC CONST FLOAT32 FLOAT32_EPSILON = FLT_EPSILON;
|
||||||
|
STATIC CONST FLOAT64 FLOAT64_EPSILON = DBL_EPSILON;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Platform Detection
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
||||||
|
# ifdef _WIN64
|
||||||
|
# define IA_PLATFORM_WIN64 1
|
||||||
|
# define IA_PLATFORM_WINDOWS 1
|
||||||
|
# else
|
||||||
|
# define IA_PLATFORM_WIN32 1
|
||||||
|
# define IA_PLATFORM_WINDOWS 1
|
||||||
|
# endif
|
||||||
|
#elif __APPLE__
|
||||||
|
# include <TargetConditionals.h>
|
||||||
|
# if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR || TARGET_OS_MACCATALYST
|
||||||
|
# define IA_PLATFORM_IOS 1
|
||||||
|
# elif TARGET_OS_MAC
|
||||||
|
# define IA_PLATFORM_MAC 1
|
||||||
|
# endif
|
||||||
|
# define IA_PLATFORM_UNIX 1
|
||||||
|
#elif __ANDROID__
|
||||||
|
# define IA_PLATFORM_ANDROID 1
|
||||||
|
# define IA_PLATFORM_LINUX 1
|
||||||
|
# define IA_PLATFORM_UNIX 1
|
||||||
|
#elif __linux__
|
||||||
|
# define IA_PLATFORM_LINUX 1
|
||||||
|
# define IA_PLATFORM_UNIX 1
|
||||||
|
#elif __unix__
|
||||||
|
# define IA_PLATFORM_UNIX 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WIN32 || IA_PLATFORM_WIN64
|
||||||
|
# ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
# define WIN32_LEAN_AND_MEAN
|
||||||
|
# endif
|
||||||
|
# ifndef NOMINMAX
|
||||||
|
# define NOMINMAX
|
||||||
|
# endif
|
||||||
|
# include <windows.h>
|
||||||
|
#elif IA_PLATFORM_UNIX
|
||||||
|
# include <unistd.h>
|
||||||
|
# include <sys/wait.h>
|
||||||
|
# include <spawn.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if IA_CHECK(IA_PLATFORM_WIN64) || IA_CHECK(IA_PLATFORM_UNIX)
|
||||||
|
# define IA_CORE_PLATFORM_FEATURES 1
|
||||||
|
#else
|
||||||
|
# define IA_CORE_PLATFORM_FEATURES 0
|
||||||
|
# warning "IACore Unsupported Platform: Platform specific features will be disabled"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// C++ Containers and Helpers
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
#ifdef __cplusplus
|
||||||
|
|
||||||
|
template<typename _function_type> using Function = std::function<_function_type>;
|
||||||
|
template<typename _value_type> using InitializerList = std::initializer_list<_value_type>;
|
||||||
|
template<typename _value_type, SIZE_T count> using Array = std::array<_value_type, count>;
|
||||||
|
template<typename _value_type> using Vector = std::vector<_value_type>;
|
||||||
|
template<typename _value_type> using Optional = std::optional<_value_type>;
|
||||||
|
template<typename _key_type> using UnorderedSet = ankerl::unordered_dense::set<_key_type>;
|
||||||
|
template<typename _key_type, typename _value_type>
|
||||||
|
using UnorderedMap = ankerl::unordered_dense::map<_key_type, _value_type>;
|
||||||
|
|
||||||
|
using String = std::string;
|
||||||
|
using StringView = std::string_view;
|
||||||
|
using StringStream = std::stringstream;
|
||||||
|
|
||||||
|
template<typename... Args> STATIC String BuildString(Args &&...args)
|
||||||
|
{
|
||||||
|
std::stringstream ss;
|
||||||
|
(ss << ... << args);
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
211
Src/IACore/inc/IACore/Process.hpp
Normal file
211
Src/IACore/inc/IACore/Process.hpp
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
namespace IACore {
|
||||||
|
|
||||||
|
class Process {
|
||||||
|
public:
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Static One-Shot Execution
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Returns Exit Code or Error String
|
||||||
|
// callback receives distinct lines of output (stdout + stderr merged)
|
||||||
|
STATIC tl::expected<INT32, String> Run(
|
||||||
|
CONST String& cmd,
|
||||||
|
CONST String& args,
|
||||||
|
Function<VOID(StringView)> onOutputLine
|
||||||
|
) {
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
return RunWindows(cmd, args, onOutputLine);
|
||||||
|
#else
|
||||||
|
return RunPosix(cmd, args, onOutputLine);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Output Buffering Helper
|
||||||
|
// Splits raw chunks into lines, preserving partial lines across chunks
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
struct LineBuffer {
|
||||||
|
String accumulator;
|
||||||
|
Function<VOID(StringView)>& callback;
|
||||||
|
|
||||||
|
VOID Append(const char* data, SIZE_T size) {
|
||||||
|
SIZE_T start = 0;
|
||||||
|
for (SIZE_T i = 0; i < size; ++i) {
|
||||||
|
if (data[i] == '\n' || data[i] == '\r') {
|
||||||
|
// Flush accumulator + current chunk
|
||||||
|
if (!accumulator.empty()) {
|
||||||
|
accumulator.append(data + start, i - start);
|
||||||
|
if (!accumulator.empty()) callback(accumulator);
|
||||||
|
accumulator.clear();
|
||||||
|
} else {
|
||||||
|
// Zero copy optimization for pure lines in one chunk
|
||||||
|
if (i > start) callback(StringView(data + start, i - start));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip \r\n sequence if needed, or just start next
|
||||||
|
if (data[i] == '\r' && i + 1 < size && data[i+1] == '\n') i++;
|
||||||
|
start = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Save remaining partial line
|
||||||
|
if (start < size) {
|
||||||
|
accumulator.append(data + start, size - start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VOID Flush() {
|
||||||
|
if (!accumulator.empty()) {
|
||||||
|
callback(accumulator);
|
||||||
|
accumulator.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Windows Implementation
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
STATIC tl::expected<INT32, String> RunWindows(CONST String& cmd, CONST String& args, Function<VOID(StringView)> cb) {
|
||||||
|
SECURITY_ATTRIBUTES saAttr = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; // Allow inheritance
|
||||||
|
HANDLE hRead = NULL, hWrite = NULL;
|
||||||
|
|
||||||
|
if (!CreatePipe(&hRead, &hWrite, &saAttr, 0))
|
||||||
|
return tl::make_unexpected("Failed to create pipe");
|
||||||
|
|
||||||
|
// Ensure the read handle to the pipe for STDOUT is NOT inherited
|
||||||
|
if (!SetHandleInformation(hRead, HANDLE_FLAG_INHERIT, 0))
|
||||||
|
return tl::make_unexpected("Failed to secure pipe handles");
|
||||||
|
|
||||||
|
STARTUPINFOA si = { sizeof(STARTUPINFOA) };
|
||||||
|
si.dwFlags |= STARTF_USESTDHANDLES;
|
||||||
|
si.hStdOutput = hWrite;
|
||||||
|
si.hStdError = hWrite; // Merge stderr
|
||||||
|
si.hStdInput = NULL; // No input
|
||||||
|
|
||||||
|
PROCESS_INFORMATION pi = { 0 };
|
||||||
|
|
||||||
|
// Windows command line needs to be mutable and concatenated
|
||||||
|
String commandLine = BuildString("\"", cmd, "\" ", args);
|
||||||
|
|
||||||
|
BOOL success = CreateProcessA(
|
||||||
|
NULL, commandLine.data(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi
|
||||||
|
);
|
||||||
|
|
||||||
|
// Important: Close write end in parent, otherwise ReadFile never returns EOF!
|
||||||
|
CloseHandle(hWrite);
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
CloseHandle(hRead);
|
||||||
|
return tl::make_unexpected(String("CreateProcess failed: ") + std::to_string(GetLastError()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read Loop
|
||||||
|
LineBuffer lineBuf{ "", cb };
|
||||||
|
DWORD bytesRead;
|
||||||
|
CHAR buffer[4096];
|
||||||
|
|
||||||
|
while (ReadFile(hRead, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead != 0) {
|
||||||
|
lineBuf.Append(buffer, bytesRead);
|
||||||
|
}
|
||||||
|
lineBuf.Flush();
|
||||||
|
|
||||||
|
// NOW we wait for exit code
|
||||||
|
DWORD exitCode = 0;
|
||||||
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
||||||
|
GetExitCodeProcess(pi.hProcess, &exitCode);
|
||||||
|
|
||||||
|
CloseHandle(pi.hProcess);
|
||||||
|
CloseHandle(pi.hThread);
|
||||||
|
CloseHandle(hRead);
|
||||||
|
|
||||||
|
return static_cast<INT32>(exitCode);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// POSIX (Linux/Mac) Implementation
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
#if IA_PLATFORM_UNIX
|
||||||
|
STATIC tl::expected<INT32, String> RunPosix(CONST String& cmd, CONST String& args, Function<VOID(StringView)> cb) {
|
||||||
|
int pipefd[2];
|
||||||
|
if (pipe(pipefd) == -1) return tl::make_unexpected("Failed to create pipe");
|
||||||
|
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
if (pid == -1) {
|
||||||
|
return tl::make_unexpected("Failed to fork process");
|
||||||
|
}
|
||||||
|
else if (pid == 0) {
|
||||||
|
// --- Child Process ---
|
||||||
|
close(pipefd[0]); // Close read end
|
||||||
|
|
||||||
|
// Redirect stdout/stderr to pipe
|
||||||
|
dup2(pipefd[1], STDOUT_FILENO);
|
||||||
|
dup2(pipefd[1], STDERR_FILENO);
|
||||||
|
close(pipefd[1]); // Close original write end
|
||||||
|
|
||||||
|
// Prepare Args. execvp requires an array of char*
|
||||||
|
// This is quick and dirty splitting.
|
||||||
|
// In a real engine you might pass args as a vector<string>.
|
||||||
|
std::vector<char*> argv;
|
||||||
|
std::string cmdStr = cmd; // copy to modify if needed
|
||||||
|
argv.push_back(cmdStr.data());
|
||||||
|
|
||||||
|
// Split args string by space (simplistic)
|
||||||
|
// Better: Pass vector<string> to Run()
|
||||||
|
std::string argsCopy = args;
|
||||||
|
char* token = strtok(argsCopy.data(), " ");
|
||||||
|
while (token) {
|
||||||
|
argv.push_back(token);
|
||||||
|
token = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
argv.push_back(nullptr);
|
||||||
|
|
||||||
|
execvp(argv[0], argv.data());
|
||||||
|
_exit(127); // If execvp fails
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// --- Parent Process ---
|
||||||
|
close(pipefd[1]); // Close write end
|
||||||
|
|
||||||
|
LineBuffer lineBuf{ "", cb };
|
||||||
|
char buffer[4096];
|
||||||
|
ssize_t count;
|
||||||
|
|
||||||
|
while ((count = read(pipefd[0], buffer, sizeof(buffer))) > 0) {
|
||||||
|
lineBuf.Append(buffer, count);
|
||||||
|
}
|
||||||
|
lineBuf.Flush();
|
||||||
|
close(pipefd[0]);
|
||||||
|
|
||||||
|
int status;
|
||||||
|
waitpid(pid, &status, 0);
|
||||||
|
|
||||||
|
if (WIFEXITED(status)) return WEXITSTATUS(status);
|
||||||
|
return -1; // Crashed or killed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
}
|
||||||
159
Src/IACore/inc/IACore/Utils.hpp
Normal file
159
Src/IACore/inc/IACore/Utils.hpp
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <IACore/PCH.hpp>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace IACore
|
||||||
|
{
|
||||||
|
|
||||||
|
class Utils
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
INLINE STATIC String BinaryToHexString(std::span<const UINT8> data)
|
||||||
|
{
|
||||||
|
STATIC CONSTEXPR char LUT[] = "0123456789ABCDEF";
|
||||||
|
String res;
|
||||||
|
res.reserve(data.size() * 2);
|
||||||
|
|
||||||
|
for (UINT8 b : data)
|
||||||
|
{
|
||||||
|
res.push_back(LUT[(b >> 4) & 0x0F]);
|
||||||
|
res.push_back(LUT[b & 0x0F]);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
INLINE STATIC tl::expected<Vector<UINT8>, String> HexStringToBinary(CONST StringView &hex)
|
||||||
|
{
|
||||||
|
if (hex.size() % 2 != 0)
|
||||||
|
{
|
||||||
|
return tl::make_unexpected(String("Hex string must have even length"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector<UINT8> out;
|
||||||
|
out.reserve(hex.size() / 2);
|
||||||
|
|
||||||
|
for (SIZE_T i = 0; i < hex.size(); i += 2)
|
||||||
|
{
|
||||||
|
char high = hex[i];
|
||||||
|
char low = hex[i + 1];
|
||||||
|
|
||||||
|
// Quick helper to decode nibble
|
||||||
|
auto fromHexChar = [](char c) -> int {
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
return c - '0';
|
||||||
|
if (c >= 'A' && c <= 'F')
|
||||||
|
return c - 'A' + 10;
|
||||||
|
if (c >= 'a' && c <= 'f')
|
||||||
|
return c - 'a' + 10;
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
int h = fromHexChar(high);
|
||||||
|
int l = fromHexChar(low);
|
||||||
|
|
||||||
|
if (h == -1 || l == -1)
|
||||||
|
{
|
||||||
|
return tl::make_unexpected(String("Invalid hex character found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
out.push_back(CAST((h << 4) | l, UINT8));
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Range> INLINE STATIC VOID Sort(Range &&range)
|
||||||
|
{
|
||||||
|
std::ranges::sort(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Range, typename T> INLINE STATIC auto BinarySearchLeft(Range &&range, CONST T &value)
|
||||||
|
{
|
||||||
|
return std::ranges::lower_bound(range, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Range, typename T> INLINE STATIC auto BinarySearchRight(Range &&range, CONST T &value)
|
||||||
|
{
|
||||||
|
return std::ranges::upper_bound(range, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Hash Combination Logic
|
||||||
|
// Uses the "golden ratio" mix (similar to Boost) to preserve entropy.
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
template<typename T> INLINE STATIC void HashCombine(UINT64 &seed, CONST T &v)
|
||||||
|
{
|
||||||
|
// Use Ankerl's high-speed hasher for the individual type
|
||||||
|
// This automatically handles ints, floats, strings, etc. efficiently.
|
||||||
|
auto hasher = ankerl::unordered_dense::hash<T>();
|
||||||
|
UINT64 h = hasher(v);
|
||||||
|
|
||||||
|
// 0x9e3779b97f4a7c15 is the 64-bit golden ratio (phi) approximation
|
||||||
|
// This spreads bits to avoid collisions in the hash table.
|
||||||
|
seed ^= h + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Variadic Hasher
|
||||||
|
// Allows: IACore::ComputeHash(x, y, z);
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
template<typename... Args> INLINE STATIC UINT64 ComputeHash(CONST Args &...args)
|
||||||
|
{
|
||||||
|
UINT64 seed = 0;
|
||||||
|
(HashCombine(seed, args), ...); // C++17/20 Fold Expression
|
||||||
|
return seed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// F;pat Hasher
|
||||||
|
// Allows: IACore::ComputeHashFlat(x, y, z);
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
template<typename T, typename... MemberPtrs> INLINE STATIC UINT64 ComputeHashFlat(CONST T &obj, MemberPtrs... members)
|
||||||
|
{
|
||||||
|
UINT64 seed = 0;
|
||||||
|
// C++17 Fold Expression: applies (seed, obj.*ptr) for every ptr in members
|
||||||
|
(HashCombine(seed, obj.*members), ...);
|
||||||
|
return seed;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace IACore
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// MACRO: IA_MAKE_HASHABLE
|
||||||
|
//
|
||||||
|
// Injects the specialization for ankerl::unordered_dense::hash.
|
||||||
|
//
|
||||||
|
// Usage:
|
||||||
|
// struct Vector3 { float x, y, z; };
|
||||||
|
// IA_MAKE_HASHABLE(Vector3, &Vector3::x, &Vector3::y, &Vector3::z)
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
#define IA_MAKE_HASHABLE(Type, ...) \
|
||||||
|
template<> struct ankerl::unordered_dense::hash<Type> \
|
||||||
|
{ \
|
||||||
|
using is_avalanching = void; \
|
||||||
|
NO_DISCARD("Hash value should be used") \
|
||||||
|
UINT64 operator()(CONST Type &v) const NOEXCEPT \
|
||||||
|
{ \
|
||||||
|
/* Pass the object and the list of member pointers */ \
|
||||||
|
return IACore::Utils::ComputeHashFlat(v, __VA_ARGS__); \
|
||||||
|
} \
|
||||||
|
};
|
||||||
|
|
||||||
3
Tests/CMakeLists.txt
Normal file
3
Tests/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
add_subdirectory(Unit/)
|
||||||
|
add_subdirectory(Regression/)
|
||||||
0
Tests/Regression/CMakeLists.txt
Normal file
0
Tests/Regression/CMakeLists.txt
Normal file
220
Tests/Unit/BinaryReader.cpp
Normal file
220
Tests/Unit/BinaryReader.cpp
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <IACore/BinaryReader.hpp>
|
||||||
|
|
||||||
|
#include <IACore/IATest.hpp>
|
||||||
|
|
||||||
|
using namespace IACore;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Test Block Definition
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
IAT_BEGIN_BLOCK(Core, BinaryReader)
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 1. Basic Primitive Reading (UINT8)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestReadUint8()
|
||||||
|
{
|
||||||
|
UINT8 data[] = { 0xAA, 0xBB, 0xCC };
|
||||||
|
BinaryReader reader(data);
|
||||||
|
|
||||||
|
// Read First Byte
|
||||||
|
auto val1 = reader.Read<UINT8>();
|
||||||
|
IAT_CHECK(val1.has_value());
|
||||||
|
IAT_CHECK_EQ(*val1, 0xAA);
|
||||||
|
IAT_CHECK_EQ(reader.Cursor(), 1);
|
||||||
|
|
||||||
|
// Read Second Byte
|
||||||
|
auto val2 = reader.Read<UINT8>();
|
||||||
|
IAT_CHECK_EQ(*val2, 0xBB);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 2. Multi-byte Reading (Endianness check)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestReadMultiByte()
|
||||||
|
{
|
||||||
|
// 0x04030201 in Little Endian memory layout
|
||||||
|
UINT8 data[] = { 0x01, 0x02, 0x03, 0x04 };
|
||||||
|
BinaryReader reader(data);
|
||||||
|
|
||||||
|
auto val = reader.Read<UINT32>();
|
||||||
|
IAT_CHECK(val.has_value());
|
||||||
|
|
||||||
|
// Assuming standard x86/ARM Little Endian for this test
|
||||||
|
// If your engine supports Big Endian, you'd check architecture here
|
||||||
|
IAT_CHECK_EQ(*val, 0x04030201);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(reader.Cursor(), 4);
|
||||||
|
IAT_CHECK(reader.IsEOF());
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 3. Floating Point (Approx check)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestReadFloat()
|
||||||
|
{
|
||||||
|
FLOAT32 pi = 3.14159f;
|
||||||
|
// Bit-cast float to bytes for setup
|
||||||
|
UINT8 data[4];
|
||||||
|
std::memcpy(data, &pi, 4);
|
||||||
|
|
||||||
|
BinaryReader reader(data);
|
||||||
|
auto val = reader.Read<FLOAT32>();
|
||||||
|
|
||||||
|
IAT_CHECK(val.has_value());
|
||||||
|
IAT_CHECK_APPROX(*val, pi);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 4. String Reading
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestReadString()
|
||||||
|
{
|
||||||
|
const char* raw = "HelloIA";
|
||||||
|
BinaryReader reader(std::span<const UINT8>((const UINT8*)raw, 7));
|
||||||
|
|
||||||
|
// Read "Hello" (5 bytes)
|
||||||
|
auto str = reader.ReadString(5);
|
||||||
|
IAT_CHECK(str.has_value());
|
||||||
|
IAT_CHECK_EQ(*str, String("Hello"));
|
||||||
|
IAT_CHECK_EQ(reader.Cursor(), 5);
|
||||||
|
|
||||||
|
// Read remaining "IA"
|
||||||
|
auto str2 = reader.ReadString(2);
|
||||||
|
IAT_CHECK_EQ(*str2, String("IA"));
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 5. Batch Buffer Reading
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestReadBuffer()
|
||||||
|
{
|
||||||
|
UINT8 src[] = { 1, 2, 3, 4, 5 };
|
||||||
|
UINT8 dst[3] = { 0 };
|
||||||
|
BinaryReader reader(src);
|
||||||
|
|
||||||
|
// Read 3 bytes into dst
|
||||||
|
auto res = reader.Read(dst, 3);
|
||||||
|
IAT_CHECK(res.has_value());
|
||||||
|
|
||||||
|
// Verify dst content
|
||||||
|
IAT_CHECK_EQ(dst[0], 1);
|
||||||
|
IAT_CHECK_EQ(dst[1], 2);
|
||||||
|
IAT_CHECK_EQ(dst[2], 3);
|
||||||
|
|
||||||
|
// Verify cursor
|
||||||
|
IAT_CHECK_EQ(reader.Cursor(), 3);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 6. Navigation (Seek, Skip, Remaining)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestNavigation()
|
||||||
|
{
|
||||||
|
UINT8 data[10] = { 0 }; // Zero init
|
||||||
|
BinaryReader reader(data);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(reader.Remaining(), 10);
|
||||||
|
|
||||||
|
// Skip
|
||||||
|
reader.Skip(5);
|
||||||
|
IAT_CHECK_EQ(reader.Cursor(), 5);
|
||||||
|
IAT_CHECK_EQ(reader.Remaining(), 5);
|
||||||
|
|
||||||
|
// Skip clamping
|
||||||
|
reader.Skip(100); // Should clamp to 10
|
||||||
|
IAT_CHECK_EQ(reader.Cursor(), 10);
|
||||||
|
IAT_CHECK(reader.IsEOF());
|
||||||
|
|
||||||
|
// Seek
|
||||||
|
reader.Seek(2);
|
||||||
|
IAT_CHECK_EQ(reader.Cursor(), 2);
|
||||||
|
IAT_CHECK_EQ(reader.Remaining(), 8);
|
||||||
|
IAT_CHECK_NOT(reader.IsEOF());
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 7. Error Handling (EOF Protection)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestBoundaryChecks()
|
||||||
|
{
|
||||||
|
UINT8 data[] = { 0x00, 0x00 };
|
||||||
|
BinaryReader reader(data);
|
||||||
|
|
||||||
|
// Valid read
|
||||||
|
UNUSED(reader.Read<UINT16>());
|
||||||
|
IAT_CHECK(reader.IsEOF());
|
||||||
|
|
||||||
|
// Invalid Read Primitive
|
||||||
|
auto val = reader.Read<UINT8>();
|
||||||
|
IAT_CHECK_NOT(val.has_value()); // Should be unexpected
|
||||||
|
|
||||||
|
// Invalid Read String
|
||||||
|
auto str = reader.ReadString(1);
|
||||||
|
IAT_CHECK_NOT(str.has_value());
|
||||||
|
|
||||||
|
// Invalid Batch Read
|
||||||
|
UINT8 buf[1];
|
||||||
|
auto batch = reader.Read(buf, 1);
|
||||||
|
IAT_CHECK_NOT(batch.has_value());
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Registration
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
IAT_BEGIN_TEST_LIST()
|
||||||
|
IAT_ADD_TEST(TestReadUint8);
|
||||||
|
IAT_ADD_TEST(TestReadMultiByte);
|
||||||
|
IAT_ADD_TEST(TestReadFloat);
|
||||||
|
IAT_ADD_TEST(TestReadString);
|
||||||
|
IAT_ADD_TEST(TestReadBuffer);
|
||||||
|
IAT_ADD_TEST(TestNavigation);
|
||||||
|
IAT_ADD_TEST(TestBoundaryChecks);
|
||||||
|
IAT_END_TEST_LIST()
|
||||||
|
|
||||||
|
IAT_END_BLOCK()
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
|
||||||
|
// Define runner (StopOnFail=false, Verbose=true)
|
||||||
|
ia::iatest::runner<false, true> testRunner;
|
||||||
|
|
||||||
|
// Run the BinaryReader block
|
||||||
|
testRunner.testBlock<Core_BinaryReader>();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
222
Tests/Unit/BinaryWriter.cpp
Normal file
222
Tests/Unit/BinaryWriter.cpp
Normal file
@ -0,0 +1,222 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <IACore/BinaryWriter.hpp>
|
||||||
|
|
||||||
|
#include <IACore/IATest.hpp>
|
||||||
|
|
||||||
|
using namespace IACore;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Test Block Definition
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
IAT_BEGIN_BLOCK(Core, BinaryWriter)
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 1. Vector Mode (Dynamic Growth)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestVectorGrowth()
|
||||||
|
{
|
||||||
|
std::vector<UINT8> buffer;
|
||||||
|
// Start empty
|
||||||
|
BinaryWriter writer(buffer);
|
||||||
|
|
||||||
|
// Write 1 Byte
|
||||||
|
writer.Write<UINT8>(0xAA);
|
||||||
|
IAT_CHECK_EQ(buffer.size(), 1);
|
||||||
|
IAT_CHECK_EQ(buffer[0], 0xAA);
|
||||||
|
|
||||||
|
// Write 4 Bytes (UINT32)
|
||||||
|
// 0xDDCCBBAA -> Little Endian: AA BB CC DD
|
||||||
|
writer.Write<UINT32>(0xDDCCBBAA);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(buffer.size(), 5);
|
||||||
|
|
||||||
|
// Verify Memory Layout
|
||||||
|
IAT_CHECK_EQ(buffer[1], 0xAA);
|
||||||
|
IAT_CHECK_EQ(buffer[2], 0xBB);
|
||||||
|
IAT_CHECK_EQ(buffer[3], 0xCC);
|
||||||
|
IAT_CHECK_EQ(buffer[4], 0xDD);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 2. Vector Mode (Appending to Existing Data)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestVectorAppend()
|
||||||
|
{
|
||||||
|
// Vector starts with existing data
|
||||||
|
std::vector<UINT8> buffer = { 0x01, 0x02 };
|
||||||
|
BinaryWriter writer(buffer);
|
||||||
|
|
||||||
|
// Should append to end, not overwrite 0x01
|
||||||
|
writer.Write<UINT8>(0x03);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(buffer.size(), 3);
|
||||||
|
IAT_CHECK_EQ(buffer[0], 0x01);
|
||||||
|
IAT_CHECK_EQ(buffer[2], 0x03);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 3. Fixed Mode (Span / No Allocation)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestFixedBuffer()
|
||||||
|
{
|
||||||
|
UINT8 rawData[10];
|
||||||
|
// Initialize with zeros
|
||||||
|
std::memset(rawData, 0, 10);
|
||||||
|
|
||||||
|
BinaryWriter writer(rawData); // Implicit conversion to span
|
||||||
|
|
||||||
|
// Write UINT8
|
||||||
|
writer.Write<UINT8>(0xFF);
|
||||||
|
IAT_CHECK_EQ(rawData[0], 0xFF);
|
||||||
|
|
||||||
|
// Write UINT16
|
||||||
|
writer.Write<UINT16>(0x2211);
|
||||||
|
IAT_CHECK_EQ(rawData[1], 0x11);
|
||||||
|
IAT_CHECK_EQ(rawData[2], 0x22);
|
||||||
|
|
||||||
|
// Verify we haven't touched the rest
|
||||||
|
IAT_CHECK_EQ(rawData[3], 0x00);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 4. WriteBytes (Bulk Write)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestWriteBytes()
|
||||||
|
{
|
||||||
|
std::vector<UINT8> buffer;
|
||||||
|
BinaryWriter writer(buffer);
|
||||||
|
|
||||||
|
const char* msg = "IA";
|
||||||
|
writer.WriteBytes((PVOID)msg, 2);
|
||||||
|
writer.Write<UINT8>(0x00); // Null term
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(buffer.size(), 3);
|
||||||
|
IAT_CHECK_EQ(buffer[0], 'I');
|
||||||
|
IAT_CHECK_EQ(buffer[1], 'A');
|
||||||
|
IAT_CHECK_EQ(buffer[2], 0x00);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 5. Random Access (WriteAt)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestRandomAccess()
|
||||||
|
{
|
||||||
|
std::vector<UINT8> buffer;
|
||||||
|
BinaryWriter writer(buffer);
|
||||||
|
|
||||||
|
// Fill with placeholders
|
||||||
|
writer.Write<UINT32>(0xFFFFFFFF);
|
||||||
|
writer.Write<UINT32>(0xFFFFFFFF);
|
||||||
|
|
||||||
|
// Overwrite the first integer with 0x00000000
|
||||||
|
writer.WriteAt<UINT32>(0, 0);
|
||||||
|
|
||||||
|
// Overwrite the byte at index 4 (start of second int)
|
||||||
|
writer.WriteAt<UINT8>(4, 0xAA);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(buffer[0], 0x00);
|
||||||
|
IAT_CHECK_EQ(buffer[3], 0x00);
|
||||||
|
IAT_CHECK_EQ(buffer[4], 0xAA); // Modified
|
||||||
|
IAT_CHECK_EQ(buffer[5], 0xFF); // Unmodified
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 6. Floating Point Writing
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestWriteFloat()
|
||||||
|
{
|
||||||
|
std::vector<UINT8> buffer;
|
||||||
|
BinaryWriter writer(buffer);
|
||||||
|
|
||||||
|
FLOAT32 val = 1.234f;
|
||||||
|
writer.Write<FLOAT32>(val);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(buffer.size(), 4);
|
||||||
|
|
||||||
|
// Read it back via memcpy to verify
|
||||||
|
FLOAT32 readBack;
|
||||||
|
std::memcpy(&readBack, buffer.data(), 4);
|
||||||
|
|
||||||
|
IAT_CHECK_APPROX(readBack, val);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 7. GetPtrAt (Pointer Arithmetic Check)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestGetPtr()
|
||||||
|
{
|
||||||
|
std::vector<UINT8> buffer = { 0x10, 0x20, 0x30 };
|
||||||
|
BinaryWriter writer(buffer);
|
||||||
|
|
||||||
|
PUINT8 p1 = writer.GetPtrAt(1);
|
||||||
|
IAT_CHECK(p1 != nullptr);
|
||||||
|
IAT_CHECK_EQ(*p1, 0x20);
|
||||||
|
|
||||||
|
// Bounds check (Valid boundary)
|
||||||
|
PUINT8 pLast = writer.GetPtrAt(2);
|
||||||
|
IAT_CHECK(pLast != nullptr);
|
||||||
|
|
||||||
|
// Bounds check (Invalid)
|
||||||
|
// Note: We don't test Panic here, but we check if it returns valid ptrs
|
||||||
|
PUINT8 pInvalid = writer.GetPtrAt(3);
|
||||||
|
IAT_CHECK(pInvalid == nullptr);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Registration
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
IAT_BEGIN_TEST_LIST()
|
||||||
|
IAT_ADD_TEST(TestVectorGrowth);
|
||||||
|
IAT_ADD_TEST(TestVectorAppend);
|
||||||
|
IAT_ADD_TEST(TestFixedBuffer);
|
||||||
|
IAT_ADD_TEST(TestWriteBytes);
|
||||||
|
IAT_ADD_TEST(TestRandomAccess);
|
||||||
|
IAT_ADD_TEST(TestWriteFloat);
|
||||||
|
IAT_ADD_TEST(TestGetPtr);
|
||||||
|
IAT_END_TEST_LIST()
|
||||||
|
|
||||||
|
IAT_END_BLOCK()
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
|
||||||
|
// Define runner (StopOnFail=false, Verbose=true)
|
||||||
|
ia::iatest::runner<false, true> testRunner;
|
||||||
|
|
||||||
|
// Run the BinaryReader block
|
||||||
|
testRunner.testBlock<Core_BinaryWriter>();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
39
Tests/Unit/CCompile.c
Normal file
39
Tests/Unit/CCompile.c
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#if defined(__cplusplus)
|
||||||
|
#error "CRITICAL: This file MUST be compiled as C to test ABI compatibility."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <IACore/IACore.hpp>
|
||||||
|
|
||||||
|
#if TRUE != 1
|
||||||
|
#error "TRUE macro is broken in C mode"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
IA_VERSION_TYPE version = IA_MAKE_VERSION(1, 0, 0);
|
||||||
|
|
||||||
|
IA_ASSERT(version > 0);
|
||||||
|
|
||||||
|
UNUSED(version);
|
||||||
|
|
||||||
|
int32_t myNumber = 10;
|
||||||
|
|
||||||
|
(void)myNumber;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
62
Tests/Unit/CMakeLists.txt
Normal file
62
Tests/Unit/CMakeLists.txt
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
# ------------------------------------------------
|
||||||
|
# C Compile Test
|
||||||
|
# ------------------------------------------------
|
||||||
|
enable_language(C)
|
||||||
|
|
||||||
|
add_executable(Test_Unit_CCompile "CCompile.c")
|
||||||
|
|
||||||
|
set_target_properties(Test_Unit_CCompile PROPERTIES
|
||||||
|
C_STANDARD 99
|
||||||
|
C_STANDARD_REQUIRED ON
|
||||||
|
LINKER_LANGUAGE C
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(Test_Unit_CCompile PRIVATE IACore)
|
||||||
|
|
||||||
|
# ------------------------------------------------
|
||||||
|
# Unit: BinaryReader
|
||||||
|
# ------------------------------------------------
|
||||||
|
add_executable(Test_Unit_BinaryReader "BinaryReader.cpp")
|
||||||
|
target_link_libraries(Test_Unit_BinaryReader PRIVATE IACore)
|
||||||
|
target_compile_options(Test_Unit_BinaryReader PRIVATE -fexceptions)
|
||||||
|
set_target_properties(Test_Unit_BinaryReader PROPERTIES USE_EXCEPTIONS ON)
|
||||||
|
|
||||||
|
# ------------------------------------------------
|
||||||
|
# Unit: BinaryWriter
|
||||||
|
# ------------------------------------------------
|
||||||
|
add_executable(Test_Unit_BinaryWriter "BinaryWriter.cpp")
|
||||||
|
target_link_libraries(Test_Unit_BinaryWriter PRIVATE IACore)
|
||||||
|
target_compile_options(Test_Unit_BinaryWriter PRIVATE -fexceptions)
|
||||||
|
set_target_properties(Test_Unit_BinaryWriter PROPERTIES USE_EXCEPTIONS ON)
|
||||||
|
|
||||||
|
# ------------------------------------------------
|
||||||
|
# Unit: Environment
|
||||||
|
# ------------------------------------------------
|
||||||
|
add_executable(Test_Unit_Environment "Environment.cpp")
|
||||||
|
target_link_libraries(Test_Unit_Environment PRIVATE IACore)
|
||||||
|
target_compile_options(Test_Unit_Environment PRIVATE -fexceptions)
|
||||||
|
set_target_properties(Test_Unit_Environment PROPERTIES USE_EXCEPTIONS ON)
|
||||||
|
|
||||||
|
# ------------------------------------------------
|
||||||
|
# Unit: File
|
||||||
|
# ------------------------------------------------
|
||||||
|
add_executable(Test_Unit_File "File.cpp")
|
||||||
|
target_link_libraries(Test_Unit_File PRIVATE IACore)
|
||||||
|
target_compile_options(Test_Unit_File PRIVATE -fexceptions)
|
||||||
|
set_target_properties(Test_Unit_File PROPERTIES USE_EXCEPTIONS ON)
|
||||||
|
|
||||||
|
# ------------------------------------------------
|
||||||
|
# Unit: Process
|
||||||
|
# ------------------------------------------------
|
||||||
|
add_executable(Test_Unit_Process "Process.cpp")
|
||||||
|
target_link_libraries(Test_Unit_Process PRIVATE IACore)
|
||||||
|
target_compile_options(Test_Unit_Process PRIVATE -fexceptions)
|
||||||
|
set_target_properties(Test_Unit_Process PROPERTIES USE_EXCEPTIONS ON)
|
||||||
|
|
||||||
|
# ------------------------------------------------
|
||||||
|
# Unit: Utils
|
||||||
|
# ------------------------------------------------
|
||||||
|
add_executable(Test_Unit_Utils "Utils.cpp")
|
||||||
|
target_link_libraries(Test_Unit_Utils PRIVATE IACore)
|
||||||
|
target_compile_options(Test_Unit_Utils PRIVATE -fexceptions)
|
||||||
|
set_target_properties(Test_Unit_Utils PROPERTIES USE_EXCEPTIONS ON)
|
||||||
190
Tests/Unit/Environment.cpp
Normal file
190
Tests/Unit/Environment.cpp
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <IACore/Environment.hpp>
|
||||||
|
|
||||||
|
#include <IACore/IATest.hpp>
|
||||||
|
|
||||||
|
using namespace IACore;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Constants
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// We use a unique prefix to ensure we don't accidentally mess with real
|
||||||
|
// system variables like PATH or HOME.
|
||||||
|
static const char* TEST_KEY = "IA_TEST_ENV_VAR_12345";
|
||||||
|
static const char* TEST_VAL = "Hello World";
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Test Block Definition
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
IAT_BEGIN_BLOCK(Core, Environment)
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 1. Basic Set and Get (The Happy Path)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestBasicCycle()
|
||||||
|
{
|
||||||
|
// 1. Ensure clean slate
|
||||||
|
Environment::Unset(TEST_KEY);
|
||||||
|
IAT_CHECK_NOT(Environment::Exists(TEST_KEY));
|
||||||
|
|
||||||
|
// 2. Set
|
||||||
|
BOOL setRes = Environment::Set(TEST_KEY, TEST_VAL);
|
||||||
|
IAT_CHECK(setRes);
|
||||||
|
IAT_CHECK(Environment::Exists(TEST_KEY));
|
||||||
|
|
||||||
|
// 3. Find (Optional)
|
||||||
|
auto opt = Environment::Find(TEST_KEY);
|
||||||
|
IAT_CHECK(opt.has_value());
|
||||||
|
IAT_CHECK_EQ(*opt, String(TEST_VAL));
|
||||||
|
|
||||||
|
// 4. Get (Direct)
|
||||||
|
String val = Environment::Get(TEST_KEY);
|
||||||
|
IAT_CHECK_EQ(val, String(TEST_VAL));
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
Environment::Unset(TEST_KEY);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 2. Overwriting Values
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestOverwrite()
|
||||||
|
{
|
||||||
|
Environment::Set(TEST_KEY, "ValueA");
|
||||||
|
IAT_CHECK_EQ(Environment::Get(TEST_KEY), String("ValueA"));
|
||||||
|
|
||||||
|
// Overwrite
|
||||||
|
Environment::Set(TEST_KEY, "ValueB");
|
||||||
|
IAT_CHECK_EQ(Environment::Get(TEST_KEY), String("ValueB"));
|
||||||
|
|
||||||
|
Environment::Unset(TEST_KEY);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 3. Unset Logic
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestUnset()
|
||||||
|
{
|
||||||
|
Environment::Set(TEST_KEY, "To Be Deleted");
|
||||||
|
IAT_CHECK(Environment::Exists(TEST_KEY));
|
||||||
|
|
||||||
|
BOOL unsetRes = Environment::Unset(TEST_KEY);
|
||||||
|
IAT_CHECK(unsetRes);
|
||||||
|
|
||||||
|
// Verify it is actually gone
|
||||||
|
IAT_CHECK_NOT(Environment::Exists(TEST_KEY));
|
||||||
|
|
||||||
|
// Find should return nullopt
|
||||||
|
auto opt = Environment::Find(TEST_KEY);
|
||||||
|
IAT_CHECK_NOT(opt.has_value());
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 4. Default Value Fallbacks
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestDefaults()
|
||||||
|
{
|
||||||
|
const char* ghostKey = "IA_THIS_KEY_DOES_NOT_EXIST";
|
||||||
|
|
||||||
|
// Ensure it really doesn't exist
|
||||||
|
Environment::Unset(ghostKey);
|
||||||
|
|
||||||
|
// Test Get with implicit default ("")
|
||||||
|
String empty = Environment::Get(ghostKey);
|
||||||
|
IAT_CHECK(empty.empty());
|
||||||
|
|
||||||
|
// Test Get with explicit default
|
||||||
|
String fallback = Environment::Get(ghostKey, "MyDefault");
|
||||||
|
IAT_CHECK_EQ(fallback, String("MyDefault"));
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 5. Empty Strings vs Null/Unset
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// This is a critical edge case.
|
||||||
|
// Does Set(Key, "") create an existing empty variable, or unset it?
|
||||||
|
// Standard POSIX/Windows API behavior is that it EXISTS, but is empty.
|
||||||
|
BOOL TestEmptyValue()
|
||||||
|
{
|
||||||
|
Environment::Set(TEST_KEY, "");
|
||||||
|
|
||||||
|
// It should exist
|
||||||
|
IAT_CHECK(Environment::Exists(TEST_KEY));
|
||||||
|
|
||||||
|
// It should be retrievable as empty string
|
||||||
|
auto opt = Environment::Find(TEST_KEY);
|
||||||
|
IAT_CHECK(opt.has_value());
|
||||||
|
IAT_CHECK(opt->empty());
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
Environment::Unset(TEST_KEY);
|
||||||
|
IAT_CHECK_NOT(Environment::Exists(TEST_KEY));
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 6. Validation / Bad Input
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestBadInput()
|
||||||
|
{
|
||||||
|
// Setting an empty key should fail gracefully
|
||||||
|
BOOL res = Environment::Set("", "Value");
|
||||||
|
IAT_CHECK_NOT(res);
|
||||||
|
|
||||||
|
// Unsetting an empty key should fail
|
||||||
|
BOOL resUnset = Environment::Unset("");
|
||||||
|
IAT_CHECK_NOT(resUnset);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Registration
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
IAT_BEGIN_TEST_LIST()
|
||||||
|
IAT_ADD_TEST(TestBasicCycle);
|
||||||
|
IAT_ADD_TEST(TestOverwrite);
|
||||||
|
IAT_ADD_TEST(TestUnset);
|
||||||
|
IAT_ADD_TEST(TestDefaults);
|
||||||
|
IAT_ADD_TEST(TestEmptyValue);
|
||||||
|
IAT_ADD_TEST(TestBadInput);
|
||||||
|
IAT_END_TEST_LIST()
|
||||||
|
|
||||||
|
IAT_END_BLOCK()
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
|
||||||
|
// Define runner (StopOnFail=false, Verbose=true)
|
||||||
|
ia::iatest::runner<false, true> testRunner;
|
||||||
|
|
||||||
|
// Run the BinaryReader block
|
||||||
|
testRunner.testBlock<Core_Environment>();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
246
Tests/Unit/File.cpp
Normal file
246
Tests/Unit/File.cpp
Normal file
@ -0,0 +1,246 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <IACore/File.hpp>
|
||||||
|
|
||||||
|
#include <IACore/IATest.hpp>
|
||||||
|
|
||||||
|
using namespace IACore;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Test Block Definition
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
IAT_BEGIN_BLOCK(Core, File)
|
||||||
|
|
||||||
|
// Helper to generate a temp path for testing
|
||||||
|
String GetTempPath(const String& filename) {
|
||||||
|
auto path = std::filesystem::temp_directory_path() / filename;
|
||||||
|
return path.string();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper to write raw data for setup (bypassing the class we are testing)
|
||||||
|
void CreateDummyFile(const String& path, const String& content) {
|
||||||
|
std::ofstream out(path);
|
||||||
|
out << content;
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 1. Path Utilities (Pure Logic)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestPathUtils()
|
||||||
|
{
|
||||||
|
// We use forward slashes as std::filesystem handles them cross-platform
|
||||||
|
String complexPath = "assets/textures/hero_diffuse.png";
|
||||||
|
|
||||||
|
// Test ExtractDirectory
|
||||||
|
String dir = File::ExtractDirectory(complexPath);
|
||||||
|
// Note: On Windows this might return "assets\textures", check specific impl if strict
|
||||||
|
// But std::filesystem usually normalizes based on the input string format.
|
||||||
|
IAT_CHECK(dir.find("textures") != String::npos);
|
||||||
|
|
||||||
|
// Test ExtractFilename (With Extension)
|
||||||
|
String nameWithExt = File::ExtractFilename<true>(complexPath);
|
||||||
|
IAT_CHECK_EQ(nameWithExt, String("hero_diffuse.png"));
|
||||||
|
|
||||||
|
// Test ExtractFilename (No Extension / Stem)
|
||||||
|
String nameNoExt = File::ExtractFilename<false>(complexPath);
|
||||||
|
IAT_CHECK_EQ(nameNoExt, String("hero_diffuse"));
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 2. Static Read String
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestStaticReadString()
|
||||||
|
{
|
||||||
|
String path = GetTempPath("ia_test_text.txt");
|
||||||
|
String content = "Hello IA Engine";
|
||||||
|
|
||||||
|
// Arrange
|
||||||
|
CreateDummyFile(path, content);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
auto result = File::ReadToString(path);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
IAT_CHECK(result.has_value());
|
||||||
|
IAT_CHECK_EQ(*result, content);
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
std::filesystem::remove(path);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 3. Static Read Binary (Vector)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestStaticReadVector()
|
||||||
|
{
|
||||||
|
String path = GetTempPath("ia_test_bin.dat");
|
||||||
|
|
||||||
|
// Arrange: Create a binary file manually
|
||||||
|
std::ofstream out(path, std::ios::binary);
|
||||||
|
UINT8 rawBytes[] = { 0xDE, 0xAD, 0xBE, 0xEF };
|
||||||
|
out.write((char*)rawBytes, 4);
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
auto result = File::ReadToVector(path);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
IAT_CHECK(result.has_value());
|
||||||
|
Vector<UINT8>& vec = *result;
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(vec.size(), 4);
|
||||||
|
IAT_CHECK_EQ(vec[0], 0xDE);
|
||||||
|
IAT_CHECK_EQ(vec[3], 0xEF);
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
std::filesystem::remove(path);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 4. Instance Write & Read Loop
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestInstanceWriteRead()
|
||||||
|
{
|
||||||
|
String path = GetTempPath("ia_instance_io.bin");
|
||||||
|
UINT32 flagsWrite = (UINT32)File::EOpenFlags::Write |
|
||||||
|
(UINT32)File::EOpenFlags::Binary |
|
||||||
|
(UINT32)File::EOpenFlags::Trunc;
|
||||||
|
|
||||||
|
// 1. Write
|
||||||
|
{
|
||||||
|
File f;
|
||||||
|
auto res = f.Open(path, flagsWrite);
|
||||||
|
IAT_CHECK(res.has_value());
|
||||||
|
IAT_CHECK(f.IsOpen());
|
||||||
|
|
||||||
|
UINT32 magic = 12345;
|
||||||
|
f.Write(&magic, sizeof(magic));
|
||||||
|
|
||||||
|
f.Close();
|
||||||
|
IAT_CHECK_NOT(f.IsOpen());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Read Back
|
||||||
|
{
|
||||||
|
UINT32 flagsRead = (UINT32)File::EOpenFlags::Read |
|
||||||
|
(UINT32)File::EOpenFlags::Binary;
|
||||||
|
|
||||||
|
File f(path, flagsRead); // Test RAII constructor
|
||||||
|
IAT_CHECK(f.IsOpen());
|
||||||
|
|
||||||
|
UINT32 magicRead = 0;
|
||||||
|
SIZE_T bytesRead = f.Read(&magicRead, sizeof(magicRead));
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(bytesRead, sizeof(UINT32));
|
||||||
|
IAT_CHECK_EQ(magicRead, 12345);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::remove(path);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 5. Append Mode
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestAppendMode()
|
||||||
|
{
|
||||||
|
String path = GetTempPath("ia_append.txt");
|
||||||
|
UINT32 flagsAppend = (UINT32)File::EOpenFlags::Write | (UINT32)File::EOpenFlags::Append;
|
||||||
|
|
||||||
|
// Create initial file
|
||||||
|
CreateDummyFile(path, "A");
|
||||||
|
|
||||||
|
// Open in append mode
|
||||||
|
File f;
|
||||||
|
const auto openResult = f.Open(path, flagsAppend);
|
||||||
|
if(!openResult)
|
||||||
|
{
|
||||||
|
IA_PANIC(openResult.error().c_str())
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
char c = 'B';
|
||||||
|
f.Write(&c, 1);
|
||||||
|
f.Close();
|
||||||
|
|
||||||
|
// Verify content is "AB"
|
||||||
|
auto content = File::ReadToString(path);
|
||||||
|
IAT_CHECK(content.has_value());
|
||||||
|
IAT_CHECK_EQ(*content, String("AB"));
|
||||||
|
|
||||||
|
std::filesystem::remove(path);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 6. Error Handling
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestNonExistentFile()
|
||||||
|
{
|
||||||
|
String ghostPath = GetTempPath("this_does_not_exist.ghost");
|
||||||
|
|
||||||
|
// Ensure it really doesn't exist
|
||||||
|
if(File::Exists(ghostPath)) std::filesystem::remove(ghostPath);
|
||||||
|
|
||||||
|
// Test Static
|
||||||
|
auto resStatic = File::ReadToString(ghostPath);
|
||||||
|
IAT_CHECK_NOT(resStatic.has_value());
|
||||||
|
// Optional: Check error message content
|
||||||
|
// IAT_CHECK(resStatic.error().find("not found") != String::npos);
|
||||||
|
|
||||||
|
// Test Instance
|
||||||
|
File f;
|
||||||
|
auto resInstance = f.Open(ghostPath, (UINT32)File::EOpenFlags::Read);
|
||||||
|
IAT_CHECK_NOT(resInstance.has_value());
|
||||||
|
IAT_CHECK_NOT(f.IsOpen());
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Registration
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
IAT_BEGIN_TEST_LIST()
|
||||||
|
IAT_ADD_TEST(TestPathUtils);
|
||||||
|
IAT_ADD_TEST(TestStaticReadString);
|
||||||
|
IAT_ADD_TEST(TestStaticReadVector);
|
||||||
|
IAT_ADD_TEST(TestInstanceWriteRead);
|
||||||
|
IAT_ADD_TEST(TestAppendMode);
|
||||||
|
IAT_ADD_TEST(TestNonExistentFile);
|
||||||
|
IAT_END_TEST_LIST()
|
||||||
|
|
||||||
|
IAT_END_BLOCK()
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
|
||||||
|
// Define runner (StopOnFail=false, Verbose=true)
|
||||||
|
ia::iatest::runner<false, true> testRunner;
|
||||||
|
|
||||||
|
// Run the BinaryReader block
|
||||||
|
testRunner.testBlock<Core_File>();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
247
Tests/Unit/Process.cpp
Normal file
247
Tests/Unit/Process.cpp
Normal file
@ -0,0 +1,247 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <IACore/Process.hpp>
|
||||||
|
|
||||||
|
#include <IACore/IATest.hpp>
|
||||||
|
|
||||||
|
using namespace IACore;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Platform Abstraction for Test Commands
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
#define CMD_ECHO_EXE "cmd.exe"
|
||||||
|
// Windows requires /c to run a command string
|
||||||
|
#define CMD_ARG_PREFIX "/c"
|
||||||
|
#define NULL_DEVICE "NUL"
|
||||||
|
#else
|
||||||
|
#define CMD_ECHO_EXE "/bin/echo"
|
||||||
|
#define CMD_ARG_PREFIX ""
|
||||||
|
#define NULL_DEVICE "/dev/null"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
IAT_BEGIN_BLOCK(Core, Process)
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 1. Basic Execution (Exit Code 0)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestBasicRun()
|
||||||
|
{
|
||||||
|
// Simple "echo hello"
|
||||||
|
String captured;
|
||||||
|
|
||||||
|
auto result = Process::Run(CMD_ECHO_EXE, CMD_ARG_PREFIX " HelloIA",
|
||||||
|
[&](StringView line) {
|
||||||
|
captured = line;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
IAT_CHECK(result.has_value());
|
||||||
|
IAT_CHECK_EQ(*result, 0); // Exit code 0
|
||||||
|
|
||||||
|
// Note: Echo might add newline, but your LineBuffer trims/handles it.
|
||||||
|
// We check if "HelloIA" is contained or equal.
|
||||||
|
IAT_CHECK(captured.find("HelloIA") != String::npos);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 2. Argument Parsing
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestArguments()
|
||||||
|
{
|
||||||
|
Vector<String> lines;
|
||||||
|
|
||||||
|
// Echo two distinct words.
|
||||||
|
// Windows: cmd.exe /c echo one two
|
||||||
|
// Linux: /bin/echo one two
|
||||||
|
String args = String(CMD_ARG_PREFIX) + " one two";
|
||||||
|
if(args[0] == ' ') args.erase(0, 1); // cleanup space if prefix empty
|
||||||
|
|
||||||
|
auto result = Process::Run(CMD_ECHO_EXE, args,
|
||||||
|
[&](StringView line) {
|
||||||
|
lines.push_back(String(line));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(*result, 0);
|
||||||
|
IAT_CHECK(lines.size() > 0);
|
||||||
|
|
||||||
|
// Output should contain "one two"
|
||||||
|
IAT_CHECK(lines[0].find("one two") != String::npos);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 3. Error / Non-Zero Exit Codes
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestExitCodes()
|
||||||
|
{
|
||||||
|
// We need a command that returns non-zero.
|
||||||
|
// Windows: cmd /c exit 1
|
||||||
|
// Linux: /bin/sh -c "exit 1"
|
||||||
|
|
||||||
|
String cmd, arg;
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
cmd = "cmd.exe";
|
||||||
|
arg = "/c exit 42";
|
||||||
|
#else
|
||||||
|
cmd = "/bin/sh";
|
||||||
|
arg = "-c \"exit 42\""; // quotes needed for sh -c
|
||||||
|
#endif
|
||||||
|
|
||||||
|
auto result = Process::Run(cmd, arg, [](StringView){});
|
||||||
|
|
||||||
|
IAT_CHECK(result.has_value());
|
||||||
|
IAT_CHECK_EQ(*result, 42);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 4. Missing Executable Handling
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestMissingExe()
|
||||||
|
{
|
||||||
|
// Try to run a random string
|
||||||
|
auto result = Process::Run("sdflkjghsdflkjg", "", [](StringView){});
|
||||||
|
|
||||||
|
// Windows: CreateProcess usually fails -> returns unexpected
|
||||||
|
// Linux: execvp fails inside child, returns 127 via waitpid
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
IAT_CHECK_NOT(result.has_value()); // Should be an error string
|
||||||
|
#else
|
||||||
|
// Linux fork succeeds, but execvp fails, returning 127
|
||||||
|
IAT_CHECK(result.has_value());
|
||||||
|
IAT_CHECK_EQ(*result, 127);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 5. Line Buffer Logic (The 4096 split test)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestLargeOutput()
|
||||||
|
{
|
||||||
|
// We need to generate output larger than the internal 4096 buffer
|
||||||
|
// to ensure the "partial line" logic works when a line crosses a buffer boundary.
|
||||||
|
|
||||||
|
// We will construct a python script or shell command to print a massive line.
|
||||||
|
// Cross platform approach: Use Python if available, or just a long echo.
|
||||||
|
// Let's assume 'python3' or 'python' is in path, otherwise skip?
|
||||||
|
// Safer: Use pure shell loop if possible, or just a massive command line arg.
|
||||||
|
|
||||||
|
String massiveString;
|
||||||
|
massiveString.reserve(5000);
|
||||||
|
for(int i=0; i<500; ++i) massiveString += "1234567890"; // 5000 chars
|
||||||
|
|
||||||
|
String cmd, arg;
|
||||||
|
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
cmd = "cmd.exe";
|
||||||
|
// Windows has command line length limits (~8k), 5k is safe.
|
||||||
|
arg = "/c echo " + massiveString;
|
||||||
|
#else
|
||||||
|
cmd = "/bin/echo";
|
||||||
|
arg = massiveString;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
String captured;
|
||||||
|
auto result = Process::Run(cmd, arg,
|
||||||
|
[&](StringView line) {
|
||||||
|
captured += line;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
IAT_CHECK(result.has_value());
|
||||||
|
IAT_CHECK_EQ(*result, 0);
|
||||||
|
|
||||||
|
// If the LineBuffer failed to stitch chunks, the length wouldn't match
|
||||||
|
// or we would get multiple callbacks if we expected 1 line.
|
||||||
|
IAT_CHECK_EQ(captured.length(), massiveString.length());
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 6. Multi-Line Handling
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestMultiLine()
|
||||||
|
{
|
||||||
|
// Windows: cmd /c "echo A && echo B"
|
||||||
|
// Linux: /bin/sh -c "echo A; echo B"
|
||||||
|
|
||||||
|
String cmd, arg;
|
||||||
|
#if IA_PLATFORM_WINDOWS
|
||||||
|
cmd = "cmd.exe";
|
||||||
|
arg = "/c \"echo LineA && echo LineB\"";
|
||||||
|
#else
|
||||||
|
cmd = "/bin/sh";
|
||||||
|
arg = "-c \"echo LineA; echo LineB\"";
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int lineCount = 0;
|
||||||
|
bool foundA = false;
|
||||||
|
bool foundB = false;
|
||||||
|
|
||||||
|
UNUSED(Process::Run(cmd, arg, [&](StringView line) {
|
||||||
|
lineCount++;
|
||||||
|
if (line.find("LineA") != String::npos) foundA = true;
|
||||||
|
if (line.find("LineB") != String::npos) foundB = true;
|
||||||
|
}));
|
||||||
|
|
||||||
|
IAT_CHECK(foundA);
|
||||||
|
IAT_CHECK(foundB);
|
||||||
|
// We expect at least 2 lines.
|
||||||
|
// (Windows sometimes echoes the command itself depending on echo settings, but we check contents)
|
||||||
|
IAT_CHECK(lineCount >= 2);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Registration
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
IAT_BEGIN_TEST_LIST()
|
||||||
|
IAT_ADD_TEST(TestBasicRun);
|
||||||
|
IAT_ADD_TEST(TestArguments);
|
||||||
|
IAT_ADD_TEST(TestExitCodes);
|
||||||
|
IAT_ADD_TEST(TestMissingExe);
|
||||||
|
IAT_ADD_TEST(TestLargeOutput);
|
||||||
|
IAT_ADD_TEST(TestMultiLine);
|
||||||
|
IAT_END_TEST_LIST()
|
||||||
|
|
||||||
|
IAT_END_BLOCK()
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
|
||||||
|
// Define runner (StopOnFail=false, Verbose=true)
|
||||||
|
ia::iatest::runner<false, true> testRunner;
|
||||||
|
|
||||||
|
// Run the BinaryReader block
|
||||||
|
testRunner.testBlock<Core_Process>();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
227
Tests/Unit/Utils.cpp
Normal file
227
Tests/Unit/Utils.cpp
Normal file
@ -0,0 +1,227 @@
|
|||||||
|
// IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <IACore/Utils.hpp>
|
||||||
|
|
||||||
|
#include <IACore/IATest.hpp>
|
||||||
|
|
||||||
|
using namespace IACore;
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Test Structs for Hashing (Must be defined at Global Scope)
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
struct TestVec3 {
|
||||||
|
FLOAT32 x, y, z;
|
||||||
|
|
||||||
|
// Equality operator required for hash maps, though strictly
|
||||||
|
// the hash function itself doesn't need it, it's good practice to test both.
|
||||||
|
bool operator==(const TestVec3& other) const {
|
||||||
|
return x == other.x && y == other.y && z == other.z;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Inject the hash specialization into the ankerl namespace
|
||||||
|
// This proves the macro works structurally
|
||||||
|
IA_MAKE_HASHABLE(TestVec3, &TestVec3::x, &TestVec3::y, &TestVec3::z);
|
||||||
|
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
// Test Block Definition
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
IAT_BEGIN_BLOCK(Core, Utils)
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 1. Binary <-> Hex String Conversion
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestHexConversion()
|
||||||
|
{
|
||||||
|
// A. Binary To Hex
|
||||||
|
UINT8 bin[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0xFF };
|
||||||
|
String hex = IACore::Utils::BinaryToHexString(bin);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(hex, String("DEADBEEF00FF"));
|
||||||
|
|
||||||
|
// B. Hex To Binary (Valid Upper)
|
||||||
|
auto resUpper = IACore::Utils::HexStringToBinary("DEADBEEF00FF");
|
||||||
|
IAT_CHECK(resUpper.has_value());
|
||||||
|
IAT_CHECK_EQ(resUpper->size(), 6);
|
||||||
|
IAT_CHECK_EQ((*resUpper)[0], 0xDE);
|
||||||
|
IAT_CHECK_EQ((*resUpper)[5], 0xFF);
|
||||||
|
|
||||||
|
// C. Hex To Binary (Valid Lower/Mixed)
|
||||||
|
auto resLower = IACore::Utils::HexStringToBinary("deadbeef00ff");
|
||||||
|
IAT_CHECK(resLower.has_value());
|
||||||
|
IAT_CHECK_EQ((*resLower)[0], 0xDE);
|
||||||
|
|
||||||
|
// D. Round Trip Integrity
|
||||||
|
Vector<UINT8> original = { 1, 2, 3, 4, 5 };
|
||||||
|
String s = IACore::Utils::BinaryToHexString(original);
|
||||||
|
auto back = IACore::Utils::HexStringToBinary(s);
|
||||||
|
IAT_CHECK(back.has_value());
|
||||||
|
IAT_CHECK_EQ(original.size(), back->size());
|
||||||
|
IAT_CHECK_EQ(original[2], (*back)[2]);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 2. Hex Error Handling
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestHexErrors()
|
||||||
|
{
|
||||||
|
// Odd Length
|
||||||
|
auto odd = IACore::Utils::HexStringToBinary("ABC");
|
||||||
|
IAT_CHECK_NOT(odd.has_value());
|
||||||
|
// Optional: IAT_CHECK_EQ(odd.error(), "Hex string must have even length");
|
||||||
|
|
||||||
|
// Invalid Characters
|
||||||
|
auto invalid = IACore::Utils::HexStringToBinary("ZZTOP");
|
||||||
|
IAT_CHECK_NOT(invalid.has_value());
|
||||||
|
|
||||||
|
// Empty string is valid (empty vector)
|
||||||
|
auto empty = IACore::Utils::HexStringToBinary("");
|
||||||
|
IAT_CHECK(empty.has_value());
|
||||||
|
IAT_CHECK_EQ(empty->size(), 0);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 3. Algorithms: Sorting
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestSort()
|
||||||
|
{
|
||||||
|
Vector<int> nums = { 5, 1, 4, 2, 3 };
|
||||||
|
|
||||||
|
IACore::Utils::Sort(nums);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(nums[0], 1);
|
||||||
|
IAT_CHECK_EQ(nums[1], 2);
|
||||||
|
IAT_CHECK_EQ(nums[2], 3);
|
||||||
|
IAT_CHECK_EQ(nums[3], 4);
|
||||||
|
IAT_CHECK_EQ(nums[4], 5);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 4. Algorithms: Binary Search (Left/Right)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestBinarySearch()
|
||||||
|
{
|
||||||
|
// Must be sorted for Binary Search
|
||||||
|
Vector<int> nums = { 10, 20, 20, 20, 30 };
|
||||||
|
|
||||||
|
// Search Left (Lower Bound) -> First element >= value
|
||||||
|
auto itLeft = IACore::Utils::BinarySearchLeft(nums, 20);
|
||||||
|
IAT_CHECK(itLeft != nums.end());
|
||||||
|
IAT_CHECK_EQ(*itLeft, 20);
|
||||||
|
IAT_CHECK_EQ(std::distance(nums.begin(), itLeft), 1); // Index 1 is first 20
|
||||||
|
|
||||||
|
// Search Right (Upper Bound) -> First element > value
|
||||||
|
auto itRight = IACore::Utils::BinarySearchRight(nums, 20);
|
||||||
|
IAT_CHECK(itRight != nums.end());
|
||||||
|
IAT_CHECK_EQ(*itRight, 30); // Points to 30
|
||||||
|
IAT_CHECK_EQ(std::distance(nums.begin(), itRight), 4); // Index 4
|
||||||
|
|
||||||
|
// Search for non-existent
|
||||||
|
auto itFail = IACore::Utils::BinarySearchLeft(nums, 99);
|
||||||
|
IAT_CHECK(itFail == nums.end());
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 5. Hashing Basics
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestHashBasics()
|
||||||
|
{
|
||||||
|
UINT64 h1 = IACore::Utils::ComputeHash(10, 20.5f, "Hello");
|
||||||
|
UINT64 h2 = IACore::Utils::ComputeHash(10, 20.5f, "Hello");
|
||||||
|
UINT64 h3 = IACore::Utils::ComputeHash(10, 20.5f, "World");
|
||||||
|
|
||||||
|
// Determinism
|
||||||
|
IAT_CHECK_EQ(h1, h2);
|
||||||
|
|
||||||
|
// Differentiation
|
||||||
|
IAT_CHECK_NEQ(h1, h3);
|
||||||
|
|
||||||
|
// Order sensitivity (Golden ratio combine should care about order)
|
||||||
|
// Hash(A, B) != Hash(B, A)
|
||||||
|
UINT64 orderA = IACore::Utils::ComputeHash(1, 2);
|
||||||
|
UINT64 orderB = IACore::Utils::ComputeHash(2, 1);
|
||||||
|
IAT_CHECK_NEQ(orderA, orderB);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// 6. Macro Verification (IA_MAKE_HASHABLE)
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
BOOL TestHashMacro()
|
||||||
|
{
|
||||||
|
TestVec3 v1 { 1.0f, 2.0f, 3.0f };
|
||||||
|
TestVec3 v2 { 1.0f, 2.0f, 3.0f };
|
||||||
|
TestVec3 v3 { 1.0f, 2.0f, 4.0f }; // Slight change
|
||||||
|
|
||||||
|
// Instantiate the hasher manually to verify the struct specialization exists
|
||||||
|
ankerl::unordered_dense::hash<TestVec3> hasher;
|
||||||
|
|
||||||
|
UINT64 h1 = hasher(v1);
|
||||||
|
UINT64 h2 = hasher(v2);
|
||||||
|
UINT64 h3 = hasher(v3);
|
||||||
|
|
||||||
|
IAT_CHECK_EQ(h1, h2); // Same content = same hash
|
||||||
|
IAT_CHECK_NEQ(h1, h3); // Different content = different hash
|
||||||
|
|
||||||
|
// Verify it works with ComputeHash when passed as object
|
||||||
|
// (Assuming you extend ComputeHash to handle custom types via the specialized hasher)
|
||||||
|
// Since ComputeHash uses ankerl::unordered_dense::hash<T> internally, this should work:
|
||||||
|
UINT64 hWrapper = IACore::Utils::ComputeHash(v1);
|
||||||
|
IAT_CHECK_EQ(h1, hWrapper);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
// Registration
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
IAT_BEGIN_TEST_LIST()
|
||||||
|
IAT_ADD_TEST(TestHexConversion);
|
||||||
|
IAT_ADD_TEST(TestHexErrors);
|
||||||
|
IAT_ADD_TEST(TestSort);
|
||||||
|
IAT_ADD_TEST(TestBinarySearch);
|
||||||
|
IAT_ADD_TEST(TestHashBasics);
|
||||||
|
IAT_ADD_TEST(TestHashMacro);
|
||||||
|
IAT_END_TEST_LIST()
|
||||||
|
|
||||||
|
IAT_END_BLOCK()
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
UNUSED(argc);
|
||||||
|
UNUSED(argv);
|
||||||
|
|
||||||
|
// Define runner (StopOnFail=false, Verbose=true)
|
||||||
|
ia::iatest::runner<false, true> testRunner;
|
||||||
|
|
||||||
|
// Run the BinaryReader block
|
||||||
|
testRunner.testBlock<Core_Utils>();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
26
Tools/Builder/Build.py
Normal file
26
Tools/Builder/Build.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# IACore-OSS; The Core Library for All IA Open Source Projects
|
||||||
|
# Copyright (C) 20245IAS (ias@iasoft.dev)
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def main(args: list[str]):
|
||||||
|
os.system("cmake -S. -B./Build/Linux -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug -DIA_BUILD_TESTS=ON")
|
||||||
|
os.system("cmake --build ./Build/Linux")
|
||||||
|
|
||||||
|
main(sys.argv)
|
||||||
8
Vendor/CMakeLists.txt
vendored
Normal file
8
Vendor/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
add_subdirectory(expected/)
|
||||||
|
|
||||||
|
set(MI_BUILD_STATIC ON)
|
||||||
|
set(MI_BUILD_SHARED ON)
|
||||||
|
add_subdirectory(mimalloc/)
|
||||||
|
|
||||||
|
add_subdirectory(unordered_dense/)
|
||||||
1
Vendor/expected
vendored
Submodule
1
Vendor/expected
vendored
Submodule
Submodule Vendor/expected added at 1770e3559f
1
Vendor/mimalloc
vendored
Submodule
1
Vendor/mimalloc
vendored
Submodule
Submodule Vendor/mimalloc added at 09a27098aa
1
Vendor/unordered_dense
vendored
Submodule
1
Vendor/unordered_dense
vendored
Submodule
Submodule Vendor/unordered_dense added at 3234af2c03
Reference in New Issue
Block a user