Skip to content

Commit

Permalink
🔖 λcommon v1.6.11: More maths methods, and C wrapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Sep 22, 2018
1 parent 7cce919 commit 9a3c33b
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 70 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ set(SOURCES_GRAPHICS src/graphics/color.cpp)
set(SOURCES_FILESYSTEM src/system/fs/filesystem.cpp)
set(SOURCES_SERIALIZERS)
set(SOURCES_SYSTEM ${SOURCES_FILESYSTEM} src/system/os.cpp src/system/terminal.cpp src/system/system.cpp src/system/uri.cpp)
set(SOURCES_BASE src/lambdacommon.cpp src/serializable.cpp src/lstring.cpp src/path.cpp src/resources.cpp)
set(SOURCES_BASE src/lambdacommon.cpp src/serializable.cpp src/lstring.cpp src/path.cpp src/resources.cpp src/maths.cpp)
set(SOURCES_FILES ${SOURCES_CONNECTION} ${SOURCES_DOCUMENT} ${SOURCES_GRAPHICS} ${SOURCES_SERIALIZERS} ${SOURCES_SYSTEM} ${SOURCES_BASE})

# Now build the library
Expand Down
4 changes: 2 additions & 2 deletions c_wrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ include_directories(../include)
# All files
# This is the C headers files.
set(C_HEADERS_SYSTEM include/clambdacommon/system/os.h include/clambdacommon/system/terminal.h include/clambdacommon/system/system.h)
set(C_HEADERS_BASE include/clambdacommon/clambdacommon.h include/clambdacommon/lstring.h)
set(C_HEADERS_BASE include/clambdacommon/clambdacommon.h include/clambdacommon/lstring.h include/clambdacommon/maths.h)
set(C_HEADERS_FILES ${C_HEADERS_SYSTEM} ${C_HEADERS_BASE})
# This is the C sources files.
set(C_SOURCES_SYSTEM src/system/os.cpp src/system/terminal.cpp src/system/system.cpp)
set(C_SOURCES_BASE src/clambdacommon.cpp src/lstring.cpp)
set(C_SOURCES_BASE src/clambdacommon.cpp src/lstring.cpp src/maths.cpp)
set(C_SOURCES_FILES ${C_SOURCES_SYSTEM} ${C_SOURCES_BASE})

# Now build the library
Expand Down
2 changes: 2 additions & 0 deletions c_wrapper/include/clambdacommon/clambdacommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
# define CLAMBDA_CYGWIN
#endif

#include "maths.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
38 changes: 38 additions & 0 deletions c_wrapper/include/clambdacommon/maths.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright © 2018 AperLambda <aperlambda@gmail.com>
*
* This file is part of λcommon.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/

#ifndef CLAMBDACOMMON_MATHS_H
#define CLAMBDACOMMON_MATHS_H

#define LCOMMON_PI 3.14159265359

// Define every functions that include templates as preprocessor defines.
#define lcommon_maths_abs(number) (number < 0 ? -(number) : number)
#define lcommon_maths_min(a, b) (a < b ? a : b)
#define lcommon_maths_max(a, b) (a > b ? a : b)
#define lcommon_maths_clamp(number, min, max) lcommon_maths_min(lcommon_maths_max(number, min), max) //maths::min(maths::max(number, min), max)

#ifdef __cplusplus
extern "C"
{
#endif

double lcommon_maths_radians(double degrees);

float lcommon_maths_fradians(float degrees);

double lcommon_maths_degrees(double radians);

float lcommon_maths_fdegrees(float radians);

#ifdef __cplusplus
}
#endif

#endif //CLAMBDACOMMON_MATHS_H
82 changes: 41 additions & 41 deletions c_wrapper/include/clambdacommon/system/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,56 @@ enum lcommon_TermFormatting
/*
* RESET
*/
RESET = 0,
LC_TERM_RESET = 0,
/*
* SET
*/
BOLD = 1,
DIM = 2,
UNDERLINED = 4,
BLINK = 5,
REVERSE = 7,
HIDDEN = 8,
LC_TERM_BOLD = 1,
LC_TERM_DIM = 2,
LC_TERM_UNDERLINED = 4,
LC_TERM_BLINK = 5,
LC_TERM_REVERSE = 7,
LC_TERM_HIDDEN = 8,
/*
* Foreground
*/
DEFAULT_FCOLOR = 39,
BLACK = 30,
RED = 31,
GREEN = 32,
YELLOW = 33,
BLUE = 34,
MAGENTA = 35,
CYAN = 36,
LIGHT_GRAY = 37,
DARK_GRAY = 90,
LIGHT_RED = 91,
LIGHT_GREEN = 92,
LIGHT_YELLOW = 93,
LIGHT_BLUE = 94,
LIGHT_MAGENTA = 95,
LIGHT_CYAN = 96,
WHITE = 97,
LC_TERM_DEFAULT_FCOLOR = 39,
LC_TERM_BLACK = 30,
LC_TERM_RED = 31,
LC_TERM_GREEN = 32,
LC_TERM_YELLOW = 33,
LC_TERM_BLUE = 34,
LC_TERM_MAGENTA = 35,
LC_TERM_CYAN = 36,
LC_TERM_LIGHT_GRAY = 37,
LC_TERM_DARK_GRAY = 90,
LC_TERM_LIGHT_RED = 91,
LC_TERM_LIGHT_GREEN = 92,
LC_TERM_LIGHT_YELLOW = 93,
LC_TERM_LIGHT_BLUE = 94,
LC_TERM_LIGHT_MAGENTA = 95,
LC_TERM_LIGHT_CYAN = 96,
LC_TERM_WHITE = 97,
/*
* Background
*/
DEFAULT_BCOLOR = 49,
B_BLACK = 40,
B_RED = 41,
B_GREEN = 42,
B_YELLOW = 43,
B_BLUE = 44,
B_MAGENTA = 45,
B_CYAN = 46,
B_LIGHT_GRAY = 47,
B_DARK_GRAY = 100,
B_LIGHT_RED = 101,
B_LIGHT_GREEN = 102,
B_LIGHT_YELLOW = 103,
B_LIGHT_BLUE = 104,
B_LIGHT_MAGENTA = 105,
B_LIGHT_CYAN = 106,
B_WHITE = 107
LC_TERM_DEFAULT_BCOLOR = 49,
LC_TERM_B_BLACK = 40,
LC_TERM_B_RED = 41,
LC_TERM_B_GREEN = 42,
LC_TERM_B_YELLOW = 43,
LC_TERM_B_BLUE = 44,
LC_TERM_B_MAGENTA = 45,
LC_TERM_B_CYAN = 46,
LC_TERM_B_LIGHT_GRAY = 47,
LC_TERM_B_DARK_GRAY = 100,
LC_TERM_B_LIGHT_RED = 101,
LC_TERM_B_LIGHT_GREEN = 102,
LC_TERM_B_LIGHT_YELLOW = 103,
LC_TERM_B_LIGHT_BLUE = 104,
LC_TERM_B_LIGHT_MAGENTA = 105,
LC_TERM_B_LIGHT_CYAN = 106,
LC_TERM_B_WHITE = 107
};
typedef enum lcommon_TermFormatting lcommon_TermFormatting;

Expand Down
31 changes: 31 additions & 0 deletions c_wrapper/src/maths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © 2018 AperLambda <aperlambda@gmail.com>
*
* This file is part of λcommon.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/

#include "../include/clambdacommon/maths.h"
#include <lambdacommon/maths.h>

double lcommon_maths_radians(double degrees)
{
return lambdacommon::maths::degrees(degrees);
}

float lcommon_maths_fradians(float degrees)
{
return lambdacommon::maths::degrees(degrees);
}

double lcommon_maths_degrees(double radians)
{
return lambdacommon::maths::radians(radians);
}

float lcommon_maths_fdegrees(float radians)
{
return lambdacommon::maths::radians(radians);
}
63 changes: 46 additions & 17 deletions c_wrapper/tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,86 @@

void printWithColor(const char *print, lcommon_TermFormatting formatting);

#define TEST(NAME, COUNT, INDEX, FUNC) printf("TESTING %s...\n RESULT: ", NAME); \
COUNT++; \
if (FUNC) \
{ \
INDEX++; \
printWithColor("OK.\n", LC_TERM_LIGHT_GREEN); \
} \
else \
printWithColor("FAILED.\n", LC_TERM_LIGHT_RED); \

int main()
{
lcommon_term_setup();
lcommon_term_setTerminalTitle(u8"cλcommon - tests");

printf("Starting clambdacommon-tests with ");
printWithColor("lambdacommon", CYAN);
printWithColor("lambdacommon", LC_TERM_CYAN);
printf(" (C wrapper) v%s\n\n", lcommon_getVersion());

printf("OS running: ");
printWithColor(lcommon_sys_getOSName(), LIGHT_YELLOW);
printWithColor(lcommon_sys_getOSName(), LC_TERM_LIGHT_YELLOW);
printf("\n\n");

printf("Computer DATA:\n");
printf(" Computer Name: ");
lcommon_term_setFormat(LIGHT_YELLOW);
lcommon_term_setFormat(LC_TERM_LIGHT_YELLOW);
printf("%s\n", lcommon_sys_getHostName());
lcommon_term_setFormat(RESET);
lcommon_term_setFormat(LC_TERM_RESET);

printf(" User Name: ");
lcommon_term_setFormat(LIGHT_YELLOW);
lcommon_term_setFormat(LC_TERM_LIGHT_YELLOW);
printf("%s\n", lcommon_sys_getUserName());
lcommon_term_setFormat(RESET);
lcommon_term_setFormat(LC_TERM_RESET);

printf(" User Directory: ");
lcommon_term_setFormats(2, LIGHT_BLUE, BOLD);
lcommon_term_setFormats(2, LC_TERM_LIGHT_BLUE, LC_TERM_BOLD);
printf("%s\n", lcommon_sys_getUserDirectoryStr());
lcommon_term_setFormat(RESET);
lcommon_term_setFormat(LC_TERM_RESET);

printf(" Is run as root: ");
lcommon_term_setFormat(LIGHT_RED);
lcommon_term_setFormat(LC_TERM_LIGHT_RED);
printf("%s\n", lcommon_sys_isProcessRunningAsRoot() ? "true" : "false");
lcommon_term_setFormat(RESET);
lcommon_term_setFormat(LC_TERM_RESET);

printf(" CPU: %s (%u cores)\n", lcommon_sys_getProcessorName(), lcommon_sys_getProcessorCores());

uint64_t totalMem = lcommon_sys_getMemoryTotal();
uint64_t usedMem = lcommon_sys_getMemoryUsed();
uint64_t availableMem = lcommon_sys_getMemoryAvailable();
printf(" OS Physical Memory: ");
lcommon_term_setFormat(LIGHT_GREEN);
lcommon_term_setFormat(LC_TERM_LIGHT_GREEN);
printf("%uMB (%lfGB)\n", (uint32_t) (totalMem / 1048576), totalMem / 1073741824.0);
lcommon_term_setFormat(RESET);
lcommon_term_setFormat(LC_TERM_RESET);
printf(" OS Available Memory: ");
lcommon_term_setFormat(LIGHT_GREEN);
lcommon_term_setFormat(LC_TERM_LIGHT_GREEN);
printf("%uMB (%lfGB)\n", (uint32_t) (availableMem / 1048576), availableMem / 1073741824.0);
lcommon_term_setFormat(RESET);
lcommon_term_setFormat(LC_TERM_RESET);
printf(" OS Used Memory: ");
lcommon_term_setFormat(LIGHT_GREEN);
lcommon_term_setFormat(LC_TERM_LIGHT_GREEN);
printf("%uMB (%lfGB)\n\n", (uint32_t) (usedMem / 1048576), usedMem / 1073741824.0);
lcommon_term_setFormat(RESET);
lcommon_term_setFormat(LC_TERM_RESET);

uint32_t testsCount = 0;
uint32_t testsPassed = 0;

printf("===== MATHS SECTION =====\n");

TEST("lcommon_maths_abs((int) 42)", testsCount, testsPassed, lcommon_maths_abs((int) 42) == 42);

TEST("lcommon_maths_abs((float) -64.0f)", testsCount, testsPassed, lcommon_maths_abs(-64.f) == 64.f);

TEST("lcommon_maths_min(42, 64)", testsCount, testsPassed, lcommon_maths_min(42, 64) == 42);

TEST("lcommon_maths_clamp(128, 0, 255)", testsCount, testsPassed, lcommon_maths_clamp(128, 0, 255) == 128);

TEST("lcommon_maths_clamp(32.f, 0.f, 1.f)", testsCount, testsPassed, lcommon_maths_clamp(32.f, 0.f, 1.f) == 1.f);

printf("Tests results: %u/%u\n", testsPassed, testsCount);
if (testsPassed != testsCount)
return 1;

return 0;
}
Expand All @@ -65,5 +94,5 @@ void printWithColor(const char *print, lcommon_TermFormatting formatting)
{
lcommon_term_setFormat(formatting);
printf("%s", print);
lcommon_term_setFormat(RESET);
lcommon_term_setFormat(LC_TERM_RESET);
}
5 changes: 4 additions & 1 deletion include/lambdacommon/lambdacommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@

#define LAMBDACOMMON_VERSION_MAJOR 1
#define LAMBDACOMMON_VERSION_MINOR 6
#define LAMBDACOMMON_VERSION_PATCH 10
#define LAMBDACOMMON_VERSION_PATCH 11

// Deletes the pointer and sets the variable to null. It's just simpler to write like this.
#define LCOMMON_DELETE_POINTER(pointer) delete pointer; pointer = nullptr;

namespace lambdacommon
{
Expand Down
4 changes: 0 additions & 4 deletions include/lambdacommon/lstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ namespace lambdacommon
*/
extern long LAMBDACOMMON_API parseLong(const std::string &longNumber, int base = 10);

#ifdef LAMBDA_WINDOWS

/**
* Converts a std::wstring to a std::string.
* @param wstring The std::wstring to convert.
Expand All @@ -141,8 +139,6 @@ namespace lambdacommon
* @return The converted wstring.
*/
extern std::wstring LAMBDACOMMON_API convertStringToWString(std::string string);

#endif
}
}

Expand Down
Loading

0 comments on commit 9a3c33b

Please sign in to comment.