diff --git a/.github/workflows/rust-ci.yml b/.github/workflows/rust-ci.yml new file mode 100644 index 0000000..aeeddb2 --- /dev/null +++ b/.github/workflows/rust-ci.yml @@ -0,0 +1,28 @@ +name: Rust CI + +on: + push: + branches: + - main + pull_request: + branches: + -main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Set up Rust + uses: actions/checkout@v4 + - name: Install cargo-audit + run: cargo install cargo-audit + - name: Build + run: cargo build --verbose + - name: Test + run: cargo test --verbose + - name: Clippy + run: cargo clippy --verbose -- -D warnings + - name: Audit + run: cargo audit + diff --git a/Makefile b/Makefile index 99dec21..f6519c3 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ build-preview: test: cargo test + cargo test -- --ignored clean: cargo clean \ No newline at end of file diff --git a/README.md b/README.md index 3c7e443..ca4ed07 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,11 @@ Or, they can be executed using the Make command. ```bash make test ``` +**_Note:_** When running the `cargo test` command, expensive tests are ignored. However, when using `make test`, all tests, including the ignored ones, will be triggered. + +## CI/CD +This project features a lightweight CI/CD setup in the form of GitHub workflows. It includes a single job comprising several steps for auditing vulnerabilities, testing, and linting options. + ## Python implementation I've also developed this repository in [Python](https://github.com/mcrespoae/one-max-genetic-algorithm-python), achieving an average execution time that's 3.4 times slower compared to Rust. diff --git a/src/one_max_genetic_algorithm.rs b/src/one_max_genetic_algorithm.rs index 0b4c688..947fca9 100644 --- a/src/one_max_genetic_algorithm.rs +++ b/src/one_max_genetic_algorithm.rs @@ -135,12 +135,11 @@ pub fn mutate(genome: &[u8], mutation_rate: f64) -> Vec { let mut rng = thread_rng(); let mut mutated_genome = genome.to_vec(); - for i in 0..genome.len() { + for gene in mutated_genome.iter_mut().take(genome.len()) { if rng.gen::() < mutation_rate { - mutated_genome[i] ^= 1; // XOR operator, change a 0 to a 1 and vice versa + *gene ^= 1; } } - mutated_genome } diff --git a/tests/one_max_genetic_algorithm_test.rs b/tests/one_max_genetic_algorithm_test.rs index 4bff93a..f34a366 100644 --- a/tests/one_max_genetic_algorithm_test.rs +++ b/tests/one_max_genetic_algorithm_test.rs @@ -258,6 +258,7 @@ mod unit_tests { } #[test] + #[ignore] // it is too expensive for gitgub actions fn test_mutate_population_fitness_all_ones() { let num_times = 100_000; let mutation_rate = 0.5; @@ -276,6 +277,7 @@ mod unit_tests { } #[test] + #[ignore] // it is too expensive for gitgub actions fn test_mutate_population_fitness_all_zeroes() { let num_times = 100_000; let mutation_rate = 0.5; @@ -295,6 +297,7 @@ mod unit_tests { } #[test] + #[ignore] // it is too expensive for gitgub actions fn test_population_size_odd() { let population_size = 101; let population: Vec> = vec![vec![0, 1, 0, 1]; population_size]; @@ -324,6 +327,7 @@ mod unit_tests { } #[test] + #[ignore] // it is too expensive for gitgub actions fn test_genetic_algorithm_custom_parameters() { // Test with custom parameters let population_size = 50;