Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Create unit test for rust code #1615

Merged
merged 26 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
927a494
feat: Add new function to allocate any object to heap with zero alloc…
IshanGrover2004 Jun 1, 2024
c60bef9
feat: Add unit tests for `decoder/commands.rs`
IshanGrover2004 Jun 1, 2024
ad4e75f
docs: Mention about PR in changelogs
IshanGrover2004 Jun 1, 2024
91de924
feat: Add unit tests for `decoder/windows.rs`
IshanGrover2004 Jun 3, 2024
7f7fbd4
fix: Intialise tmp extern C values for easy mocking
IshanGrover2004 Jun 3, 2024
11309c6
feat: Add unit tests for `decoder/timing.rs`
IshanGrover2004 Jun 4, 2024
2fd23f8
feat: Add unit tests for `decoder/output.rs`
IshanGrover2004 Jun 5, 2024
05c1ddb
feat: Add unit tests for `decoder/mod.rs`
IshanGrover2004 Jun 5, 2024
5374b37
feat: Add unit tests for `decoder/tv_screen.rs`
IshanGrover2004 Jun 6, 2024
01e9367
feat: Add unit tests for `lib.rs`
IshanGrover2004 Jun 6, 2024
78f95bd
fix: Failing test
IshanGrover2004 Jun 7, 2024
cd68d6b
feat: [WIP] Add unit tests for `decoder/service_decoder.rs`
IshanGrover2004 Jun 7, 2024
00d0fd1
feat: Add unit tests for `decoder/service_decoder.rs`
IshanGrover2004 Jun 9, 2024
9bb5678
feat: Add unit tests for `hardsubx/imgops.rs`
IshanGrover2004 Jun 12, 2024
b1e4865
feat: Add unit tests for `hardsubx/utility.rs`
IshanGrover2004 Jun 12, 2024
37547e5
fix: cargo clippy
IshanGrover2004 Jul 17, 2024
454d136
fix: doctest for `lib_ccxr` module
IshanGrover2004 Jul 17, 2024
a237cbe
feat: Add test `lib_ccxr/util/mod.rs`
IshanGrover2004 Jul 17, 2024
e30151b
feat: Add test `lib_ccxr/util/levenshtein.rs`
IshanGrover2004 Jul 17, 2024
73cdd1d
feat: Add test `lib_ccxr/util/bits.rs`
IshanGrover2004 Jul 17, 2024
21591ae
feat: Add test `lib_ccxr/time/units.rs`
IshanGrover2004 Jul 18, 2024
8ffab1d
chore: Change function name
IshanGrover2004 Aug 10, 2024
808745b
fix: Failing of missing values `tlt_config`
IshanGrover2004 Aug 10, 2024
9b197fb
ci: Run unit test cases in `lib_ccxr` module also
IshanGrover2004 Aug 10, 2024
e55d3ff
ci: Run clippy & fmt in `lib_ccxr` module also
IshanGrover2004 Aug 10, 2024
5c96a1c
chore(clippy): Fix clippy warnings
IshanGrover2004 Aug 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ jobs:
git diff-index --quiet HEAD -- || (git diff && exit 1)
format_rust:
runs-on: ubuntu-latest
strategy:
matrix:
workdir: ['./src/rust', './src/rust/lib_ccxr']
defaults:
run:
working-directory: ./src/rust
working-directory: ${{ matrix.workdir }}
steps:
- uses: actions/checkout@v4
- name: cache
uses: actions/cache@v4
with:
path: |
src/rust/.cargo/registry
src/rust/.cargo/git
src/rust/target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
${{ matrix.workdir }}/.cargo/registry
${{ matrix.workdir }}/.cargo/git
${{ matrix.workdir }}/target
key: ${{ runner.os }}-cargo-${{ hashFiles('${{ matrix.workdir }}/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
- uses: actions-rs/toolchain@v1
with:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test_rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
src/rust/.cargo/registry
src/rust/.cargo/git
src/rust/target
src/rust/lib_ccxr/target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
- uses: actions-rs/toolchain@v1
Expand All @@ -35,3 +36,6 @@ jobs:
- name: Test main module
run: cargo test
working-directory: ./src/rust
- name: Test lib_ccxr module
run: cargo test
working-directory: ./src/rust/lib_ccxr
1 change: 1 addition & 0 deletions docs/CHANGES.TXT
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1.0 (to be released)
-----------------
- New: Create unit test for rust code (#1615)
- Breaking: Major argument flags revamp for CCExtractor (#1564 & #1619)
- New: Create a Docker image to simplify the CCExtractor usage without any environmental hustle (#1611)
- New: Add time units module in lib_ccxr (#1623)
Expand Down
183 changes: 164 additions & 19 deletions src/rust/lib_ccxr/src/time/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum TimestampFormat {
///
/// # Examples
/// ```rust
/// # use crate::time::units::{Timestamp, TimestampFormat};
/// # use lib_ccxr::time::units::{Timestamp, TimestampFormat};
/// let timestamp = Timestamp::from_millis(6524365);
/// let output = timestamp.to_formatted_time(TimestampFormat::None).unwrap();
/// assert_eq!(output, "");
Expand All @@ -56,7 +56,7 @@ pub enum TimestampFormat {
///
/// # Examples
/// ```rust
/// # use crate::time::units::{Timestamp, TimestampFormat};
/// # use lib_ccxr::time::units::{Timestamp, TimestampFormat};
/// let timestamp = Timestamp::from_millis(6524365);
/// let output = timestamp.to_formatted_time(TimestampFormat::HHMMSS).unwrap();
/// assert_eq!(output, "01:48:44");
Expand All @@ -67,7 +67,7 @@ pub enum TimestampFormat {
///
/// # Examples
/// ```rust
/// # use crate::time::units::{Timestamp, TimestampFormat};
/// # use lib_ccxr::time::units::{Timestamp, TimestampFormat};
/// let timestamp = Timestamp::from_millis(6524365);
/// let output = timestamp.to_formatted_time(
/// TimestampFormat::Seconds {
Expand All @@ -83,7 +83,7 @@ pub enum TimestampFormat {
///
/// # Examples
/// ```rust
/// # use crate::time::units::{Timestamp, TimestampFormat};
/// # use lib_ccxr::time::units::{Timestamp, TimestampFormat};
/// // 11 March 2023 14:53:36.749 in UNIX timestamp.
/// let timestamp = Timestamp::from_millis(1678546416749);
/// let output = timestamp.to_formatted_time(
Expand All @@ -99,7 +99,7 @@ pub enum TimestampFormat {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::{Timestamp, TimestampFormat};
/// # use lib_ccxr::time::units::{Timestamp, TimestampFormat};
/// let timestamp = Timestamp::from_millis(6524365);
/// let output = timestamp.to_formatted_time(TimestampFormat::HHMMSSFFF).unwrap();
/// assert_eq!(output, "01:48:44,365");
Expand Down Expand Up @@ -155,7 +155,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;;
/// let timestamp = Timestamp::from_millis(6524365);
/// assert_eq!(timestamp.millis(), 6524365);
/// ```
Expand All @@ -167,7 +167,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;;
/// let timestamp = Timestamp::from_millis(6524365);
/// assert_eq!(timestamp.seconds(), 6524);
/// ```
Expand All @@ -181,7 +181,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;;
/// let timestamp = Timestamp::from_millis(6524365);
/// assert_eq!(timestamp.as_sec_millis().unwrap(), (6524, 365));
/// ```
Expand All @@ -199,12 +199,12 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;;
/// let timestamp = Timestamp::from_millis(6524365);
/// assert_eq!(timestamp.as_hms_millis().unwrap(), (1, 48, 44, 365));
/// ```
/// ```rust
/// # use lib_ccxr::util::time::{Timestamp, TimestampError};
/// # use lib_ccxr::time::units::{Timestamp, TimestampError};
/// let timestamp = Timestamp::from_millis(1678546416749);
/// assert!(matches!(
/// timestamp.as_hms_millis().unwrap_err(),
Expand All @@ -227,7 +227,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::from_millis(6524365);
/// let mut output = String::new();
/// timestamp.write_srt_time(&mut output);
Expand All @@ -243,7 +243,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::from_millis(6524365);
/// let mut output = String::new();
/// timestamp.write_vtt_time(&mut output);
Expand All @@ -261,7 +261,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::from_millis(6524365);
/// let mut output = String::new();
/// timestamp.write_hms_millis_time(&mut output, ':');
Expand All @@ -283,7 +283,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::from_millis(6524365);
/// let mut output = String::new();
/// timestamp.write_ctime(&mut output);
Expand Down Expand Up @@ -344,7 +344,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::from_millis(6524365);
/// assert_eq!(timestamp.to_srt_time().unwrap(), "01:48:44,365");
/// ```
Expand All @@ -358,7 +358,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::from_millis(6524365);
/// assert_eq!(timestamp.to_vtt_time().unwrap(), "01:48:44.365");
/// ```
Expand All @@ -374,7 +374,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::from_millis(6524365);
/// assert_eq!(timestamp.to_hms_millis_time(':').unwrap(), "01:48:44:365");
/// ```
Expand All @@ -388,7 +388,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::from_millis(6524365);
/// assert_eq!(timestamp.to_ctime().unwrap(), "Thu Jan 01 01:48:44 1970");
/// ```
Expand All @@ -411,7 +411,7 @@ impl Timestamp {
///
/// # Examples
/// ```rust
/// # use lib_ccxr::util::time::Timestamp;
/// # use lib_ccxr::time::units::Timestamp;
/// let timestamp = Timestamp::parse_optional_hhmmss_from_str("01:12:45").unwrap();
/// assert_eq!(timestamp, Timestamp::from_millis(4_365_000));
/// ```
Expand Down Expand Up @@ -643,3 +643,148 @@ impl GopTimeCode {
)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_timestamp_from_millis() {
let ts = Timestamp::from_millis(5000);
assert_eq!(ts.millis(), 5000);
assert_eq!(ts.seconds(), 5);
}

#[test]
fn test_timestamp_from_hms_millis() {
let ts = Timestamp::from_hms_millis(1, 30, 45, 500).unwrap();
assert_eq!(ts.millis(), 5445500);

// Out of range case
assert!(matches!(
Timestamp::from_hms_millis(1, 60, 0, 0),
Err(TimestampError::InputOutOfRangeError)
));
}

#[test]
fn test_timestamp_as_sec_millis() {
let ts = Timestamp::from_millis(5445500);
assert_eq!(ts.as_sec_millis().unwrap(), (5445, 500));

let ts = Timestamp::from_millis(0);
assert_eq!(ts.as_sec_millis().unwrap(), (0, 0));

let ts = Timestamp::from_millis(1000);
assert_eq!(ts.as_sec_millis().unwrap(), (1, 0));

let ts = Timestamp::from_millis(-1000);
assert!(ts.as_sec_millis().is_err());
}

#[test]
fn test_timestamp_as_hms_millis() {
let ts = Timestamp::from_millis(5445500);
assert_eq!(ts.as_hms_millis().unwrap(), (1, 30, 45, 500));

let ts = Timestamp::from_millis(0);
assert_eq!(ts.as_hms_millis().unwrap(), (0, 0, 0, 0));

let ts = Timestamp::from_millis(3600000);
assert_eq!(ts.as_hms_millis().unwrap(), (1, 0, 0, 0));

let ts = Timestamp::from_millis(-1);
assert!(ts.as_hms_millis().is_err());
}

#[test]
fn test_timestamp_to_srt_time() {
let ts = Timestamp::from_millis(5445500);
assert_eq!(ts.to_srt_time().unwrap(), "01:30:45,500");

let ts = Timestamp::from_millis(0);
assert_eq!(ts.to_srt_time().unwrap(), "00:00:00,000");

let ts = Timestamp::from_millis(3661001);
assert_eq!(ts.to_srt_time().unwrap(), "01:01:01,001");

let ts = Timestamp::from_millis(-1);
assert!(ts.to_srt_time().is_err());
}

#[test]
fn test_timestamp_to_vtt_time() {
let ts = Timestamp::from_millis(5445500);
assert_eq!(ts.to_vtt_time().unwrap(), "01:30:45.500");

let ts = Timestamp::from_millis(0);
assert_eq!(ts.to_vtt_time().unwrap(), "00:00:00.000");

let ts = Timestamp::from_millis(3661001);
assert_eq!(ts.to_vtt_time().unwrap(), "01:01:01.001");

let ts = Timestamp::from_millis(-1);
assert!(ts.to_vtt_time().is_err());
}

#[test]
fn test_timestamp_to_hms_millis_time() {
let ts = Timestamp::from_millis(5445500);
assert_eq!(ts.to_hms_millis_time(':').unwrap(), "01:30:45:500");

let ts = Timestamp::from_millis(0);
assert_eq!(ts.to_hms_millis_time('.').unwrap(), "00:00:00.000");

let ts = Timestamp::from_millis(-3661001);
assert_eq!(ts.to_hms_millis_time(':').unwrap(), "-01:01:01:001");

let ts = Timestamp::from_millis(1);
assert_eq!(ts.to_hms_millis_time(':').unwrap(), "00:00:00:001");
}

#[test]
fn test_timestamp_to_ctime() {
let ts = Timestamp::from_millis(5445500);
assert_eq!(ts.to_ctime().unwrap(), "Thu Jan 01 01:30:45 1970");

let ts = Timestamp::from_millis(0);
assert_eq!(ts.to_ctime().unwrap(), "Thu Jan 01 00:00:00 1970");

let ts = Timestamp::from_millis(31536000000); // 1 year later
assert_eq!(ts.to_ctime().unwrap(), "Fri Jan 01 00:00:00 1971");

let ts = Timestamp::from_millis(-1);
assert!(ts.to_ctime().is_err());
}

#[test]
fn test_timestamp_parse_optional_hhmmss_from_str() {
assert_eq!(
Timestamp::parse_optional_hhmmss_from_str("01:30:45").unwrap(),
Timestamp::from_millis(5445000)
);
assert_eq!(
Timestamp::parse_optional_hhmmss_from_str("30:45").unwrap(),
Timestamp::from_millis(1845000)
);

// Error cases
assert!(matches!(
Timestamp::parse_optional_hhmmss_from_str("01:60:00"),
Err(TimestampError::InputOutOfRangeError)
));
assert!(matches!(
Timestamp::parse_optional_hhmmss_from_str("01:30:45:00"),
Err(TimestampError::ParsingError)
));
}

#[test]
fn test_timestamp_arithmetic() {
let ts1 = Timestamp::from_millis(5000);
let ts2 = Timestamp::from_millis(3000);
assert_eq!((ts1 + ts2).millis(), 8000);
assert_eq!((ts1 - ts2).millis(), 2000);
assert_eq!((-ts1).millis(), -5000);
}
}
Loading
Loading