Skip to content

Commit

Permalink
test coverage github action (solana-labs#94)
Browse files Browse the repository at this point in the history
* test coverage github action

* move actions to another job

* moving stuff around

* grr

* artifact error

* try cobertura format with codecov

* add decimal test

* formatting

* tests for decimal

* rate tests

* format

* tests for calculate liquidation
  • Loading branch information
0xripleys authored Jul 22, 2022
1 parent 2514030 commit e79ed86
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 14 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,43 @@ jobs:
- name: Build and test
run: ./ci/cargo-build-test.sh

cargo-coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set env vars
run: |
source ci/rust-version.sh nightly
echo "RUST_NIGHTLY=$rust_nightly" >> $GITHUB_ENV
source ci/solana-version.sh
echo "SOLANA_VERSION=$solana_version" >> $GITHUB_ENV
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ env.RUST_NIGHTLY }}
override: true
profile: minimal

- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
# target # Removed due to build dependency caching conflicts
key: cargo-build-${{ hashFiles('**/Cargo.lock') }}-${{ env.RUST_NIGHTLY }}

- name: Install dependencies
run: |
./ci/install-build-deps.sh
./ci/install-program-deps.sh
echo "$HOME/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
- name: run test coverage
run: ./coverage.sh token-lending

- name: Codecov
uses: codecov/codecov-action@v3.1.0
with:
file: target/cov/cobertura.xml
1 change: 1 addition & 0 deletions ci/install-program-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ set -x
cargo --version
cargo install rustfilt || true
cargo install honggfuzz --version=0.5.52 --force || true
cargo install grcov --force

cargo +"$rust_stable" build-bpf --version
6 changes: 3 additions & 3 deletions ci/rust-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export rust_nightly_docker_image=solanalabs/rust-nightly:"$nightly_version"
stable)
rustup_install "$rust_stable"
;;
# nightly)
# rustup_install "$rust_nightly"
# ;;
nightly)
rustup_install "$rust_nightly"
;;
all)
rustup_install "$rust_stable"
rustup_install "$rust_nightly"
Expand Down
18 changes: 8 additions & 10 deletions coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Runs all program tests and builds a code coverage report
#

source ci/rust-version.sh nightly # get $rust_nightly env variable

set -e
cd "$(dirname "$0")"

Expand All @@ -11,11 +13,6 @@ if ! which grcov; then
exit 1
fi

if [[ ! "$(grcov --version)" =~ "0.6.1" ]]; then
echo Error: Required grcov version not installed
exit 1
fi

: "${CI_COMMIT:=local}"
reportName="lcov-${CI_COMMIT:0:9}"

Expand Down Expand Up @@ -57,7 +54,7 @@ for program in ${programs[@]}; do
(
set -ex
cd $program
cargo +nightly test --target-dir $here/target/cov
cargo +"$rust_nightly" test --features test-bpf --target-dir $here/target/cov
)
done

Expand All @@ -81,14 +78,15 @@ find target/cov -type f -name '*.gcda' -newer target/cov/before-test ! -newer ta

(
set -x
grcov target/cov/tmp --llvm -t html -o target/cov/$reportName
grcov target/cov/tmp --llvm -t lcov -o target/cov/lcov.info
grcov target/cov/tmp -t html -o target/cov/$reportName
grcov target/cov/tmp -t lcov -o target/cov/lcov.info
grcov target/cov/tmp -t cobertura -o target/cov/cobertura.xml

cd target/cov
tar zcf report.tar.gz $reportName
)

ls -l target/cov/$reportName/index.html
ln -sfT $reportName target/cov/LATEST
ls -l target/cov/
ln -sf $reportName target/cov/LATEST

exit $test_status
66 changes: 66 additions & 0 deletions token-lending/program/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,70 @@ mod test {
fn test_scaler() {
assert_eq!(U192::exp10(SCALE), Decimal::wad());
}

#[test]
fn test_u192() {
let one = U192::from(1);
assert_eq!(one.0, [1u64, 0, 0]);

let wad = Decimal::wad();
assert_eq!(wad.0, [WAD, 0, 0]);

let hundred = Decimal::from(100u64);
// 2^64 * 5 + 7766279631452241920 = 1e20
assert_eq!(hundred.0 .0, [7766279631452241920, 5, 0]);
}

#[test]
fn test_from_percent() {
let left = Decimal::from_percent(20);
let right = Decimal::from(20u64).try_div(Decimal::from(100u64)).unwrap();

assert_eq!(left, right);
}

#[test]
fn test_to_scaled_val() {
assert_eq!(
Decimal(U192::from(u128::MAX)).to_scaled_val().unwrap(),
u128::MAX
);

assert_eq!(
Decimal(U192::from(u128::MAX))
.try_add(Decimal(U192::from(1)))
.unwrap()
.to_scaled_val(),
Err(ProgramError::from(LendingError::MathOverflow))
);
}

#[test]
fn test_round_floor_ceil_u64() {
let mut val = Decimal::one();
assert_eq!(val.try_round_u64().unwrap(), 1);
assert_eq!(val.try_floor_u64().unwrap(), 1);
assert_eq!(val.try_ceil_u64().unwrap(), 1);

val = val
.try_add(Decimal::from_scaled_val(HALF_WAD as u128 - 1))
.unwrap();
assert_eq!(val.try_round_u64().unwrap(), 1);
assert_eq!(val.try_floor_u64().unwrap(), 1);
assert_eq!(val.try_ceil_u64().unwrap(), 2);

val = val.try_add(Decimal::from_scaled_val(1)).unwrap();
assert_eq!(val.try_round_u64().unwrap(), 2);
assert_eq!(val.try_floor_u64().unwrap(), 1);
assert_eq!(val.try_ceil_u64().unwrap(), 2);
}

#[test]
fn test_display() {
assert_eq!(Decimal::from(1u64).to_string(), "1.000000000000000000");
assert_eq!(
Decimal::from_scaled_val(1u128).to_string(),
"0.000000000000000001"
);
}
}
46 changes: 46 additions & 0 deletions token-lending/program/src/math/rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,55 @@ impl TryMul<Rate> for Rate {
#[cfg(test)]
mod test {
use super::*;
use std::convert::TryInto;

#[test]
fn test_scaled_val() {
assert_eq!(Rate::from_percent(50).to_scaled_val(), HALF_WAD as u128);
}

#[test]
fn checked_pow() {
assert_eq!(Rate::one(), Rate::one().try_pow(u64::MAX).unwrap());
assert_eq!(
Rate::from_percent(200).try_pow(7).unwrap(),
Decimal::from(128u64).try_into().unwrap()
);
}

#[test]
fn test_display() {
assert_eq!(
Rate::one().try_div(3u64).unwrap().to_string(),
"0.333333333333333333"
);
}

#[test]
fn test_basic_arithmetic() {
assert_eq!(
Rate::one().try_add(Rate::one()).unwrap(),
Rate::from_scaled_val(2 * WAD)
);

assert_eq!(Rate::one().try_sub(Rate::one()).unwrap(), Rate::zero());

assert_eq!(
Rate::from_percent(240)
.try_mul(Rate::from_percent(50))
.unwrap(),
Rate::from_percent(120)
);
assert_eq!(
Rate::from_percent(240).try_mul(10).unwrap(),
Decimal::from(24u64).try_into().unwrap()
);

assert_eq!(
Rate::from_percent(240)
.try_div(Rate::from_percent(60))
.unwrap(),
Rate::from_scaled_val(4 * WAD)
);
}
}
Loading

0 comments on commit e79ed86

Please sign in to comment.