From 40b0c972271054fafd816002bbee6808919a31a6 Mon Sep 17 00:00:00 2001 From: axiongsupra Date: Thu, 6 Jun 2024 22:22:54 -0400 Subject: [PATCH] Add back aggregator tests and fix the errors --- .../tests/aggregator_tests.move | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 aptos-move/framework/supra-framework/tests/aggregator_tests.move diff --git a/aptos-move/framework/supra-framework/tests/aggregator_tests.move b/aptos-move/framework/supra-framework/tests/aggregator_tests.move new file mode 100644 index 0000000000000..ad7f4a3b1ef16 --- /dev/null +++ b/aptos-move/framework/supra-framework/tests/aggregator_tests.move @@ -0,0 +1,53 @@ +#[test_only] +module supra_framework::aggregator_tests { + + use supra_framework::aggregator; + use supra_framework::aggregator_factory; + + #[test(account = @supra_framework)] + fun test_can_add_and_sub_and_read(account: signer) { + aggregator_factory::initialize_aggregator_factory_for_test(&account); + let aggregator = aggregator_factory::create_aggregator(&account, 1000); + + aggregator::add(&mut aggregator, 12); + assert!(aggregator::read(&aggregator) == 12, 0); + + aggregator::add(&mut aggregator, 3); + assert!(aggregator::read(&aggregator) == 15, 0); + + aggregator::add(&mut aggregator, 3); + aggregator::add(&mut aggregator, 2); + aggregator::sub(&mut aggregator, 20); + assert!(aggregator::read(&aggregator) == 0, 0); + + aggregator::add(&mut aggregator, 1000); + aggregator::sub(&mut aggregator, 1000); + + aggregator::destroy(aggregator); + } + + #[test(account = @supra_framework)] + #[expected_failure(abort_code = 0x020001, location = supra_framework::aggregator)] + fun test_overflow(account: signer) { + aggregator_factory::initialize_aggregator_factory_for_test(&account); + let aggregator = aggregator_factory::create_aggregator(&account, 10); + + // Overflow! + aggregator::add(&mut aggregator, 12); + + aggregator::destroy(aggregator); + } + + #[test(account = @supra_framework)] + #[expected_failure(abort_code = 0x020002, location = supra_framework::aggregator)] + fun test_underflow(account: signer) { + aggregator_factory::initialize_aggregator_factory_for_test(&account); + let aggregator = aggregator_factory::create_aggregator(&account, 10); + + // Underflow! + aggregator::sub(&mut aggregator, 100); + aggregator::add(&mut aggregator, 100); + + aggregator::destroy(aggregator); + } +}