Pārlūkot izejas kodu

Added unit tests for pstree

Lorenz Bung 8 gadus atpakaļ
vecāks
revīzija
f8142a44ea
2 mainītis faili ar 32 papildinājumiem un 8 dzēšanām
  1. 6
    6
      hw4/task1/src/pstree.rs
  2. 26
    2
      hw4/task1/src/unit_test_pstree.rs

+ 6
- 6
hw4/task1/src/pstree.rs Parādīt failu

@@ -4,7 +4,7 @@ use procinfo::pid;
4 4
 use self::libc::pid_t;
5 5
 
6 6
 /// Datenstruktur für einen Prozess.
7
-struct Process {
7
+pub struct Process {
8 8
     name : String,
9 9
     pid : pid_t,
10 10
     ppid : pid_t,
@@ -13,7 +13,7 @@ struct Process {
13 13
 impl Process {
14 14
 
15 15
     /// Erstellt eine Prozess-Datenstruktur aus procinfo::Stat.
16
-    fn new(with_pid:pid_t) -> Self {
16
+    pub fn new(with_pid:pid_t) -> Self {
17 17
         if let Ok(stat) = pid::stat(with_pid) {
18 18
             Process{name: stat.command, pid:stat.pid, ppid:stat.ppid}
19 19
         } else {
@@ -22,17 +22,17 @@ impl Process {
22 22
     }
23 23
 
24 24
     /// Prüft ob das Prozess-Struct ein Elternprozess besitzt.
25
-    fn has_parent(&self) -> bool {
25
+    pub fn has_parent(&self) -> bool {
26 26
         self.ppid != 0
27 27
     }
28 28
 
29 29
     /// Gibt den Elternprozess zurück.
30
-    fn parent(&self) -> Self {
30
+    pub fn parent(&self) -> Self {
31 31
         Process::new(self.ppid)
32 32
     }
33 33
 
34 34
     /// Prüft ob das Prozess-Struct einen (entfernten) Elternprozess mit dem übergebenen pid hat.
35
-    fn has_parent_with_pid(&self, pid: pid_t) -> bool {
35
+    pub fn has_parent_with_pid(&self, pid: pid_t) -> bool {
36 36
         if self.pid == pid {
37 37
             return true
38 38
         }
@@ -45,7 +45,7 @@ impl Process {
45 45
     }
46 46
 
47 47
     /// Gibt über Rekursion über die Eltern eine Prozesskette aus.
48
-    fn print_recursive(&self, to_pid:pid_t, output: &mut String) {
48
+    pub fn print_recursive(&self, to_pid:pid_t, output: &mut String) {
49 49
 
50 50
         if output.len() == 0 {
51 51
             *output = format!("{}({}){}", self.name, self.pid, output);

+ 26
- 2
hw4/task1/src/unit_test_pstree.rs Parādīt failu

@@ -1,7 +1,31 @@
1 1
 #[cfg(test)]
2 2
 mod tests {
3
+    use pstree::Process;
4
+
3 5
     #[test]
4
-    fn invalid_pid() {
5
-        assert_eq!()
6
+    #[should_panic]
7
+    fn new_invalid_pid() {
8
+        Process::new(-1000);
9
+    }
10
+
11
+    #[test]
12
+    fn new_valid_pid() {
13
+        Process::new(1);
14
+    }
15
+
16
+    #[test]
17
+    fn hasparent_no() {
18
+        assert_eq!(false, Process::has_parent(&Process::new(1)));
19
+    }
20
+
21
+    #[test]
22
+    fn hasparent_yes() {
23
+
24
+    }
25
+
26
+    #[test]
27
+    #[should_panic]
28
+    fn parent_not_existing() {
29
+        Process::parent(&Process::new(1));
6 30
     }
7 31
 }

Notiek ielāde…
Atcelt
Saglabāt