Skip to content

Commit

Permalink
Added github workflows and ignored expensive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrespoae committed Apr 20, 2024
1 parent af6ed09 commit ecf7ca7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/rust-ci.yml
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build-preview:

test:
cargo test
cargo test -- --ignored

clean:
cargo clean
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
5 changes: 2 additions & 3 deletions src/one_max_genetic_algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,11 @@ pub fn mutate(genome: &[u8], mutation_rate: f64) -> Vec<u8> {
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::<f64>() < mutation_rate {
mutated_genome[i] ^= 1; // XOR operator, change a 0 to a 1 and vice versa
*gene ^= 1;
}
}

mutated_genome
}

Expand Down
4 changes: 4 additions & 0 deletions tests/one_max_genetic_algorithm_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<u8>> = vec![vec![0, 1, 0, 1]; population_size];
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ecf7ca7

Please sign in to comment.