diff --git a/include/juno/buff/buff_api.hpp b/include/juno/buff/buff_api.hpp new file mode 100644 index 00000000..c4c3b85d --- /dev/null +++ b/include/juno/buff/buff_api.hpp @@ -0,0 +1,207 @@ +/* + MIT License + + Copyright (c) 2025 Robin A. Onsay + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. +*/ + +/** + This API has been generated by LibJuno: + https://www.robinonsay.com/libjuno/ +*/ + +/** + This header contains the juno_buff_queue library API + @author +*/ +#ifndef JUNO_BUFF_QUEUE_API_HPP +#define JUNO_BUFF_QUEUE_API_HPP +#include "juno/macros.h" +#include "juno/status.h" +#include "juno/module.h" +#include "juno/module.hpp" +#include "juno/types.h" +#include "buff_queue_api.h" +#include "buff_stack_api.h" +#include + + +template +struct JUNO_BUFF_QUEUE_IMPL_T JUNO_MODULE_DERIVE(JUNO_BUFF_QUEUE_ROOT_T, +private: + T tArrBuff[N]; +public: + static JUNO_RESULT_T> New(JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvFailureUserData) + { + JUNO_BUFF_QUEUE_IMPL_T tNew{}; + tNew.tRoot._pfcnFailureHandler = pfcnFailureHandler; + tNew.tRoot._pvFailurUserData = pvFailureUserData; + tNew.tRoot.iStartIndex = 0; + tNew.tRoot.zCapacity = N; + tNew.tRoot.zLength = 0; + return JUNO_RESULT_T>{JUNO_STATUS_SUCCESS, tNew}; + } + + JUNO_RESULT_T Dequeue() + { + JUNO_RESULT_T tResult{}; + auto iIndexResult = JunoBuff_QueueDequeue(&tRoot); + tResult.tStatus = iIndexResult.tStatus; + ASSERT_SUCCESS(iIndexResult.tStatus, return tResult); + tResult.tSuccess = tArrBuff[iIndexResult.tSuccess]; + return tResult; + } + JUNO_STATUS_T EnqueueByRef(T& tData) + { + auto iIndexResult = JunoBuff_QueueEnqueue(&tRoot); + ASSERT_SUCCESS(iIndexResult.tStatus, return iIndexResult.tStatus); + tArrBuff[iIndexResult.tSuccess] = tData; + return JUNO_STATUS_SUCCESS; + } + JUNO_STATUS_T EnqueueByCopy(T tData) + { + auto iIndexResult = JunoBuff_QueueEnqueue(&tRoot); + ASSERT_SUCCESS(iIndexResult.tStatus, return iIndexResult.tStatus); + tArrBuff[iIndexResult.tSuccess] = tData; + return JUNO_STATUS_SUCCESS; + } + JUNO_RESULT_T Peek() + { + JUNO_RESULT_T tResult{}; + auto iIndexResult = JunoBuff_QueueGetIndex(&tRoot); + tResult.tStatus = iIndexResult.tStatus; + ASSERT_SUCCESS(iIndexResult.tStatus, return tResult); + tResult.tSuccess = tArrBuff[iIndexResult.tSuccess]; + return tResult; + } +); + +template> +struct JUNO_BUFF_QUEUE_T JUNO_MODULE_DERIVE(JUNO_BUFF_QUEUE_ROOT_T, +private: + Q tQueueImpl; +public: + static JUNO_RESULT_T> New(JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvFailureUserData) + { + JUNO_BUFF_QUEUE_T tNew{}; + auto tResult = JUNO_RESULT_T>{JUNO_STATUS_SUCCESS, tNew}; + JUNO_RESULT_T tQResult = Q::New(pfcnFailureHandler, pvFailureUserData); + ASSERT_SUCCESS(tQResult.tStatus, tResult.tStatus = tQResult.tStatus; return tResult); + tNew.tQueueImpl = tQResult.tSuccess; + return tResult; + } + + JUNO_RESULT_T Dequeue() + { + return tQueueImpl.Dequeue(); + } + JUNO_STATUS_T Enqueue(T& tData) + { + return tQueueImpl.EnqueueByRef(tData); + } + JUNO_STATUS_T Enqueue(T tData) + { + return tQueueImpl.EnqueueByCopy(tData); + } + JUNO_RESULT_T Peek() + { + return tQueueImpl.Peek(); + } + +); + +template +struct JUNO_BUFF_STACK_IMPL_T JUNO_MODULE_DERIVE(JUNO_BUFF_STACK_ROOT_T, +private: + T tArrBuff[N]; +public: + static JUNO_RESULT_T> New(JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvFailureUserData) + { + JUNO_BUFF_STACK_IMPL_T tNew{}; + tNew.tRoot._pfcnFailureHandler = pfcnFailureHandler; + tNew.tRoot._pvFailurUserData = pvFailureUserData; + tNew.tRoot.zCapacity = N; + tNew.tRoot.zLength = 0; + return JUNO_RESULT_T>{JUNO_STATUS_SUCCESS, tNew}; + } + + JUNO_RESULT_T Pop() + { + JUNO_RESULT_T tResult{}; + auto iIndexResult = JunoBuff_StackPop(&tRoot); + tResult.tStatus = iIndexResult.tStatus; + ASSERT_SUCCESS(iIndexResult.tStatus, return tResult); + tResult.tSuccess = tArrBuff[iIndexResult.tSuccess]; + return tResult; + } + JUNO_STATUS_T PushByRef(T& tData) + { + auto iIndexResult = JunoBuff_StackPush(&tRoot); + ASSERT_SUCCESS(iIndexResult.tStatus, return iIndexResult.tStatus); + tArrBuff[iIndexResult.tSuccess] = tData; + return JUNO_STATUS_SUCCESS; + } + JUNO_STATUS_T PushByCopy(T tData) + { + auto iIndexResult = JunoBuff_StackPush(&tRoot); + ASSERT_SUCCESS(iIndexResult.tStatus, return iIndexResult.tStatus); + tArrBuff[iIndexResult.tSuccess] = tData; + return JUNO_STATUS_SUCCESS; + } + JUNO_RESULT_T Peek() + { + JUNO_RESULT_T tResult{}; + auto iIndexResult = JunoBuff_StackPeek(&tRoot); + tResult.tStatus = iIndexResult.tStatus; + ASSERT_SUCCESS(iIndexResult.tStatus, return tResult); + tResult.tSuccess = tArrBuff[iIndexResult.tSuccess]; + return tResult; + } +); + +template> +struct JUNO_BUFF_STACK_T JUNO_MODULE_DERIVE(JUNO_BUFF_STACK_ROOT_T, +private: + S tStackImpl; +public: + static JUNO_RESULT_T> New(JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvFailureUserData) + { + JUNO_BUFF_STACK_T tNew{}; + auto tResult = JUNO_RESULT_T>{JUNO_STATUS_SUCCESS, tNew}; + JUNO_RESULT_T tQResult = S::New(pfcnFailureHandler, pvFailureUserData); + ASSERT_SUCCESS(tQResult.tStatus, tResult.tStatus = tQResult.tStatus; return tResult); + tNew.tStackImpl = tQResult.tSuccess; + return tResult; + } + + JUNO_RESULT_T Pop() + { + return tStackImpl.Pop(); + } + JUNO_STATUS_T Push(T& tData) + { + return tStackImpl.PushByRef(tData); + } + JUNO_STATUS_T Push(T tData) + { + return tStackImpl.PushByCopy(tData); + + } + JUNO_RESULT_T Peek() + { + return tStackImpl.Peek; + } +); + +#endif // JUNO_BUFF_QUEUE_API_H + diff --git a/include/juno/buff/buff_queue_api.h b/include/juno/buff/buff_queue_api.h index 83104ae5..67ce19ba 100644 --- a/include/juno/buff/buff_queue_api.h +++ b/include/juno/buff/buff_queue_api.h @@ -27,7 +27,6 @@ #ifndef JUNO_BUFF_QUEUE_API_H #define JUNO_BUFF_QUEUE_API_H #include "juno/macros.h" -#include "juno/memory/memory_api.h" #include "juno/status.h" #include "juno/module.h" #include "juno/types.h" @@ -37,11 +36,9 @@ extern "C" #endif typedef struct JUNO_BUFF_QUEUE_API_TAG JUNO_BUFF_QUEUE_API_T; -typedef struct JUNO_BUFF_QUEUE_TAG JUNO_BUFF_QUEUE_ROOT_T; +typedef struct JUNO_BUFF_QUEUE_ROOT_TAG JUNO_BUFF_QUEUE_ROOT_T; -JUNO_MODULE_DECLARE(JUNO_BUFF_QUEUE_T); - -struct JUNO_BUFF_QUEUE_TAG JUNO_MODULE_ROOT(JUNO_BUFF_QUEUE_ROOT_T, +struct JUNO_BUFF_QUEUE_ROOT_TAG JUNO_MODULE_ROOT(JUNO_BUFF_QUEUE_API_T, size_t iStartIndex; size_t zLength; size_t zCapacity; diff --git a/include/juno/buff/buff_stack_api.h b/include/juno/buff/buff_stack_api.h index 7e18374e..f54f9bdf 100644 --- a/include/juno/buff/buff_stack_api.h +++ b/include/juno/buff/buff_stack_api.h @@ -39,8 +39,6 @@ extern "C" typedef struct JUNO_BUFF_STACK_API_TAG JUNO_BUFF_STACK_API_T; typedef struct JUNO_BUFF_STACK_ROOT_TAG JUNO_BUFF_STACK_ROOT_T; -JUNO_MODULE_DECLARE(JUNO_BUFF_STACK_T); - struct JUNO_BUFF_STACK_ROOT_TAG JUNO_MODULE_ROOT(JUNO_BUFF_STACK_API_T, size_t zLength; size_t zCapacity; diff --git a/include/juno/module.h b/include/juno/module.h index 36148baa..2bf27d80 100644 --- a/include/juno/module.h +++ b/include/juno/module.h @@ -220,6 +220,12 @@ typedef struct NAME_T \ SUCCESS_T tSuccess; \ } NAME_T +#define JUNO_RESULT(SUCCESS_T) \ +{ \ + JUNO_STATUS_T tStatus; \ + SUCCESS_T tSuccess; \ +} + /** * @def JUNO_MODULE_OPTION(NAME_T, SUCCESS_T) * @brief Defines an option type combining a flag to indicate some and a success payload. @@ -233,4 +239,10 @@ typedef struct NAME_T \ SOME_T tSome; \ } NAME_T; +#define JUNO_OPTION(SOME_T) \ +{ \ + bool bIsSome; \ + SOME_T tSome; \ +} + #endif // JUNO_MODULE_H diff --git a/include/juno/module.hpp b/include/juno/module.hpp new file mode 100644 index 00000000..086b4568 --- /dev/null +++ b/include/juno/module.hpp @@ -0,0 +1,38 @@ +/* + MIT License + + Copyright (c) 2025 Robin A. Onsay + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. +*/ +#ifndef JUNO_MODULE_HPP +#define JUNO_MODULE_HPP + +#include "status.h" +#include +#include "module.h" + +template +struct JUNO_RESULT_T +{ + JUNO_STATUS_T tStatus; + T tSuccess; +}; + +template +struct JUNO_OPTION_T +{ + bool bIsSome; + T tSome; +}; + +#endif // JUNO_MODULE_H diff --git a/tests/test_buff.c b/tests/test_buff.c index ec34091d..3d071003 100644 --- a/tests/test_buff.c +++ b/tests/test_buff.c @@ -141,6 +141,8 @@ static void test_queue(void) TEST_ASSERT_EQUAL(iTruth, iTestQueue[iIndex]); iTestQueue[iIndex] = 0; } + tResult = JunoBuff_QueueDequeue(&tQueue); + TEST_ASSERT_NOT_EQUAL(JUNO_STATUS_SUCCESS, tResult.tStatus); } static void test_stack(void) diff --git a/tests/test_buff_cpp.cpp b/tests/test_buff_cpp.cpp new file mode 100644 index 00000000..eecb628e --- /dev/null +++ b/tests/test_buff_cpp.cpp @@ -0,0 +1,71 @@ +#include "juno/status.h" +#include "unity.h" +#include "unity_internals.h" +#include "juno/buff/buff_api.hpp" +#include +#include +#include +#include +#include + + +void setUp(void) +{ + +} +void tearDown(void) +{ + +} + + + +static void test_queue(void) +{ + using BUFF_T = JUNO_BUFF_QUEUE_T; + auto tTestQueueResult = BUFF_T::New(NULL, NULL); + TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tTestQueueResult.tStatus); + auto tTestQueue = tTestQueueResult.tSuccess; + for(size_t i = 0; i < tTestQueue.tRoot.zCapacity; i++) + { + auto tStatus = tTestQueue.Enqueue(i+1); + TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tStatus); + } + auto tStatus = tTestQueue.Enqueue(11); + TEST_ASSERT_NOT_EQUAL(JUNO_STATUS_SUCCESS, tStatus); + for(size_t i = 0; i < tTestQueue.tRoot.zCapacity; i++) + { + auto tResult = tTestQueue.Dequeue(); + TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tResult.tStatus); + TEST_ASSERT_EQUAL(i+1, tResult.tSuccess); + } +} + +static void test_stack(void) +{ + using STACK_T = JUNO_BUFF_STACK_T; + auto tTestQueueResult = STACK_T::New(NULL, NULL); + TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tTestQueueResult.tStatus); + auto tTestStack = tTestQueueResult.tSuccess; + for(size_t i = 0; i < tTestStack.tRoot.zCapacity; i++) + { + auto tStatus = tTestStack.Push(i+1); + TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tStatus); + } + auto tStatus = tTestStack.Push(11); + TEST_ASSERT_NOT_EQUAL(JUNO_STATUS_SUCCESS, tStatus); + for(size_t i = 0; i < tTestStack.tRoot.zCapacity; i++) + { + auto tResult = tTestStack.Pop(); + TEST_ASSERT_EQUAL(JUNO_STATUS_SUCCESS, tResult.tStatus); + TEST_ASSERT_EQUAL(tTestStack.tRoot.zCapacity - i, tResult.tSuccess); + } +} + +int main(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_queue); + RUN_TEST(test_stack); + return UNITY_END(); +}