-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
214 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
//#cmake:add-file ../../../../common/utilities/number/stabilized-value.h | ||
|
||
|
||
#include "../../../test.h" | ||
#include <../common/utilities/number/stabilized-value.h> | ||
|
||
using namespace utilities::number; | ||
|
||
// Test group description: | ||
// * This tests group verifies stabilized_value class. | ||
// | ||
// Current test description: | ||
// * Verify that empty function works as expected | ||
TEST_CASE( "empty test", "[stabilized value]" ) | ||
{ | ||
try | ||
{ | ||
stabilized_value< float > stab_value(5, 1.0f); | ||
CHECK(stab_value.empty()); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_FALSE(stab_value.empty()); | ||
stab_value.clear(); | ||
CHECK(stab_value.empty()); | ||
} | ||
catch(const std::exception &e) | ||
{ | ||
FAIL(" Exception caught: " << e.what()); | ||
} | ||
} | ||
|
||
TEST_CASE("stable value sanity - 40%", "[stabilized value]") | ||
{ | ||
try | ||
{ | ||
stabilized_value< float > stab_value(5, 0.4f); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
|
||
CHECK(1.0f == stab_value.get()); | ||
} | ||
catch (const std::exception &e) | ||
{ | ||
FAIL("Exception caught: " << e.what()); | ||
} | ||
} | ||
|
||
TEST_CASE("stable value sanity - 25%", "[stabilized value]") | ||
{ | ||
try | ||
{ | ||
stabilized_value< float > stab_value(5, 0.25f); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
CHECK_NOTHROW(stab_value.add(1.0f)); | ||
|
||
CHECK(1.0f == stab_value.get()); | ||
} | ||
catch (const std::exception &e) | ||
{ | ||
FAIL("Exception caught: " << e.what()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
unit-tests/utilities/number/stabilized/test-multi-threading.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
//#cmake:add-file ../../../../common/utilities/number/stabilized-value.h | ||
|
||
#include <thread> | ||
#include <chrono> | ||
#include "../../../test.h" | ||
#include <../common/utilities/number/stabilized-value.h> | ||
|
||
using namespace utilities::number; | ||
// Test group description: | ||
// * This tests group verifies stabilized_value class. | ||
// | ||
// Current test description: | ||
// * Verify that the stabilized_value class is thread safe | ||
|
||
TEST_CASE( "multi-threading", "[stabilized value]" ) | ||
{ | ||
try | ||
{ | ||
std::atomic<float> inserted_val_1 = { 20.0f }; | ||
std::atomic<float> inserted_val_2 = { 55.0f }; | ||
std::vector<float> values_vec; | ||
stabilized_value< float > stab_value( 10, 0.6f ); | ||
|
||
// Fill history with values (> 60% is 'inserted_val_1') | ||
std::thread first( [&]() { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
for (int i = 0; i < 1000; i++) | ||
{ | ||
if (i%3) | ||
stab_value.add(inserted_val_1); | ||
else | ||
stab_value.add(inserted_val_2); | ||
} | ||
} ); | ||
|
||
// Sample stabilized value | ||
std::thread second( [&]() { | ||
while (stab_value.empty()); | ||
for (int i = 0; i < 1000; i++) | ||
values_vec.push_back(stab_value.get()); | ||
} ); | ||
|
||
if (first.joinable()) first.join(); | ||
if (second.joinable()) second.join(); | ||
|
||
// Verify that all samples show the > 60% number inserted 'inserted_val_1' | ||
for (auto val : values_vec) | ||
CHECK(val == inserted_val_1); | ||
} | ||
catch( const std::exception & e ) | ||
{ | ||
FAIL( "Exception caught: " << e.what() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2020 Intel Corporation. All Rights Reserved. | ||
|
||
//#cmake:add-file ../../../../common/utilities/number/stabilized-value.h | ||
|
||
|
||
#include "../../../test.h" | ||
#include <../common/utilities/number/stabilized-value.h> | ||
|
||
using namespace utilities::number; | ||
|
||
// Test group description: | ||
// * This tests group verifies stabilized_value class. | ||
// | ||
// Current test description: | ||
// * Verify if history is not the logic works as expected and the percentage is calculated from the history current size | ||
TEST_CASE( "not full history", "[stabilized value]" ) | ||
{ | ||
try | ||
{ | ||
stabilized_value< float > stab_value(30, 0.6f); | ||
CHECK_NOTHROW(stab_value.add(76.0f)); | ||
CHECK_NOTHROW(stab_value.add(76.0f)); | ||
CHECK_NOTHROW(stab_value.add(76.0f)); | ||
CHECK_NOTHROW(stab_value.add(76.0f)); | ||
CHECK(76.0f == stab_value.get()); | ||
|
||
CHECK_NOTHROW(stab_value.add(45.0f)); | ||
CHECK_NOTHROW(stab_value.add(45.0f)); | ||
CHECK_NOTHROW(stab_value.add(45.0f)); | ||
CHECK_NOTHROW(stab_value.add(45.0f)); | ||
CHECK(76.0f == stab_value.get()); | ||
|
||
CHECK_NOTHROW(stab_value.add(45.0f)); | ||
CHECK_NOTHROW(stab_value.add(45.0f)); // The stable value should change now (4 * 76.0 + 6 * 45.0 (total 10 values)) | ||
CHECK(45.0f == stab_value.get()); | ||
} | ||
catch(const std::exception &e) | ||
{ | ||
FAIL("Exception caught: " << e.what()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters