From cce155b094e51ef46701b3f8c64de4295e21d7fa Mon Sep 17 00:00:00 2001 From: Nikita Fediuchin Date: Tue, 30 Jan 2024 21:19:57 +0200 Subject: [PATCH] Add total and free RAM getters, add tests --- .gitignore | 3 + CMakeLists.txt | 13 +++ Doxyfile | 2 +- README.md | 1 + docs/annotated.html | 4 +- docs/classes.html | 4 +- docs/classmpio_1_1_directory.html | 4 +- docs/classmpio_1_1_o_s.html | 59 +++++++--- .../dir_0af74dce6ed792f14cfe74ff7f287b3e.html | 4 +- .../dir_50ef7a3c260ecc04b67ae703ebb5de74.html | 4 +- .../dir_5cec92fd47c0900910bbc2480fdfd7be.html | 4 +- .../dir_6044ab57abda4ea3e0dfc9336fd9f6a3.html | 4 +- .../dir_d44c64559bbebec7f509842c48db8b23.html | 4 +- docs/directory_8h.html | 4 +- docs/directory_8hpp.html | 4 +- docs/doxygen_crawl.html | 6 +- docs/file_8h.html | 4 +- docs/files.html | 4 +- docs/functions.html | 7 +- docs/functions_func.html | 7 +- docs/globals.html | 7 +- docs/globals_defs.html | 4 +- docs/globals_func.html | 7 +- docs/index.html | 8 +- docs/md__b_u_i_l_d_i_n_g.html | 4 +- docs/navtreeindex0.js | 5 +- docs/os_8h.html | 53 ++++++--- docs/os_8h.js | 3 +- docs/os_8hpp.html | 4 +- docs/pages.html | 4 +- docs/sitemap.xml | 10 +- include/mpio/os.h | 20 +++- source/os.c | 35 ++++-- tests/test_directory.c | 104 ++++++++++++++++++ tests/test_os.c | 99 +++++++++++++++++ wrappers/cpp/mpio/os.hpp | 23 +++- 36 files changed, 429 insertions(+), 107 deletions(-) create mode 100644 tests/test_directory.c create mode 100644 tests/test_os.c diff --git a/.gitignore b/.gitignore index fc06e7c..35d16f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Created by https://www.toptal.com/developers/gitignore/api/c,c++,cmake,macos,linux,windows # Edit at https://www.toptal.com/developers/gitignore?templates=c,c++,cmake,macos,linux,windows +### Custom ### +build/ + ### C ### # Prerequisites *.d diff --git a/CMakeLists.txt b/CMakeLists.txt index d572b17..08f94f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED TRUE) option(MPIO_BUILD_SHARED "Build MPIO shared library" ON) +option(MPIO_BUILD_TESTS "Build MPIO library tests" ON) if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") enable_language(OBJC) @@ -63,3 +64,15 @@ if(MPIO_BUILD_SHARED) "-framework Foundation -framework CoreFoundation") endif() endif() + +if(MPIO_BUILD_TESTS) + enable_testing() + + add_executable(TestMpioDirectory tests/test_directory.c) + target_link_libraries(TestMpioDirectory PUBLIC mpio-static) + add_test(NAME TestMpioDirectory COMMAND TestMpioDirectory) + + add_executable(TestMpioOS tests/test_os.c) + target_link_libraries(TestMpioOS PUBLIC mpio-static) + add_test(NAME TestMpioOS COMMAND TestMpioOS) +endif() \ No newline at end of file diff --git a/Doxyfile b/Doxyfile index d3b4d02..01ff75e 100644 --- a/Doxyfile +++ b/Doxyfile @@ -48,7 +48,7 @@ PROJECT_NAME = MPIO # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = v1.2.1 +PROJECT_NUMBER = v1.3.0 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/README.md b/README.md index 8fd803d..80172e5 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Use building [instructions](BUILDING.md) to install all required tools and libra | Name | Description | Default value | |-------------------|---------------------------|---------------| | MPIO_BUILD_SHARED | Build MPIO shared library | `ON` | +| MPIO_BUILD_TESTS | Build MPIO library tests | `ON` | ## Cloning diff --git a/docs/annotated.html b/docs/annotated.html index 37d81c5..2a1746e 100644 --- a/docs/annotated.html +++ b/docs/annotated.html @@ -27,7 +27,7 @@ -
MPIO v1.2.1 +
MPIO v1.3.0
@@ -68,7 +68,7 @@ diff --git a/docs/classes.html b/docs/classes.html index 768941c..fa4c664 100644 --- a/docs/classes.html +++ b/docs/classes.html @@ -27,7 +27,7 @@ -
MPIO v1.2.1 +
MPIO v1.3.0
@@ -70,7 +70,7 @@ diff --git a/docs/classmpio_1_1_directory.html b/docs/classmpio_1_1_directory.html index e63f747..cad6b96 100644 --- a/docs/classmpio_1_1_directory.html +++ b/docs/classmpio_1_1_directory.html @@ -27,7 +27,7 @@ -
MPIO v1.2.1 +
MPIO v1.3.0
@@ -208,7 +208,7 @@

    - +

diff --git a/docs/classmpio_1_1_o_s.html b/docs/classmpio_1_1_o_s.html index 2c82916..5a9889b 100644 --- a/docs/classmpio_1_1_o_s.html +++ b/docs/classmpio_1_1_o_s.html @@ -27,7 +27,7 @@ -
MPIO v1.2.1 +
MPIO v1.3.0
@@ -70,13 +70,16 @@  Returns high resolution time stamp in seconds. (MT-Safe)
  static int getCpuCount () noexcept - Returns running system logical CPU count. (MT-Safe)
+ Returns system logical CPU count. (MT-Safe)
  -static int64_t getRamSize () noexcept - Returns running system total RAM size. (MT-Safe)
-  +static int64_t getTotalRamSize () noexcept + Returns system total physical RAM size. (MT-Safe)
+  +static int64_t getFreeRamSize () noexcept + Returns system free physical RAM size. (MT-Safe)
+  static string getCpuName () - Returns running system CPU name string. (MT-Safe)
+ Returns system CPU name string. (MT-Safe)
 

Detailed Description

@@ -134,13 +137,13 @@

-

Returns running system logical CPU count. (MT-Safe)

+

Returns system logical CPU count. (MT-Safe)

See the getCpuCount().

- -

◆ getRamSize()

+ +

◆ getTotalRamSize()

@@ -190,7 +221,7 @@

-

Returns running system CPU name string. (MT-Safe)

+

Returns system CPU name string. (MT-Safe)

See the getCpuName().

Exceptions
@@ -206,7 +237,7 @@

    - +
diff --git a/docs/dir_0af74dce6ed792f14cfe74ff7f287b3e.html b/docs/dir_0af74dce6ed792f14cfe74ff7f287b3e.html index 1156000..6f4be40 100644 --- a/docs/dir_0af74dce6ed792f14cfe74ff7f287b3e.html +++ b/docs/dir_0af74dce6ed792f14cfe74ff7f287b3e.html @@ -27,7 +27,7 @@

@@ -68,7 +68,7 @@ diff --git a/docs/dir_50ef7a3c260ecc04b67ae703ebb5de74.html b/docs/dir_50ef7a3c260ecc04b67ae703ebb5de74.html index 4b5ddb0..a8a31d8 100644 --- a/docs/dir_50ef7a3c260ecc04b67ae703ebb5de74.html +++ b/docs/dir_50ef7a3c260ecc04b67ae703ebb5de74.html @@ -27,7 +27,7 @@ @@ -75,7 +75,7 @@ diff --git a/docs/dir_5cec92fd47c0900910bbc2480fdfd7be.html b/docs/dir_5cec92fd47c0900910bbc2480fdfd7be.html index 3caf911..df6c45b 100644 --- a/docs/dir_5cec92fd47c0900910bbc2480fdfd7be.html +++ b/docs/dir_5cec92fd47c0900910bbc2480fdfd7be.html @@ -27,7 +27,7 @@ @@ -62,7 +62,7 @@ diff --git a/docs/dir_6044ab57abda4ea3e0dfc9336fd9f6a3.html b/docs/dir_6044ab57abda4ea3e0dfc9336fd9f6a3.html index 2129434..e4daa1a 100644 --- a/docs/dir_6044ab57abda4ea3e0dfc9336fd9f6a3.html +++ b/docs/dir_6044ab57abda4ea3e0dfc9336fd9f6a3.html @@ -27,7 +27,7 @@ @@ -72,7 +72,7 @@ diff --git a/docs/dir_d44c64559bbebec7f509842c48db8b23.html b/docs/dir_d44c64559bbebec7f509842c48db8b23.html index c603cda..a0460e4 100644 --- a/docs/dir_d44c64559bbebec7f509842c48db8b23.html +++ b/docs/dir_d44c64559bbebec7f509842c48db8b23.html @@ -27,7 +27,7 @@ @@ -68,7 +68,7 @@ diff --git a/docs/directory_8h.html b/docs/directory_8h.html index 6f4ae7f..bb2b132 100644 --- a/docs/directory_8h.html +++ b/docs/directory_8h.html @@ -27,7 +27,7 @@ @@ -222,7 +222,7 @@

    - +
diff --git a/docs/directory_8hpp.html b/docs/directory_8hpp.html index d95b89e..af84a24 100644 --- a/docs/directory_8hpp.html +++ b/docs/directory_8hpp.html @@ -27,7 +27,7 @@

@@ -79,7 +79,7 @@ diff --git a/docs/doxygen_crawl.html b/docs/doxygen_crawl.html index fd74f7d..ff3335c 100644 --- a/docs/doxygen_crawl.html +++ b/docs/doxygen_crawl.html @@ -8,14 +8,14 @@ - - + + - + diff --git a/docs/file_8h.html b/docs/file_8h.html index 1095e76..bca8a18 100644 --- a/docs/file_8h.html +++ b/docs/file_8h.html @@ -27,7 +27,7 @@ @@ -181,7 +181,7 @@

    - +
diff --git a/docs/files.html b/docs/files.html index fdc7def..2ae31a7 100644 --- a/docs/files.html +++ b/docs/files.html @@ -27,7 +27,7 @@

@@ -75,7 +75,7 @@ diff --git a/docs/functions.html b/docs/functions.html index af4093d..58ad5c9 100644 --- a/docs/functions.html +++ b/docs/functions.html @@ -27,7 +27,7 @@ @@ -59,15 +59,16 @@
  • getCpuName() : OS
  • getCurrentClock() : OS
  • getDataPath() : Directory
  • -
  • getRamSize() : OS
  • +
  • getFreeRamSize() : OS
  • getResourcesPath() : Directory
  • +
  • getTotalRamSize() : OS
  • diff --git a/docs/functions_func.html b/docs/functions_func.html index ee17e40..5628d83 100644 --- a/docs/functions_func.html +++ b/docs/functions_func.html @@ -27,7 +27,7 @@ @@ -59,15 +59,16 @@
  • getCpuName() : OS
  • getCurrentClock() : OS
  • getDataPath() : Directory
  • -
  • getRamSize() : OS
  • +
  • getFreeRamSize() : OS
  • getResourcesPath() : Directory
  • +
  • getTotalRamSize() : OS
  • diff --git a/docs/globals.html b/docs/globals.html index 62b448f..7052a14 100644 --- a/docs/globals.html +++ b/docs/globals.html @@ -27,7 +27,7 @@ @@ -61,8 +61,9 @@
  • getCpuName() : os.h
  • getCurrentClock() : os.h
  • getDataDirectory() : directory.h
  • -
  • getRamSize() : os.h
  • +
  • getFreeRamSize() : os.h
  • getResourcesDirectory() : directory.h
  • +
  • getTotalRamSize() : os.h
  • isDirectoryExists() : directory.h
  • seekFile : file.h
  • tellFile : file.h
  • @@ -72,7 +73,7 @@ diff --git a/docs/globals_defs.html b/docs/globals_defs.html index 4f15541..db281a4 100644 --- a/docs/globals_defs.html +++ b/docs/globals_defs.html @@ -27,7 +27,7 @@ @@ -63,7 +63,7 @@ diff --git a/docs/globals_func.html b/docs/globals_func.html index 45cae6e..7a9668e 100644 --- a/docs/globals_func.html +++ b/docs/globals_func.html @@ -27,7 +27,7 @@ @@ -60,8 +60,9 @@
  • getCpuName() : os.h
  • getCurrentClock() : os.h
  • getDataDirectory() : directory.h
  • -
  • getRamSize() : os.h
  • +
  • getFreeRamSize() : os.h
  • getResourcesDirectory() : directory.h
  • +
  • getTotalRamSize() : os.h
  • isDirectoryExists() : directory.h
  • @@ -69,7 +70,7 @@ diff --git a/docs/index.html b/docs/index.html index 30fced4..aca13d6 100644 --- a/docs/index.html +++ b/docs/index.html @@ -27,7 +27,7 @@ @@ -90,7 +90,9 @@

    - + + +
    runtime_errorif failed to get CPU name.
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    -
    MPIO v1.2.1 +
    MPIO v1.3.0
    Name Description Default value
    MPIO_BUILD_SHARED Build MPIO shared library ON
    MPIO_BUILD_SHARED Build MPIO shared library ON
    MPIO_BUILD_TESTS Build MPIO library tests ON

    Cloning

    @@ -108,7 +110,7 @@

    diff --git a/docs/md__b_u_i_l_d_i_n_g.html b/docs/md__b_u_i_l_d_i_n_g.html index 7d2195b..3bf2941 100644 --- a/docs/md__b_u_i_l_d_i_n_g.html +++ b/docs/md__b_u_i_l_d_i_n_g.html @@ -27,7 +27,7 @@ -
    MPIO v1.2.1 +
    MPIO v1.3.0
    @@ -142,7 +142,7 @@

    diff --git a/docs/navtreeindex0.js b/docs/navtreeindex0.js index 8beff37..c15d1ec 100644 --- a/docs/navtreeindex0.js +++ b/docs/navtreeindex0.js @@ -49,10 +49,11 @@ var NAVTREEINDEX0 = "md__b_u_i_l_d_i_n_g.html#visual-studio-code-vs-code":[5,4,1], "md__b_u_i_l_d_i_n_g.html#windows-1011":[5,1], "os_8h.html":[7,0,0,0,2], -"os_8h.html#a9c51eff74e6dbae939c893f3e7567bfd":[7,0,0,0,2,3], +"os_8h.html#a3e09009ef29e2ff41458d9ea0e26a0cc":[7,0,0,0,2,2], +"os_8h.html#a9c51eff74e6dbae939c893f3e7567bfd":[7,0,0,0,2,4], +"os_8h.html#aa7ffb4dde2f51d9b4f8ca4d22c8f6aba":[7,0,0,0,2,3], "os_8h.html#aa8dadbf5401d2d80893740fdb87961cf":[7,0,0,0,2,0], "os_8h.html#ac7e7eebb4772b4a12fc2b775f6cc9071":[7,0,0,0,2,1], -"os_8h.html#af9a61bb6508cb55e8b3b01723bf656f5":[7,0,0,0,2,2], "os_8hpp.html":[7,0,1,0,0,1], "pages.html":[] }; diff --git a/docs/os_8h.html b/docs/os_8h.html index f6b70d0..4397308 100644 --- a/docs/os_8h.html +++ b/docs/os_8h.html @@ -27,7 +27,7 @@ -
    MPIO v1.2.1 +
    MPIO v1.3.0
    @@ -69,13 +69,16 @@  Returns high resolution time stamp in seconds. (MT-Safe)
      int getCpuCount () - Returns running system logical CPU count. (MT-Safe)
    + Returns system logical CPU count. (MT-Safe)
      -int64_t getRamSize () - Returns running system total RAM size. (MT-Safe)
    -  +int64_t getTotalRamSize () + Returns system total physical RAM size. (MT-Safe)
    +  +int64_t getFreeRamSize () + Returns system free physical RAM size. (MT-Safe)
    +  char * getCpuName () - Returns running system CPU name string. (MT-Safe)
    + Returns system CPU name string. (MT-Safe)
     

    Detailed Description

    @@ -116,19 +119,19 @@

    -

    Returns running system logical CPU count. (MT-Safe)

    +

    Returns system logical CPU count. (MT-Safe)

    Usefull for a thread pool thread count.

    - -

    ◆ getRamSize()

    + +

    ◆ getTotalRamSize()

    + +

    Returns system free physical RAM size. (MT-Safe)

    +

    Usefull for an OS information logging.

    Returns
    The free RAM size in bytes on success, otherwise -1.
    @@ -156,8 +179,8 @@

    -

    Returns running system CPU name string. (MT-Safe)

    -

    Usefull for a OS information logging.

    Note
    You should free() the allocated string manually.
    +

    Returns system CPU name string. (MT-Safe)

    +

    Usefull for an OS information logging.

    Note
    You should free() the allocated string manually.
    Returns
    An allocated CPU name string.

    @@ -168,7 +191,7 @@

    diff --git a/docs/os_8h.js b/docs/os_8h.js index e7f6dad..da6ba74 100644 --- a/docs/os_8h.js +++ b/docs/os_8h.js @@ -2,6 +2,7 @@ var os_8h = [ [ "getCurrentClock", "os_8h.html#aa8dadbf5401d2d80893740fdb87961cf", null ], [ "getCpuCount", "os_8h.html#ac7e7eebb4772b4a12fc2b775f6cc9071", null ], - [ "getRamSize", "os_8h.html#af9a61bb6508cb55e8b3b01723bf656f5", null ], + [ "getTotalRamSize", "os_8h.html#a3e09009ef29e2ff41458d9ea0e26a0cc", null ], + [ "getFreeRamSize", "os_8h.html#aa7ffb4dde2f51d9b4f8ca4d22c8f6aba", null ], [ "getCpuName", "os_8h.html#a9c51eff74e6dbae939c893f3e7567bfd", null ] ]; \ No newline at end of file diff --git a/docs/os_8hpp.html b/docs/os_8hpp.html index c07b378..1aa7dcf 100644 --- a/docs/os_8hpp.html +++ b/docs/os_8hpp.html @@ -27,7 +27,7 @@ -
    MPIO v1.2.1 +
    MPIO v1.3.0
    @@ -79,7 +79,7 @@ diff --git a/docs/pages.html b/docs/pages.html index f0459af..914c58a 100644 --- a/docs/pages.html +++ b/docs/pages.html @@ -27,7 +27,7 @@ -
    MPIO v1.2.1 +
    MPIO v1.3.0
    @@ -66,7 +66,7 @@ diff --git a/docs/sitemap.xml b/docs/sitemap.xml index 5484a35..408d91f 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -5,16 +5,16 @@ xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> - https://cfnptr.github.io/mpio/directory_8h.html + https://cfnptr.github.io/mpio/file_8h.html - https://cfnptr.github.io/mpio/file_8h.html + https://cfnptr.github.io/mpio/directory_8hpp.html https://cfnptr.github.io/mpio/os_8h.html - https://cfnptr.github.io/mpio/directory_8hpp.html + https://cfnptr.github.io/mpio/directory_8h.html https://cfnptr.github.io/mpio/os_8hpp.html @@ -23,10 +23,10 @@ https://cfnptr.github.io/mpio/md__b_u_i_l_d_i_n_g.html - https://cfnptr.github.io/mpio/classmpio_1_1_o_s.html + https://cfnptr.github.io/mpio/classmpio_1_1_directory.html - https://cfnptr.github.io/mpio/classmpio_1_1_directory.html + https://cfnptr.github.io/mpio/classmpio_1_1_o_s.html https://cfnptr.github.io/mpio/dir_0af74dce6ed792f14cfe74ff7f287b3e.html diff --git a/include/mpio/os.h b/include/mpio/os.h index fb25e93..9049a9a 100644 --- a/include/mpio/os.h +++ b/include/mpio/os.h @@ -28,20 +28,28 @@ double getCurrentClock(); /** - * @brief Returns running system logical CPU count. (MT-Safe) + * @brief Returns system logical CPU count. (MT-Safe) * @details Usefull for a thread pool thread count. */ int getCpuCount(); /** - * @brief Returns running system total RAM size. (MT-Safe) - * @details Usefull for a OS information logging. + * @brief Returns system total physical RAM size. (MT-Safe) + * @details Usefull for an OS information logging. + * @return The total RAM size in bytes on success, otherwise -1. */ -int64_t getRamSize(); +int64_t getTotalRamSize(); /** - * @brief Returns running system CPU name string. (MT-Safe) - * @details Usefull for a OS information logging. + * @brief Returns system free physical RAM size. (MT-Safe) + * @details Usefull for an OS information logging. + * @return The free RAM size in bytes on success, otherwise -1. + */ +int64_t getFreeRamSize(); + +/** + * @brief Returns system CPU name string. (MT-Safe) + * @details Usefull for an OS information logging. * @note You should free() the allocated string manually. * @return An allocated CPU name string. */ diff --git a/source/os.c b/source/os.c index 8765b1b..3d2aa4a 100644 --- a/source/os.c +++ b/source/os.c @@ -65,22 +65,43 @@ int getCpuCount() #endif } -int64_t getRamSize() +int64_t getTotalRamSize() { #if __linux__ struct sysinfo info; - if (sysinfo(&info) != 0) return 0; - return info.totalram; + if (sysinfo(&info) != 0) return -1; + return (int64_t)info.totalram * (int64_t)info.mem_unit; #elif __APPLE__ int mib [] = { CTL_HW, HW_MEMSIZE }; int64_t value = 0; size_t length = sizeof(int64_t); - if(sysctl(mib, 2, &value, &length, NULL, 0) != 0) return 0; + if(sysctl(mib, 2, &value, &length, NULL, 0) != 0) return -1; return value; #elif _WIN32 - ULONGLONG value = 0; - if (GetPhysicallyInstalledSystemMemory(&value) != TRUE) return 0; - return value * 1024; + MEMORYSTATUSEX statex; + statex.dwLength = sizeof(statex); + if (GlobalMemoryStatusEx(&statex) == FALSE) return -1; + return statex.ullTotalPhys; +#endif +} + +int64_t getFreeRamSize() +{ +#if __linux__ + struct sysinfo info; + if (sysinfo(&info) != 0) return -1; + return (int64_t)info.freeram * (int64_t)info.mem_unit; +#elif __APPLE__ + mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; + vm_statistics64_data_t vmstat; + if(host_statistics64(mach_host_self(), HOST_VM_INFO64, + (host_info64_t)&vmstat, &count) != KERN_SUCCESS) return -1; + return (int64_t)vmstats.free_count * (int64_t)getpagesize(); +#elif _WIN32 + MEMORYSTATUSEX statex; + statex.dwLength = sizeof(statex); + if (GlobalMemoryStatusEx(&statex) == FALSE) return -1; + return statex.ullAvailPhys; #endif } diff --git a/tests/test_directory.c b/tests/test_directory.c new file mode 100644 index 0000000..0825234 --- /dev/null +++ b/tests/test_directory.c @@ -0,0 +1,104 @@ +// Copyright 2021-2024 Nikita Fediuchin. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "mpio/directory.h" + +#include +#include + +inline static bool testGetDataDirectory() +{ + char* dataDirectory = getDataDirectory(false); + if (!dataDirectory) + { + printf("Failed to get private data directory.\n"); + return false; + } + if (strlen(dataDirectory) == 0) + { + printf("Invalid private data directory.\n"); + return false; + } + free(dataDirectory); + + dataDirectory = getDataDirectory(true); + if (!dataDirectory) + { + printf("Failed to get shared data directory.\n"); + return false; + } + if (strlen(dataDirectory) == 0) + { + printf("Invalid shared data directory.\n"); + return false; + } + free(dataDirectory); + + return true; +} +inline static bool testGetAppDataDirectory() +{ + char* appDataDirectory = getAppDataDirectory("Mpio", false); + if (!appDataDirectory) + { + printf("Failed to get private app data directory.\n"); + return false; + } + if (strlen(appDataDirectory) == 0) + { + printf("Invalid private app data directory.\n"); + return false; + } + free(appDataDirectory); + + appDataDirectory = getAppDataDirectory("Mpio", true); + if (!appDataDirectory) + { + printf("Failed to get shared app data directory.\n"); + return false; + } + if (strlen(appDataDirectory) == 0) + { + printf("Invalid shared app data directory.\n"); + return false; + } + free(appDataDirectory); + + return true; +} +inline static bool testGetResourcesDirectory() +{ + char* resourcesDirectory = getResourcesDirectory(); + if (!resourcesDirectory) + { + printf("Failed to get resources directory.\n"); + return false; + } + if (strlen(resourcesDirectory) == 0) + { + printf("Invalid resources directory.\n"); + return false; + } + free(resourcesDirectory); + + return true; +} + +int main() +{ + bool result = testGetDataDirectory(); + result |= testGetAppDataDirectory(); + result |= testGetResourcesDirectory(); + return result ? EXIT_SUCCESS : EXIT_FAILURE; +} \ No newline at end of file diff --git a/tests/test_os.c b/tests/test_os.c new file mode 100644 index 0000000..5c27e55 --- /dev/null +++ b/tests/test_os.c @@ -0,0 +1,99 @@ +// Copyright 2021-2024 Nikita Fediuchin. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "mpio/os.h" + +#include +#include +#include + +inline static bool testGetCurrentClock() +{ + double startClock = getCurrentClock(); + printf("Current clock: %lf\n", startClock); + fflush(stdout); + double stopClock = getCurrentClock(); + + if (stopClock - startClock <= 0.0) + { + printf("Invalid clock time difference.\n"); + return false; + } + + return true; +} +inline static bool testGetCpuCount() +{ + int cpuCount = getCpuCount(); + if (cpuCount <= 0) + { + printf("Invalid logical CPU count.\n"); + return false; + } + + printf("Logical CPU count: %d\n", cpuCount); + return true; +} +inline static bool testGetTotalRamSize() +{ + int64_t ramSize = getTotalRamSize(); + if (ramSize < 0) + { + printf("Failed to get total RAM size.\n"); + return false; + } + + printf("Total RAM size: %lld\n", ramSize); + return true; +} +inline static bool testGetFreeRamSize() +{ + int64_t ramSize = getFreeRamSize(); + if (ramSize < 0) + { + printf("Failed to get free RAM size.\n"); + return false; + } + + printf("Free RAM size: %lld\n", ramSize); + return true; +} +inline static bool testGetCpuName() +{ + char* cpuName = getCpuName(); + if (!cpuName) + { + printf("Failed to get CPU name.\n"); + return false; + } + if (strlen(cpuName) == 0) + { + printf("Invalid CPU name.\n"); + return false; + } + + printf("CPU name: %s", cpuName); + free(cpuName); + return true; +} + +int main() +{ + bool result = testGetCurrentClock(); + result |= testGetCpuCount(); + result |= testGetTotalRamSize(); + result |= testGetFreeRamSize(); + result |= testGetCpuName(); + return result ? EXIT_SUCCESS : EXIT_FAILURE; +} \ No newline at end of file diff --git a/wrappers/cpp/mpio/os.hpp b/wrappers/cpp/mpio/os.hpp index c17bfba..493988b 100644 --- a/wrappers/cpp/mpio/os.hpp +++ b/wrappers/cpp/mpio/os.hpp @@ -48,7 +48,7 @@ class OS } /** - * @brief Returns running system logical CPU count. (MT-Safe) + * @brief Returns system logical CPU count. (MT-Safe) * @details See the @ref getCpuCount(). */ static int getCpuCount() noexcept @@ -57,16 +57,27 @@ class OS } /** - * @brief Returns running system total RAM size. (MT-Safe) - * @details See the @ref getRamSize(). + * @brief Returns system total physical RAM size. (MT-Safe) + * @details See the @ref getTotalRamSize(). + * @return The total RAM size in bytes on success, otherwise -1. */ - static int64_t getRamSize() noexcept + static int64_t getTotalRamSize() noexcept { - return ::getRamSize(); + return ::getTotalRamSize(); } /** - * @brief Returns running system CPU name string. (MT-Safe) + * @brief Returns system free physical RAM size. (MT-Safe) + * @details See the @ref getFreeRamSize(). + * @return The free RAM size in bytes on success, otherwise -1. + */ + static int64_t getFreeRamSize() noexcept + { + return ::getFreeRamSize(); + } + + /** + * @brief Returns system CPU name string. (MT-Safe) * @details See the @ref getCpuName(). * @throw runtime_error if failed to get CPU name. */