Skip to content

Commit e39e9fb

Browse files
[Rust] src for task 3 & unit tests for task 1
1 parent 0abae11 commit e39e9fb

10 files changed

Lines changed: 172 additions & 36 deletions

File tree

rust-method/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust-method/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[package]
2-
name = "rust-method"
2+
name = "ac-sql"
33
version = "0.1.0"
44
edition = "2024"
55

66
[[bin]]
77
name = "ac-sql"
88
path = "src/task2.rs"
99

10+
11+
1012
[dependencies]

rust-method/src/queries/delete.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::task1::calculeaza_medii_generale;
2-
use crate::Secretariat;
3-
use crate::Student;
4-
use crate::Materie;
5-
use crate::Inrolare;
2+
use crate::structuri::Secretariat;
3+
use crate::structuri::Student;
4+
use crate::structuri::Materie;
5+
use crate::structuri::Inrolare;
6+
7+
68

79
use crate::queries::where_clause::*;
810

rust-method/src/queries/select.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::Secretariat;
2-
use crate::Student;
3-
use crate::Materie;
4-
use crate::Inrolare;
1+
use crate::structuri::Secretariat;
2+
use crate::structuri::Student;
3+
use crate::structuri::Materie;
4+
use crate::structuri::Inrolare;
55

66
use crate::queries::where_clause::*;
77

rust-method/src/queries/update.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use crate::Secretariat;
2-
use crate::Student;
3-
use crate::Materie;
4-
use crate::Inrolare;
5-
1+
use crate::structuri::Secretariat;
2+
use crate::structuri::Student;
3+
use crate::structuri::Materie;
4+
use crate::structuri::Inrolare;
65

76
use crate::queries::where_clause::*;
87
use crate::queries::where_clause::parseaza_conditiile_where;

rust-method/src/queries/where_clause.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::Student;
2-
use crate::Materie;
3-
use crate::Inrolare;
1+
use crate::structuri::Student;
2+
use crate::structuri::Materie;
3+
use crate::structuri::Inrolare;
44

55
use std::str::FromStr;
66

rust-method/src/task1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ fn parseaza_intrare_inrolare(linie: &str) -> Inrolare {
101101
}
102102

103103

104-
pub fn adauga_student(s: &mut Secretariat, id: usize, nume: &String, an_studiu: u8, statut: char, medie_generala: f32) {
104+
pub fn adauga_student(s: &mut Secretariat, id: usize, nume: &str, an_studiu: u8, statut: char, medie_generala: f32) {
105105
let student = Student {
106106
id: id,
107-
nume: nume.clone(),
107+
nume: nume.to_string(),
108108
an_studiu: an_studiu,
109109
statut: statut,
110110
medie_generala: medie_generala

rust-method/src/task2.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
mod structuri;
2-
mod task1;
3-
mod task3;
1+
use crate::queries::select::select;
2+
use crate::queries::delete::delete;
3+
use crate::queries::update::update;
4+
use crate::task1::citeste_secretariat;
45

5-
mod queries {
6+
pub mod structuri;
7+
pub mod task1;
8+
pub mod task3;
9+
10+
pub mod queries {
611
pub mod where_clause;
712
pub mod select;
813
pub mod delete;
914
pub mod update;
1015
}
1116

12-
use crate::structuri::*;
13-
use crate::queries::select::select;
14-
use crate::queries::delete::delete;
15-
use crate::queries::update::update;
16-
17+
pub mod tests {
18+
pub mod test_task1;
19+
}
1720

1821
use std::{env, process::exit};
19-
use crate::task1::citeste_secretariat;
2022
use std::io;
2123

22-
23-
24-
25-
2624
fn main() {
2725
let args: Vec<String> = env::args().collect();
2826

rust-method/src/task3.rs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,84 @@
11
use crate::structuri::*;
22

33

4-
pub fn cripteaza_studenti(_secretariat: &mut Secretariat, _key: &str, _iv: &str, _cale_output: &str) {
5-
todo!("");
4+
const NR_BLOCKS: usize = 4;
5+
6+
fn xor(block: &[u8], key: &[u8]) -> Vec<u8> {
7+
block
8+
.iter()
9+
.zip(key.iter().cycle()) // repeta cheia daca e mai scurta
10+
.map(|(&b, &k)| b ^ k)
11+
.collect()
12+
13+
}
14+
15+
fn p_box(block: &[u8]) -> Vec<u8> {
16+
let block_len: usize = block.len();
17+
let mut array: Vec<u8> = vec![0; block_len];
18+
19+
20+
for i in 0..block.len() {
21+
let j: usize =
22+
(i * (block_len - 1) + 2) % block_len;
23+
array[j] = block[i];
24+
}
25+
26+
array
27+
}
28+
29+
30+
fn split_into_blocks(secretariat: &Secretariat) -> Vec<Vec<u8>> {
31+
let mut bytes_studenti: Vec<u8> = Vec::new();
32+
33+
for student in secretariat.studenti.iter() {
34+
// id
35+
bytes_studenti.extend_from_slice(&student.id.to_be_bytes());
36+
37+
// an_studiu
38+
bytes_studenti.extend_from_slice(&student.an_studiu.to_be_bytes());
39+
40+
// statut (char -> u32 -> bytes)
41+
bytes_studenti.extend_from_slice(&(student.statut as u32).to_be_bytes());
42+
43+
// medie_generala
44+
bytes_studenti.extend_from_slice(&student.medie_generala.to_be_bytes());
45+
46+
// nume (trebuie serializat separat ca string)
47+
bytes_studenti.extend_from_slice(student.nume.as_bytes());
48+
}
49+
50+
// Adaugare padding (daca este cazul):
51+
while bytes_studenti.len() % 4 != 0 {
52+
bytes_studenti.push(0 as u8);
53+
}
54+
55+
56+
// Impartire in blocuri de aceasi dimensiune
57+
let mut blocks: Vec<Vec<u8>> = Vec::new();
58+
let block_length: usize = bytes_studenti.len() / NR_BLOCKS;
59+
for i in 0..NR_BLOCKS {
60+
let start = i * block_length;
61+
let end = start + block_length;
62+
blocks.push(bytes_studenti[start..end].to_vec());
63+
}
64+
65+
blocks
66+
}
67+
68+
69+
pub fn cripteaza_studenti(secretariat: &Secretariat, key: &str, iv: &str, _cale_output: &str) {
70+
let mut blocks: Vec<Vec<u8>> = split_into_blocks(secretariat);
71+
72+
let bytes_key: Vec<u8> = key.as_bytes().to_vec();
73+
let bytes_iv: Vec<u8> = iv.as_bytes().to_vec();
74+
75+
blocks[0] = xor(&blocks[0], &bytes_iv);
76+
blocks[0] = xor(&blocks[0], &bytes_key);
77+
blocks[0] = p_box(&blocks[0]);
78+
79+
for i in 1..NR_BLOCKS {
80+
blocks[i] = xor(&blocks[i], &blocks[i - 1]);
81+
blocks[i] = xor(&blocks[i], &bytes_key);
82+
blocks[i] = p_box(&blocks[i]);
83+
}
684
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#![cfg(test)]
2+
3+
use crate::structuri::Secretariat;
4+
use crate::task1::*;
5+
6+
const TEST_DB: &str = "../tests/db/small.db";
7+
const EPSILONE: f32 = 1e-3;
8+
9+
#[test]
10+
fn test_citire_secretariat() {
11+
let s: Secretariat = citeste_secretariat(TEST_DB);
12+
13+
assert!(s.studenti.len() == 10, "Numar stdenti gresit!");
14+
assert!(s.materii.len() == 9, "Numar materii gresit!");
15+
assert!(s.inrolari.len() == 25, "Numar inrolari gresit!");
16+
17+
assert!(
18+
s.studenti[4].nume == "Popescu Adrian".to_string()
19+
&& s.studenti[4].id == 4
20+
&& s.studenti[4].an_studiu == 3
21+
&& s.studenti[4].statut == 'b'
22+
&& (s.studenti[4].medie_generala - 7.74).abs() < EPSILONE,
23+
"Studentii au fost salvati gresit!"
24+
);
25+
26+
assert!(
27+
s.materii[3].id == 3
28+
&& s.materii[3].nume == "Fizica".to_string()
29+
&& s.materii[3].nume_titular == "Petrescu Cristina".to_string(),
30+
"Materiile au fost salvate gresit!"
31+
);
32+
33+
assert!(
34+
s.inrolari[5].id_student == 2 && s.inrolari[5].id_materie == 0
35+
&& (s.inrolari[5].note[0] - 2.22).abs() < EPSILONE
36+
&& (s.inrolari[5].note[1] - 1.50).abs() < EPSILONE
37+
&& (s.inrolari[5].note[2] - 3.08).abs() < EPSILONE,
38+
"Inrolarile au fost salvate gresit!"
39+
);
40+
}
41+
42+
43+
#[test]
44+
fn test_adauga_student() {
45+
let mut s: Secretariat = citeste_secretariat(TEST_DB);
46+
47+
adauga_student(&mut s, 11, "Popescu Adrian", 3, 'b', 9.99 as f32);
48+
49+
assert!(
50+
s.studenti[10].nume == "Popescu Adrian".to_string()
51+
&& s.studenti[10].id == 11
52+
&& s.studenti[10].an_studiu == 3
53+
&& s.studenti[10].statut == 'b',
54+
"Studentul nu a fost adaguat corect!"
55+
)
56+
57+
}

0 commit comments

Comments
 (0)