diff options
Diffstat (limited to 'libparc/parc/statistics')
-rw-r--r-- | libparc/parc/statistics/parc_BasicStats.c | 259 | ||||
-rwxr-xr-x | libparc/parc/statistics/parc_BasicStats.h | 466 | ||||
-rw-r--r-- | libparc/parc/statistics/parc_EWMA.c | 207 | ||||
-rwxr-xr-x | libparc/parc/statistics/parc_EWMA.h | 413 | ||||
-rw-r--r-- | libparc/parc/statistics/test/CMakeLists.txt | 14 | ||||
-rw-r--r-- | libparc/parc/statistics/test/test_parc_BasicStats.c | 246 | ||||
-rw-r--r-- | libparc/parc/statistics/test/test_parc_EWMA.c | 257 |
7 files changed, 1862 insertions, 0 deletions
diff --git a/libparc/parc/statistics/parc_BasicStats.c b/libparc/parc/statistics/parc_BasicStats.c new file mode 100644 index 00000000..b66bbd7a --- /dev/null +++ b/libparc/parc/statistics/parc_BasicStats.c @@ -0,0 +1,259 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * 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 <config.h> + +#include <math.h> + +#include <parc/algol/parc_Object.h> +#include <parc/algol/parc_DisplayIndented.h> +#include <parc/algol/parc_Memory.h> + +#include <parc/statistics/parc_BasicStats.h> + +struct PARCBasicStats { + int64_t count; + double maximum; + double minimum; + double mean; + double variance; +}; + +static inline bool +_parcBasicStats_FloatEquals(double x, double y, double e) +{ + return fabs(x - y) < e; +} + +static bool +_parcBasicStats_Destructor(PARCBasicStats **instancePtr) +{ + assertNotNull(instancePtr, "Parameter must be a non-null pointer to a PARCBasicStats pointer."); + + return true; +} + +parcObject_ImplementAcquire(parcBasicStats, PARCBasicStats); + +parcObject_ImplementRelease(parcBasicStats, PARCBasicStats); + +parcObject_Override( + PARCBasicStats, PARCObject, + .destructor = (PARCObjectDestructor *) _parcBasicStats_Destructor, + .copy = (PARCObjectCopy *) parcBasicStats_Copy, + .toString = (PARCObjectToString *) parcBasicStats_ToString, + .equals = (PARCObjectEquals *) parcBasicStats_Equals, + .compare = (PARCObjectCompare *) parcBasicStats_Compare, + .hashCode = (PARCObjectHashCode *) parcBasicStats_HashCode, + .toJSON = (PARCObjectToJSON *) parcBasicStats_ToJSON); + + +void +parcBasicStats_AssertValid(const PARCBasicStats *instance) +{ + assertTrue(parcBasicStats_IsValid(instance), + "PARCBasicStats is not valid."); +} + + +PARCBasicStats * +parcBasicStats_Create(void) +{ + PARCBasicStats *result = parcObject_CreateInstance(PARCBasicStats); + + if (result != NULL) { + result->count = 0; + result->mean = 0; + result->variance = 0; + result->maximum = 0; + result->minimum = 0; + } + + return result; +} + +int +parcBasicStats_Compare(const PARCBasicStats *instance, const PARCBasicStats *other) +{ + int result = 0; + + return result; +} + +PARCBasicStats * +parcBasicStats_Copy(const PARCBasicStats *original) +{ + PARCBasicStats *result = parcBasicStats_Create(); + result->count = original->count; + result->mean = original->mean; + result->variance = original->variance; + result->maximum = original->maximum; + result->minimum = original->minimum; + + return result; +} + +void +parcBasicStats_Display(const PARCBasicStats *stats, int indentation) +{ + parcDisplayIndented_PrintLine(indentation, + "PARCBasicStats@%p { .count=%" PRId64 " .minimum=%llf .maximum=%llf .mean=%llf }", + stats, stats->count, stats->minimum, stats->maximum, stats->mean); +} + +bool +parcBasicStats_Equals(const PARCBasicStats *x, const PARCBasicStats *y) +{ + bool result = false; + + if (x == y) { + result = true; + } else if (x == NULL || y == NULL) { + result = false; + } else { + if (x->count == y->count) { + if (_parcBasicStats_FloatEquals(x->maximum, y->maximum, 0.00001)) { + if (_parcBasicStats_FloatEquals(x->minimum, y->minimum, 0.00001)) { + if (_parcBasicStats_FloatEquals(x->mean, y->mean, 0.00001)) { + result = true; + } + } + } + } + } + + return result; +} + +PARCHashCode +parcBasicStats_HashCode(const PARCBasicStats *instance) +{ + PARCHashCode result = 0; + + return result; +} + +bool +parcBasicStats_IsValid(const PARCBasicStats *stats) +{ + bool result = false; + + if (stats != NULL) { + result = true; + } + + return result; +} + +PARCJSON * +parcBasicStats_ToJSON(const PARCBasicStats *stats) +{ + PARCJSON *result = parcJSON_Create(); + + if (result != NULL) { + PARCJSONPair *pair = parcJSONPair_CreateFromDouble("maximum", stats->maximum); + parcJSON_AddPair(result, pair); + parcJSONPair_Release(&pair); + + pair = parcJSONPair_CreateFromDouble("minimum", stats->minimum); + parcJSON_AddPair(result, pair); + parcJSONPair_Release(&pair); + + pair = parcJSONPair_CreateFromDouble("mean", stats->mean); + parcJSON_AddPair(result, pair); + parcJSONPair_Release(&pair); + + pair = parcJSONPair_CreateFromDouble("variance", stats->variance); + parcJSON_AddPair(result, pair); + parcJSONPair_Release(&pair); + + pair = parcJSONPair_CreateFromInteger("count", stats->count); + parcJSON_AddPair(result, pair); + parcJSONPair_Release(&pair); + } + + return result; +} + +char * +parcBasicStats_ToString(const PARCBasicStats *stats) +{ + char *result = parcMemory_Format("PARCBasicStats@%p { .count=%" PRId64 " .minimum=%llf .maximum=%llf .mean=%llf }", + stats, stats->count, stats->minimum, stats->maximum, stats->mean); + + return result; +} + +void +parcBasicStats_Update(PARCBasicStats *stats, double value) +{ + stats->count++; + + if (value > stats->maximum) { + stats->maximum = value; + } + + if (value < stats->minimum) { + stats->minimum = value; + } + + double mean_ = stats->mean; + + double xMinusOldMean = value - mean_; + + stats->mean = mean_ + xMinusOldMean / stats->count; + + double xMinusCurrentMean = value - stats->mean; + + stats->variance = ((stats->variance * (stats->count - 1)) + xMinusOldMean * xMinusCurrentMean) / stats->count; +} + +double +parcBasicStats_Mean(const PARCBasicStats *stats) +{ + return stats->mean; +} + +double +parcBasicStats_Variance(const PARCBasicStats *stats) +{ + return stats->variance; +} + +double +parcBasicStats_StandardDeviation(const PARCBasicStats *stats) +{ + return sqrt(stats->variance); +} + +double +parcBasicStats_Maximum(const PARCBasicStats *stats) +{ + return stats->maximum; +} + +double +parcBasicStats_Minimum(const PARCBasicStats *stats) +{ + return stats->minimum; +} + +double +parcBasicStats_Range(const PARCBasicStats *stats) +{ + return stats->maximum - stats->minimum; +} diff --git a/libparc/parc/statistics/parc_BasicStats.h b/libparc/parc/statistics/parc_BasicStats.h new file mode 100755 index 00000000..5f82e582 --- /dev/null +++ b/libparc/parc/statistics/parc_BasicStats.h @@ -0,0 +1,466 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * 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. + */ + +/** + * @file parc_BasicStats.h + * @ingroup statistics + * @brief A basic descriptive statistics implementation for time-series data. + * + */ +#ifndef PARCLIbrary_parc_IntegerStats +#define PARCLIbrary_parc_IntegerStats +#include <stdbool.h> + +#include <parc/algol/parc_JSON.h> +#include <parc/algol/parc_HashCode.h> + +struct PARCBasicStats; +typedef struct PARCBasicStats PARCBasicStats; + +/** + * Increase the number of references to a `PARCBasicStats` instance. + * + * Note that new `PARCBasicStats` is not created, + * only that the given `PARCBasicStats` reference count is incremented. + * Discard the reference by invoking `parcBasicStats_Release`. + * + * @param [in] instance A pointer to a valid PARCBasicStats instance. + * + * @return The same value as @p instance. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * PARCBasicStats *b = parcBasicStats_Acquire(); + * + * parcBasicStats_Release(&a); + * parcBasicStats_Release(&b); + * } + * @endcode + */ +PARCBasicStats *parcBasicStats_Acquire(const PARCBasicStats *instance); + +#ifdef PARCLIbrary_DISABLE_VALIDATION +# define parcBasicStats_OptionalAssertValid(_instance_) +#else +# define parcBasicStats_OptionalAssertValid(_instance_) parcBasicStats_AssertValid(_instance_) +#endif + +/** + * Assert that the given `PARCBasicStats` instance is valid. + * + * @param [in] instance A pointer to a valid PARCBasicStats instance. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * parcBasicStats_AssertValid(a); + * + * printf("Instance is valid.\n"); + * + * parcBasicStats_Release(&b); + * } + * @endcode + */ +void parcBasicStats_AssertValid(const PARCBasicStats *instance); + +/** + * Create an instance of PARCBasicStats + * + * <#Paragraphs Of Explanation#> + * + * @return non-NULL A pointer to a valid PARCBasicStats instance. + * @return NULL An error occurred. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * parcBasicStats_Release(&a); + * } + * @endcode + */ +PARCBasicStats *parcBasicStats_Create(void); + +/** + * Compares @p instance with @p other for order. + * + * Returns a negative integer, zero, or a positive integer as @p instance + * is less than, equal to, or greater than @p other. + * + * @param [in] instance A pointer to a valid PARCBasicStats instance. + * @param [in] other A pointer to a valid PARCBasicStats instance. + * + * @return <0 Instance is less than @p other. + * @return 0 Instance a and instance b compare the same. + * @return >0 Instance a is greater than instance b. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * PARCBasicStats *b = parcBasicStats_Create(); + * + * if (parcBasicStats_Compare(a, b) == 0) { + * printf("Instances are equal.\n"); + * } + * + * parcBasicStats_Release(&a); + * parcBasicStats_Release(&b); + * } + * @endcode + * + * @see parcBasicStats_Equals + */ +int parcBasicStats_Compare(const PARCBasicStats *instance, const PARCBasicStats *other); + +/** + * Create an independent copy the given `PARCBuffer` + * + * A new buffer is created as a complete copy of the original. + * + * @param [in] original A pointer to a valid PARCBasicStats instance. + * + * @return NULL Memory could not be allocated. + * @return non-NULL A pointer to a new `PARCBasicStats` instance. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * PARCBasicStats *copy = parcBasicStats_Copy(&b); + * + * parcBasicStats_Release(&b); + * parcBasicStats_Release(©); + * } + * @endcode + */ +PARCBasicStats *parcBasicStats_Copy(const PARCBasicStats *original); + +/** + * Print a human readable representation of the given `PARCBasicStats`. + * + * @param [in] instance A pointer to a valid PARCBasicStats instance. + * @param [in] indentation The indentation level to use for printing. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * parcBasicStats_Display(a, 0); + * + * parcBasicStats_Release(&a); + * } + * @endcode + */ +void parcBasicStats_Display(const PARCBasicStats *instance, int indentation); + +/** + * Determine if two `PARCBasicStats` instances are equal. + * + * The following equivalence relations on non-null `PARCBasicStats` instances are maintained: * + * * It is reflexive: for any non-null reference value x, `parcBasicStats_Equals(x, x)` must return true. + * + * * It is symmetric: for any non-null reference values x and y, `parcBasicStats_Equals(x, y)` must return true if and only if + * `parcBasicStats_Equals(y x)` returns true. + * + * * It is transitive: for any non-null reference values x, y, and z, if + * `parcBasicStats_Equals(x, y)` returns true and + * `parcBasicStats_Equals(y, z)` returns true, + * then `parcBasicStats_Equals(x, z)` must return true. + * + * * It is consistent: for any non-null reference values x and y, multiple invocations of `parcBasicStats_Equals(x, y)` + * consistently return true or consistently return false. + * + * * For any non-null reference value x, `parcBasicStats_Equals(x, NULL)` must return false. + * + * @param [in] x A pointer to a valid PARCBasicStats instance. + * @param [in] y A pointer to a valid PARCBasicStats instance. + * + * @return true The instances x and y are equal. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * PARCBasicStats *b = parcBasicStats_Create(); + * + * if (parcBasicStats_Equals(a, b)) { + * printf("Instances are equal.\n"); + * } + * + * parcBasicStats_Release(&a); + * parcBasicStats_Release(&b); + * } + * @endcode + * @see parcBasicStats_HashCode + */ +bool parcBasicStats_Equals(const PARCBasicStats *x, const PARCBasicStats *y); + +/** + * Returns a hash code value for the given instance. + * + * The general contract of `HashCode` is: + * + * Whenever it is invoked on the same instance more than once during an execution of an application, + * the `HashCode` function must consistently return the same value, + * provided no information used in a corresponding comparisons on the instance is modified. + * + * This value need not remain consistent from one execution of an application to another execution of the same application. + * If two instances are equal according to the {@link parcBasicStats_Equals} method, + * then calling the {@link parcBasicStats_HashCode} method on each of the two instances must produce the same integer result. + * + * It is not required that if two instances are unequal according to the + * {@link parcBasicStats_Equals} function, + * then calling the `parcBasicStats_HashCode` + * method on each of the two objects must produce distinct integer results. + * + * @param [in] instance A pointer to a valid PARCBasicStats instance. + * + * @return The hashcode for the given instance. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * PARCHashCode hashValue = parcBasicStats_HashCode(buffer); + * parcBasicStats_Release(&a); + * } + * @endcode + */ +PARCHashCode parcBasicStats_HashCode(const PARCBasicStats *instance); + +/** + * Determine if an instance of `PARCBasicStats` is valid. + * + * Valid means the internal state of the type is consistent with its required current or future behaviour. + * This may include the validation of internal instances of types. + * + * @param [in] instance A pointer to a valid PARCBasicStats instance. + * + * @return true The instance is valid. + * @return false The instance is not valid. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * if (parcBasicStats_IsValid(a)) { + * printf("Instance is valid.\n"); + * } + * + * parcBasicStats_Release(&a); + * } + * @endcode + * + */ +bool parcBasicStats_IsValid(const PARCBasicStats *instance); + +/** + * Release a previously acquired reference to the given `PARCBasicStats` instance, + * decrementing the reference count for the instance. + * + * The pointer to the instance is set to NULL as a side-effect of this function. + * + * If the invocation causes the last reference to the instance to be released, + * the instance is deallocated and the instance's implementation will perform + * additional cleanup and release other privately held references. + * + * @param [in,out] instancePtr A pointer to a pointer to the instance to release. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * parcBasicStats_Release(&a); + * } + * @endcode + */ +void parcBasicStats_Release(PARCBasicStats **instancePtr); + +/** + * Create a `PARCJSON` instance (representation) of the given object. + * + * @param [in] instance A pointer to a valid PARCBasicStats instance. + * + * @return NULL Memory could not be allocated to contain the `PARCJSON` instance. + * @return non-NULL An allocated C string that must be deallocated via parcMemory_Deallocate(). + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * PARCJSON *json = parcBasicStats_ToJSON(a); + * + * char *cString = parcJSON_ToString(json); + * printf("JSON representation: %s\n", cString); + * + * parcMemory_Deallocate(&cString); + * parcJSON_Release(&json); + * + * parcBasicStats_Release(&a); + * } + * @endcode + */ +PARCJSON *parcBasicStats_ToJSON(const PARCBasicStats *instance); + +/** + * Produce a null-terminated string representation of the specified `PARCBasicStats`. + * + * The result must be freed by the caller via {@link parcMemory_Deallocate}. + * + * @param [in] instance A pointer to a valid PARCBasicStats instance. + * + * @return NULL Cannot allocate memory. + * @return non-NULL A pointer to an allocated, null-terminated C string that must be deallocated via {@link parcMemory_Deallocate}. + * + * Example: + * @code + * { + * PARCBasicStats *a = parcBasicStats_Create(); + * + * char *string = parcBasicStats_ToString(a); + * + * parcBasicStats_Release(&a); + * + * parcMemory_Deallocate(&string); + * } + * @endcode + * + * @see parcBasicStats_Display + */ +char *parcBasicStats_ToString(const PARCBasicStats *instance); + +/** + * Add a value to the observed set of values. + * + * @param [in] stats A pointer to a valid `PARCBasicStats` instance. + * + * Example: + * @code + * { + * <#example#> + * } + * @endcode + */ +void parcBasicStats_Update(PARCBasicStats *stats, double value); + +/** + * The arithmetic mean of the set of observed values. + * + * @param [in] stats A pointer to a valid `PARCBasicStats` instance. + * + * @return The arithmetic mean of the set of observed values. + * + * Example: + * @code + * { + * <#example#> + * } + * @endcode + */ +double parcBasicStats_Mean(const PARCBasicStats *stats); + +/** + * The variance of the set of observed values. + * + * @param [in] stats A pointer to a valid `PARCBasicStats` instance. + * + * @return The variance of the set of observed values. + * + * Example: + * @code + * { + * <#example#> + * } + * @endcode + */ +double parcBasicStats_Variance(const PARCBasicStats *stats); + +/** + * The standard deviation of the set of observed values. + * + * @param [in] stats A pointer to a valid `PARCBasicStats` instance. + * + * @return The standard deviation of the set of observed values. + * + * Example: + * @code + * { + * <#example#> + * } + * @endcode + */ +double parcBasicStats_StandardDeviation(const PARCBasicStats *stats); + +/** + * The maximum value of the set of observed values. + * + * @param [in] stats A pointer to a valid `PARCBasicStats` instance. + * + * @return The maximum value of the set of observed values. + * + * Example: + * @code + * { + * <#example#> + * } + * @endcode + */ +double parcBasicStats_Maximum(const PARCBasicStats *stats); + +/** + * The minimum value of the set of observed values. + * + * @param [in] stats A pointer to a valid `PARCBasicStats` instance. + * + * @return The minimum value of the set of observed values. + * + * Example: + * @code + * { + * <#example#> + * } + * @endcode + */ +double parcBasicStats_Minimum(const PARCBasicStats *stats); + +/** + * The arithmetic range of the observed set of values. + * + * @param [in] stats A pointer to a valid `PARCBasicStats` instance. + * + * @return The arithmetic range of the observed set of values. + * + * Example: + * @code + * { + * <#example#> + * } + * @endcode + */ +double parcBasicStats_Range(const PARCBasicStats *stats); +#endif diff --git a/libparc/parc/statistics/parc_EWMA.c b/libparc/parc/statistics/parc_EWMA.c new file mode 100644 index 00000000..20f85d4a --- /dev/null +++ b/libparc/parc/statistics/parc_EWMA.c @@ -0,0 +1,207 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * 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 <config.h> + +#include <math.h> + +#include <parc/algol/parc_Object.h> +#include <parc/algol/parc_DisplayIndented.h> +#include <parc/algol/parc_Memory.h> + +#include <parc/statistics/parc_EWMA.h> + +struct PARCEWMA { + bool initialized; + int64_t value; + double coefficient; + double coefficient_r; +}; + +static inline bool +_parcEWMA_FloatEquals(double x, double y, double e) +{ + return fabs(x - y) < e; +} + +static bool +_parcEWMA_Destructor(PARCEWMA **instancePtr) +{ + assertNotNull(instancePtr, "Parameter must be a non-null pointer to a PARCEWMA pointer."); + + return true; +} + +parcObject_ImplementAcquire(parcEWMA, PARCEWMA); + +parcObject_ImplementRelease(parcEWMA, PARCEWMA); + +parcObject_Override( + PARCEWMA, PARCObject, + .destructor = (PARCObjectDestructor *) _parcEWMA_Destructor, + .copy = (PARCObjectCopy *) parcEWMA_Copy, + .toString = (PARCObjectToString *) parcEWMA_ToString, + .equals = (PARCObjectEquals *) parcEWMA_Equals, + .compare = (PARCObjectCompare *) parcEWMA_Compare, + .hashCode = (PARCObjectHashCode *) parcEWMA_HashCode, + .toJSON = (PARCObjectToJSON *) parcEWMA_ToJSON); + +void +parcEWMA_AssertValid(const PARCEWMA *instance) +{ + assertTrue(parcEWMA_IsValid(instance), + "PARCEWMA is not valid."); +} + +PARCEWMA * +parcEWMA_Create(double coefficient) +{ + PARCEWMA *result = parcObject_CreateInstance(PARCEWMA); + if (result != NULL) { + result->initialized = false; + result->value = 0; + result->coefficient = coefficient; + result->coefficient_r = 1.0 - coefficient; + } + + return result; +} + +int +parcEWMA_Compare(const PARCEWMA *instance, const PARCEWMA *other) +{ + int result = 0; + + if (instance == other) { + result = 0; + } else if (instance == NULL) { + result = -1; + } else if (other == NULL) { + result = 1; + } else { + result = instance->value - other->value; + } + + return result; +} + +PARCEWMA * +parcEWMA_Copy(const PARCEWMA *original) +{ + PARCEWMA *result = parcEWMA_Create(original->coefficient); + result->initialized = original->initialized; + result->value = original->value; + + return result; +} + +void +parcEWMA_Display(const PARCEWMA *ewma, int indentation) +{ + parcDisplayIndented_PrintLine(indentation, + "PARCEWMA@%p { .initialized=%s .coefficient=%lf, .value=%" PRId64 " }", + ewma, + ewma->initialized ? "true" : "false", + ewma->coefficient, + ewma->value); +} + +bool +parcEWMA_Equals(const PARCEWMA *x, const PARCEWMA *y) +{ + bool result = false; + + if (x == y) { + result = true; + } else if (x == NULL || y == NULL) { + result = false; + } else { + if (x->initialized == y->initialized) { + if (_parcEWMA_FloatEquals(x->coefficient, y->coefficient, 0.00001)) { + if (_parcEWMA_FloatEquals(x->value, y->value, 0.00001)) { + result = true; + } + } + } + } + + return result; +} + +PARCHashCode +parcEWMA_HashCode(const PARCEWMA *instance) +{ + PARCHashCode result = 0; + + return result; +} + +bool +parcEWMA_IsValid(const PARCEWMA *instance) +{ + bool result = false; + + if (instance != NULL) { + result = true; + } + + return result; +} + +PARCJSON * +parcEWMA_ToJSON(const PARCEWMA *instance) +{ + PARCJSON *result = parcJSON_Create(); + + if (result != NULL) { + } + + return result; +} + +char * +parcEWMA_ToString(const PARCEWMA *ewma) +{ + char *result = parcMemory_Format("PARCEWMA@%p { .initialized=%s .coefficient=%lf, .value=%" PRId64 " }", + ewma, + ewma->initialized ? "true" : "false", + ewma->coefficient, + ewma->value); + return result; +} + +int64_t +parcEWMA_Update(PARCEWMA *ewma, const int64_t value) +{ + if (ewma->initialized) { + // E_t = a * V + (1 - a) * E_(t-1) + double x = (ewma->coefficient * value); + double y = (ewma->coefficient_r * ewma->value); + + ewma->value = x + y; + } else { + ewma->value = value; + ewma->initialized = true; + } + return ewma->value; +} + +int64_t +parcEWMA_GetValue(const PARCEWMA *ewma) +{ + return ewma->value; +} diff --git a/libparc/parc/statistics/parc_EWMA.h b/libparc/parc/statistics/parc_EWMA.h new file mode 100755 index 00000000..bbcb53df --- /dev/null +++ b/libparc/parc/statistics/parc_EWMA.h @@ -0,0 +1,413 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * 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. + */ + +/** + * @file parc_EWMA.h + * @ingroup statistics + * @brief A simple exponential moving average smoothing filter for integers. + * + * An exponentially weighted moving average (EWMA) is a type of infinite impulse response filter that + * applies weighting factors which decrease exponentially. The weighting for each older datum decreases + * exponentially, never reaching zero. + * + */ +#ifndef PARCLibrary_parc_EWMA +#define PARCLibrary_parc_EWMA +#include <stdbool.h> + +#include <parc/algol/parc_JSON.h> +#include <parc/algol/parc_HashCode.h> + +struct PARCEWMA; +typedef struct PARCEWMA PARCEWMA; + +/** + * Increase the number of references to a `PARCEWMA` instance. + * + * Note that new `PARCEWMA` is not created, + * only that the given `PARCEWMA` reference count is incremented. + * Discard the reference by invoking `parcEWMA_Release`. + * + * @param [in] instance A pointer to a valid PARCEWMA instance. + * + * @return The same value as @p instance. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * PARCEWMA *b = parcEWMA_Acquire(); + * + * parcEWMA_Release(&a); + * parcEWMA_Release(&b); + * } + * @endcode + */ +PARCEWMA *parcEWMA_Acquire(const PARCEWMA *instance); + +#ifdef PARCLibrary_DISABLE_VALIDATION +# define parcEWMA_OptionalAssertValid(_instance_) +#else +# define parcEWMA_OptionalAssertValid(_instance_) parcEWMA_AssertValid(_instance_) +#endif + +/** + * Assert that the given `PARCEWMA` instance is valid. + * + * @param [in] instance A pointer to a valid PARCEWMA instance. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * parcEWMA_AssertValid(a); + * + * printf("Instance is valid.\n"); + * + * parcEWMA_Release(&b); + * } + * @endcode + */ +void parcEWMA_AssertValid(const PARCEWMA *instance); + +/** + * Create an instance of PARCEWMA + * + * The coefficient represents a constant smoothing factor affecting + * the degree of prior samples to be applied upon each new update. + * Typically the the coefficient is _0 < coefficient < 1.0_. + * A higher coefficient discounts older observations faster. + * + * @return non-NULL A pointer to a valid PARCEWMA instance. + * @return NULL An error occurred. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * parcEWMA_Release(&a); + * } + * @endcode + */ +PARCEWMA *parcEWMA_Create(double coefficient); + +/** + * Compares @p instance with @p other for order. + * + * Returns a negative integer, zero, or a positive integer as @p instance + * is less than, equal to, or greater than @p other. + * + * @param [in] instance A pointer to a valid PARCEWMA instance. + * @param [in] other A pointer to a valid PARCEWMA instance. + * + * @return <0 Instance is less than @p other. + * @return 0 Instance a and instance b compare the same. + * @return >0 Instance a is greater than instance b. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * PARCEWMA *b = parcEWMA_Create(0.75); + * + * if (parcEWMA_Compare(a, b) == 0) { + * printf("Instances are equal.\n"); + * } + * + * parcEWMA_Release(&a); + * parcEWMA_Release(&b); + * } + * @endcode + * + * @see parcEWMA_Equals + */ +int parcEWMA_Compare(const PARCEWMA *instance, const PARCEWMA *other); + +/** + * Create an independent copy the given `PARCBuffer` + * + * A new buffer is created as a complete copy of the original. + * + * @param [in] original A pointer to a valid PARCEWMA instance. + * + * @return NULL Memory could not be allocated. + * @return non-NULL A pointer to a new `PARCEWMA` instance. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * PARCEWMA *copy = parcEWMA_Copy(&b); + * + * parcEWMA_Release(&b); + * parcEWMA_Release(©); + * } + * @endcode + */ +PARCEWMA *parcEWMA_Copy(const PARCEWMA *original); + +/** + * Print a human readable representation of the given `PARCEWMA`. + * + * @param [in] instance A pointer to a valid PARCEWMA instance. + * @param [in] indentation The indentation level to use for printing. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * parcEWMA_Display(a, 0); + * + * parcEWMA_Release(&a); + * } + * @endcode + */ +void parcEWMA_Display(const PARCEWMA *instance, int indentation); + +/** + * Determine if two `PARCEWMA` instances are equal. + * + * The following equivalence relations on non-null `PARCEWMA` instances are maintained: * + * * It is reflexive: for any non-null reference value x, `parcEWMA_Equals(x, x)` must return true. + * + * * It is symmetric: for any non-null reference values x and y, `parcEWMA_Equals(x, y)` must return true if and only if + * `parcEWMA_Equals(y x)` returns true. + * + * * It is transitive: for any non-null reference values x, y, and z, if + * `parcEWMA_Equals(x, y)` returns true and + * `parcEWMA_Equals(y, z)` returns true, + * then `parcEWMA_Equals(x, z)` must return true. + * + * * It is consistent: for any non-null reference values x and y, multiple invocations of `parcEWMA_Equals(x, y)` + * consistently return true or consistently return false. + * + * * For any non-null reference value x, `parcEWMA_Equals(x, NULL)` must return false. + * + * @param [in] x A pointer to a valid PARCEWMA instance. + * @param [in] y A pointer to a valid PARCEWMA instance. + * + * @return true The instances x and y are equal. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * PARCEWMA *b = parcEWMA_Create(0.75); + * + * if (parcEWMA_Equals(a, b)) { + * printf("Instances are equal.\n"); + * } + * + * parcEWMA_Release(&a); + * parcEWMA_Release(&b); + * } + * @endcode + * @see parcEWMA_HashCode + */ +bool parcEWMA_Equals(const PARCEWMA *x, const PARCEWMA *y); + +/** + * Returns a hash code value for the given instance. + * + * The general contract of `HashCode` is: + * + * Whenever it is invoked on the same instance more than once during an execution of an application, + * the `HashCode` function must consistently return the same value, + * provided no information used in a corresponding comparisons on the instance is modified. + * + * This value need not remain consistent from one execution of an application to another execution of the same application. + * If two instances are equal according to the {@link parcEWMA_Equals} method, + * then calling the {@link parcEWMA_HashCode} method on each of the two instances must produce the same integer result. + * + * It is not required that if two instances are unequal according to the + * {@link parcEWMA_Equals} function, + * then calling the `parcEWMA_HashCode` + * method on each of the two objects must produce distinct integer results. + * + * @param [in] instance A pointer to a valid PARCEWMA instance. + * + * @return The hashcode for the given instance. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * PARCHashCode hashValue = parcEWMA_HashCode(buffer); + * parcEWMA_Release(&a); + * } + * @endcode + */ +PARCHashCode parcEWMA_HashCode(const PARCEWMA *instance); + +/** + * Determine if an instance of `PARCEWMA` is valid. + * + * Valid means the internal state of the type is consistent with its required current or future behaviour. + * This may include the validation of internal instances of types. + * + * @param [in] instance A pointer to a valid PARCEWMA instance. + * + * @return true The instance is valid. + * @return false The instance is not valid. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * if (parcEWMA_IsValid(a)) { + * printf("Instance is valid.\n"); + * } + * + * parcEWMA_Release(&a); + * } + * @endcode + * + */ +bool parcEWMA_IsValid(const PARCEWMA *instance); + +/** + * Release a previously acquired reference to the given `PARCEWMA` instance, + * decrementing the reference count for the instance. + * + * The pointer to the instance is set to NULL as a side-effect of this function. + * + * If the invocation causes the last reference to the instance to be released, + * the instance is deallocated and the instance's implementation will perform + * additional cleanup and release other privately held references. + * + * @param [in,out] instancePtr A pointer to a pointer to the instance to release. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * parcEWMA_Release(&a); + * } + * @endcode + */ +void parcEWMA_Release(PARCEWMA **instancePtr); + +/** + * Create a `PARCJSON` instance (representation) of the given object. + * + * @param [in] instance A pointer to a valid PARCEWMA instance. + * + * @return NULL Memory could not be allocated to contain the `PARCJSON` instance. + * @return non-NULL An allocated C string that must be deallocated via parcMemory_Deallocate(). + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * PARCJSON *json = parcEWMA_ToJSON(a); + * + * char *cString = parcJSON_ToString(json); + * printf("JSON representation: %s\n", cString); + * + * parcMemory_Deallocate(&string); + * parcJSON_Release(&json); + * + * parcEWMA_Release(&a); + * } + * @endcode + */ +PARCJSON *parcEWMA_ToJSON(const PARCEWMA *instance); + +/** + * Produce a null-terminated string representation of the specified `PARCEWMA`. + * + * The result must be freed by the caller via {@link parcMemory_Deallocate}. + * + * @param [in] instance A pointer to a valid PARCEWMA instance. + * + * @return NULL Cannot allocate memory. + * @return non-NULL A pointer to an allocated, null-terminated C string that must be deallocated via {@link parcMemory_Deallocate}. + * + * Example: + * @code + * { + * PARCEWMA *a = parcEWMA_Create(0.75); + * + * char *string = parcEWMA_ToString(a); + * + * parcEWMA_Release(&a); + * + * parcMemory_Deallocate(&string); + * } + * @endcode + * + * @see parcEWMA_Display + */ +char *parcEWMA_ToString(const PARCEWMA *instance); + +/** + * Update the given `PARCEWMA` filter. + * + * The value of the filter is modified by the input of an updated value + * + * @param [in] ewma A pointer to a valid `PARCEWMA` instance. + * + * @return The current exponentitally smoothed value of the filter. + * + * Example: + * @code + * { + * PARCEWMA *ewma = parcEWMA_Create(0.75); + * + * for (uint64_t i = 0; i < 10; i++) { + * parcEWMA_Update(ewma, i); + * } + * + * int64_t smoothedValue = parcEWMA_GetValue(ewma); + * + * parcEWMA_Release(&ewma); + * } + * @endcode + */ +int64_t parcEWMA_Update(PARCEWMA *ewma, const int64_t value); + +/** + * Get the current exponentitally smoothed value of the filter. + * + * @param [in] ewma A pointer to a valid `PARCEWMA` instance. + * + * @return The current exponentitally smoothed value of the filter. + * + * Example: + * @code + * { + * PARCEWMA *ewma = parcEWMA_Create(0.75); + * + * for (uint64_t i = 0; i < 10; i++) { + * parcEWMA_Update(ewma, i); + * } + * + * int64_t smoothedValue = parcEWMA_GetValue(ewma); + * + * parcEWMA_Release(&ewma); + * } + * @endcode + */ +int64_t parcEWMA_GetValue(const PARCEWMA *ewma); +#endif diff --git a/libparc/parc/statistics/test/CMakeLists.txt b/libparc/parc/statistics/test/CMakeLists.txt new file mode 100644 index 00000000..fde78d5b --- /dev/null +++ b/libparc/parc/statistics/test/CMakeLists.txt @@ -0,0 +1,14 @@ +set(TestsExpectedToPass + test_parc_BasicStats + test_parc_EWMA + ) + +# Enable gcov output for the tests +add_definitions(--coverage) +set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} " --coverage") + +foreach(test ${TestsExpectedToPass}) + AddTest(${test}) +endforeach() + + diff --git a/libparc/parc/statistics/test/test_parc_BasicStats.c b/libparc/parc/statistics/test/test_parc_BasicStats.c new file mode 100644 index 00000000..129e8317 --- /dev/null +++ b/libparc/parc/statistics/test/test_parc_BasicStats.c @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * 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 "../parc_BasicStats.c" + +#include <inttypes.h> +#include <math.h> + +#include <LongBow/testing.h> +#include <LongBow/debugging.h> +#include <parc/algol/parc_Memory.h> +#include <parc/algol/parc_SafeMemory.h> +#include <parc/algol/parc_DisplayIndented.h> + +#include <parc/testing/parc_MemoryTesting.h> +#include <parc/testing/parc_ObjectTesting.h> + +LONGBOW_TEST_RUNNER(parc_BasicStats) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified, but all tests should be idempotent. + // Never rely on the execution order of tests or share state between them. + LONGBOW_RUN_TEST_FIXTURE(CreateAcquireRelease); + LONGBOW_RUN_TEST_FIXTURE(Object); + LONGBOW_RUN_TEST_FIXTURE(Specialization); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(parc_BasicStats) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(parc_BasicStats) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(CreateAcquireRelease) +{ + LONGBOW_RUN_TEST_CASE(CreateAcquireRelease, CreateRelease); +} + +LONGBOW_TEST_FIXTURE_SETUP(CreateAcquireRelease) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(CreateAcquireRelease) +{ + if (!parcMemoryTesting_ExpectedOutstanding(0, "%s leaked memory.", longBowTestCase_GetFullName(testCase))) { + return LONGBOW_STATUS_MEMORYLEAK; + } + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(CreateAcquireRelease, CreateRelease) +{ + PARCBasicStats *instance = parcBasicStats_Create(); + assertNotNull(instance, "Expected non-null result from parcBasicStats_Create();"); + + parcObjectTesting_AssertAcquireReleaseContract(parcBasicStats_Acquire, instance); + + parcBasicStats_Release(&instance); + assertNull(instance, "Expected null result from parcBasicStats_Release();"); +} + +LONGBOW_TEST_FIXTURE(Object) +{ + LONGBOW_RUN_TEST_CASE(Object, parcBasicStats_Compare); + LONGBOW_RUN_TEST_CASE(Object, parcBasicStats_Copy); + LONGBOW_RUN_TEST_CASE(Object, parcBasicStats_Display); + LONGBOW_RUN_TEST_CASE(Object, parcBasicStats_Equals); + LONGBOW_RUN_TEST_CASE(Object, parcBasicStats_HashCode); + LONGBOW_RUN_TEST_CASE(Object, parcBasicStats_IsValid); + LONGBOW_RUN_TEST_CASE(Object, parcBasicStats_ToJSON); + LONGBOW_RUN_TEST_CASE(Object, parcBasicStats_ToString); +} + +LONGBOW_TEST_FIXTURE_SETUP(Object) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Object) +{ + if (!parcMemoryTesting_ExpectedOutstanding(0, "%s mismanaged memory.", longBowTestCase_GetFullName(testCase))) { + return LONGBOW_STATUS_MEMORYLEAK; + } + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Object, parcBasicStats_Compare) +{ + testUnimplemented(""); +} + +LONGBOW_TEST_CASE(Object, parcBasicStats_Copy) +{ + PARCBasicStats *instance = parcBasicStats_Create(); + PARCBasicStats *copy = parcBasicStats_Copy(instance); + assertTrue(parcBasicStats_Equals(instance, copy), "Expected the copy to be equal to the original"); + + parcBasicStats_Release(&instance); + parcBasicStats_Release(©); +} + +LONGBOW_TEST_CASE(Object, parcBasicStats_Display) +{ + PARCBasicStats *instance = parcBasicStats_Create(); + parcBasicStats_Display(instance, 0); + parcBasicStats_Release(&instance); +} + +LONGBOW_TEST_CASE(Object, parcBasicStats_Equals) +{ + PARCBasicStats *x = parcBasicStats_Create(); + PARCBasicStats *y = parcBasicStats_Create(); + PARCBasicStats *z = parcBasicStats_Create(); + + parcObjectTesting_AssertEquals(x, y, z, NULL); + + parcBasicStats_Release(&x); + parcBasicStats_Release(&y); + parcBasicStats_Release(&z); +} + +LONGBOW_TEST_CASE(Object, parcBasicStats_HashCode) +{ + PARCBasicStats *x = parcBasicStats_Create(); + PARCBasicStats *y = parcBasicStats_Create(); + + parcObjectTesting_AssertHashCode(x, y); + + parcBasicStats_Release(&x); + parcBasicStats_Release(&y); +} + +LONGBOW_TEST_CASE(Object, parcBasicStats_IsValid) +{ + PARCBasicStats *instance = parcBasicStats_Create(); + assertTrue(parcBasicStats_IsValid(instance), "Expected parcBasicStats_Create to result in a valid instance."); + + parcBasicStats_Release(&instance); + assertFalse(parcBasicStats_IsValid(instance), "Expected parcBasicStats_Release to result in an invalid instance."); +} + +LONGBOW_TEST_CASE(Object, parcBasicStats_ToJSON) +{ + PARCBasicStats *instance = parcBasicStats_Create(); + + PARCJSON *json = parcBasicStats_ToJSON(instance); + + parcJSON_Release(&json); + + parcBasicStats_Release(&instance); +} + +LONGBOW_TEST_CASE(Object, parcBasicStats_ToString) +{ + PARCBasicStats *instance = parcBasicStats_Create(); + + char *string = parcBasicStats_ToString(instance); + + assertNotNull(string, "Expected non-NULL result from parcBasicStats_ToString"); + + parcMemory_Deallocate((void **) &string); + parcBasicStats_Release(&instance); +} + +LONGBOW_TEST_FIXTURE(Specialization) +{ + LONGBOW_RUN_TEST_CASE(Specialization, parcBasicStats_Update); +} + +LONGBOW_TEST_FIXTURE_SETUP(Specialization) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Specialization) +{ + if (!parcMemoryTesting_ExpectedOutstanding(0, "%s mismanaged memory.", longBowTestCase_GetFullName(testCase))) { + return LONGBOW_STATUS_MEMORYLEAK; + } + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Specialization, parcBasicStats_Update) +{ + PARCBasicStats *stats = parcBasicStats_Create(); + + parcBasicStats_Update(stats, 1); + parcBasicStats_Update(stats, 2); + parcBasicStats_Update(stats, 3); + parcBasicStats_Update(stats, 4); + parcBasicStats_Update(stats, 5); + parcBasicStats_Update(stats, 6); + parcBasicStats_Update(stats, 7); + parcBasicStats_Update(stats, 8); + parcBasicStats_Update(stats, 9); + parcBasicStats_Update(stats, 10); + + double expected = 5.500; + double actual = parcBasicStats_Mean(stats); + assertTrue(fabs(actual - expected) < 0.001, "Expected %lf actual %lf", expected, actual); + + expected = 8.25; + double variance = parcBasicStats_Variance(stats); + assertTrue(fabs(variance - expected) < 0.01, "Expected %lf actual %lf", expected, variance); + + expected = 2.872; + double stddev = parcBasicStats_StandardDeviation(stats); + assertTrue(fabs(stddev - expected) < 0.001, "Expected %lf actual %lf", expected, stddev); + + parcBasicStats_Release(&stats); +} + +int +main(int argc, char *argv[argc]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_BasicStats); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} + + diff --git a/libparc/parc/statistics/test/test_parc_EWMA.c b/libparc/parc/statistics/test/test_parc_EWMA.c new file mode 100644 index 00000000..689f4704 --- /dev/null +++ b/libparc/parc/statistics/test/test_parc_EWMA.c @@ -0,0 +1,257 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * 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 "../parc_EWMA.c" +#include <stdio.h> +#include <inttypes.h> + +#include <LongBow/testing.h> +#include <LongBow/debugging.h> +#include <parc/algol/parc_Memory.h> +#include <parc/algol/parc_SafeMemory.h> +#include <parc/algol/parc_DisplayIndented.h> + +#include <parc/testing/parc_MemoryTesting.h> +#include <parc/testing/parc_ObjectTesting.h> + +LONGBOW_TEST_RUNNER(parc_EWMA) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified, but all tests should be idempotent. + // Never rely on the execution order of tests or share state between them. + LONGBOW_RUN_TEST_FIXTURE(CreateAcquireRelease); + LONGBOW_RUN_TEST_FIXTURE(Object); + LONGBOW_RUN_TEST_FIXTURE(Specialization); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(parc_EWMA) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(parc_EWMA) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(CreateAcquireRelease) +{ + LONGBOW_RUN_TEST_CASE(CreateAcquireRelease, CreateRelease); +} + +LONGBOW_TEST_FIXTURE_SETUP(CreateAcquireRelease) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(CreateAcquireRelease) +{ + if (!parcMemoryTesting_ExpectedOutstanding(0, "%s leaked memory.", longBowTestCase_GetFullName(testCase))) { + return LONGBOW_STATUS_MEMORYLEAK; + } + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(CreateAcquireRelease, CreateRelease) +{ + PARCEWMA *instance = parcEWMA_Create(0.75); + assertNotNull(instance, "Expected non-null result from parcEWMA_Create();"); + + parcObjectTesting_AssertAcquireReleaseContract(parcEWMA_Acquire, instance); + + parcEWMA_Release(&instance); + assertNull(instance, "Expected null result from parcEWMA_Release();"); +} + +LONGBOW_TEST_FIXTURE(Object) +{ + LONGBOW_RUN_TEST_CASE(Object, parcEWMA_Compare); + LONGBOW_RUN_TEST_CASE(Object, parcEWMA_Copy); + LONGBOW_RUN_TEST_CASE(Object, parcEWMA_Display); + LONGBOW_RUN_TEST_CASE(Object, parcEWMA_Equals); + LONGBOW_RUN_TEST_CASE(Object, parcEWMA_HashCode); + LONGBOW_RUN_TEST_CASE(Object, parcEWMA_IsValid); + LONGBOW_RUN_TEST_CASE(Object, parcEWMA_ToJSON); + LONGBOW_RUN_TEST_CASE(Object, parcEWMA_ToString); +} + +LONGBOW_TEST_FIXTURE_SETUP(Object) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Object) +{ + if (!parcMemoryTesting_ExpectedOutstanding(0, "%s mismanaged memory.", longBowTestCase_GetFullName(testCase))) { + return LONGBOW_STATUS_MEMORYLEAK; + } + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Object, parcEWMA_Compare) +{ + PARCEWMA *ewma = parcEWMA_Create(0.75); + PARCEWMA *lesser = parcEWMA_Create(0.75); + PARCEWMA *greater = parcEWMA_Create(0.75); + + parcEWMA_Update(ewma, 5); + parcEWMA_Update(lesser, 1); + parcEWMA_Update(greater, 10); + + parcObjectTesting_AssertCompareToImpl((PARCObjectCompare *) parcEWMA_Compare, + ewma, + (const void *[]) { ewma, NULL }, + (const void *[]) { lesser, NULL }, + (const void *[]) { greater, NULL }); + + parcEWMA_Release(&ewma); + parcEWMA_Release(&lesser); + parcEWMA_Release(&greater); +} + +LONGBOW_TEST_CASE(Object, parcEWMA_Copy) +{ + PARCEWMA *instance = parcEWMA_Create(0.75); + PARCEWMA *copy = parcEWMA_Copy(instance); + assertTrue(parcEWMA_Equals(instance, copy), "Expected the copy to be equal to the original"); + + parcEWMA_Release(&instance); + parcEWMA_Release(©); +} + +LONGBOW_TEST_CASE(Object, parcEWMA_Display) +{ + PARCEWMA *instance = parcEWMA_Create(0.75); + parcEWMA_Display(instance, 0); + parcEWMA_Release(&instance); +} + +LONGBOW_TEST_CASE(Object, parcEWMA_Equals) +{ + PARCEWMA *x = parcEWMA_Create(0.75); + PARCEWMA *y = parcEWMA_Create(0.75); + PARCEWMA *z = parcEWMA_Create(0.75); + + parcObjectTesting_AssertEquals(x, y, z, NULL); + + parcEWMA_Release(&x); + parcEWMA_Release(&y); + parcEWMA_Release(&z); +} + +LONGBOW_TEST_CASE(Object, parcEWMA_HashCode) +{ + PARCEWMA *x = parcEWMA_Create(0.75); + PARCEWMA *y = parcEWMA_Create(0.75); + + parcObjectTesting_AssertHashCode(x, y); + + parcEWMA_Release(&x); + parcEWMA_Release(&y); +} + +LONGBOW_TEST_CASE(Object, parcEWMA_IsValid) +{ + PARCEWMA *instance = parcEWMA_Create(0.75); + assertTrue(parcEWMA_IsValid(instance), "Expected parcEWMA_Create to result in a valid instance."); + + parcEWMA_Release(&instance); + assertFalse(parcEWMA_IsValid(instance), "Expected parcEWMA_Release to result in an invalid instance."); +} + +LONGBOW_TEST_CASE(Object, parcEWMA_ToJSON) +{ + PARCEWMA *instance = parcEWMA_Create(0.75); + + PARCJSON *json = parcEWMA_ToJSON(instance); + + parcJSON_Release(&json); + + parcEWMA_Release(&instance); +} + +LONGBOW_TEST_CASE(Object, parcEWMA_ToString) +{ + PARCEWMA *instance = parcEWMA_Create(0.75); + + char *string = parcEWMA_ToString(instance); + + assertNotNull(string, "Expected non-NULL result from parcEWMA_ToString"); + + parcMemory_Deallocate((void **) &string); + parcEWMA_Release(&instance); +} + +LONGBOW_TEST_FIXTURE(Specialization) +{ + LONGBOW_RUN_TEST_CASE(Specialization, parcEWMA_Update); + LONGBOW_RUN_TEST_CASE(Specialization, parcEWMA_GetValue); +} + +LONGBOW_TEST_FIXTURE_SETUP(Specialization) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Specialization, parcEWMA_Update) +{ + PARCEWMA *instance = parcEWMA_Create(0.25); + + for (int64_t i = 0; i < 100; i++) { + int64_t value = parcEWMA_Update(instance, i); + printf("%" PRId64 " %" PRId64 " %" PRId64 "\n", value, i, value - i); + } + + parcEWMA_Release(&instance); +} + +LONGBOW_TEST_CASE(Specialization, parcEWMA_GetValue) +{ + PARCEWMA *instance = parcEWMA_Create(0.75); + + parcEWMA_Update(instance, 0); + + int64_t value = parcEWMA_GetValue(instance); + + assertTrue(value == 0, "Expected 0, actual %" PRId64 "", value); + + parcEWMA_Release(&instance); +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Specialization) +{ + if (!parcMemoryTesting_ExpectedOutstanding(0, "%s mismanaged memory.", longBowTestCase_GetFullName(testCase))) { + return LONGBOW_STATUS_MEMORYLEAK; + } + + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[argc]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_EWMA); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} + + |