Skip to content

Commit

Permalink
[FEATURE] Create unit test for rust code (#1615)
Browse files Browse the repository at this point in the history
* feat: Add new function to allocate any object to heap with zero allocated

* feat: Add unit tests for `decoder/commands.rs`

* docs: Mention about PR in changelogs

* feat: Add unit tests for `decoder/windows.rs`

Refactor the code and use Default where needed
Implement `PartialEq` also

* fix: Intialise tmp extern C values for easy mocking

* feat: Add unit tests for `decoder/timing.rs`

* feat: Add unit tests for `decoder/output.rs`

* feat: Add unit tests for `decoder/mod.rs`

* feat: Add unit tests for `decoder/tv_screen.rs`

* feat: Add unit tests for `lib.rs`

* fix: Failing test

* feat: [WIP] Add unit tests for `decoder/service_decoder.rs`

* feat: Add unit tests for `decoder/service_decoder.rs`

* feat: Add unit tests for `hardsubx/imgops.rs`

* feat: Add unit tests for `hardsubx/utility.rs`

* fix: cargo clippy

* fix: doctest for `lib_ccxr` module

* feat: Add test `lib_ccxr/util/mod.rs`

* feat: Add test `lib_ccxr/util/levenshtein.rs`

* feat: Add test `lib_ccxr/util/bits.rs`

* feat: Add test `lib_ccxr/time/units.rs`

* chore: Change function name

* fix: Failing of missing values `tlt_config`

* ci: Run unit test cases in `lib_ccxr` module also

* ci: Run clippy & fmt in `lib_ccxr` module also

* chore(clippy): Fix clippy warnings
  • Loading branch information
IshanGrover2004 authored Aug 11, 2024
1 parent 5f9b395 commit f8001ae
Show file tree
Hide file tree
Showing 19 changed files with 1,690 additions and 51 deletions.
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

0 comments on commit f8001ae

Please sign in to comment.