浏览代码

Added unit tests for pstree

Lorenz Bung 8 年前
父节点
当前提交
f8142a44ea
共有 2 个文件被更改,包括 32 次插入8 次删除
  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 查看文件

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

+ 26
- 2
hw4/task1/src/unit_test_pstree.rs 查看文件

1
 #[cfg(test)]
1
 #[cfg(test)]
2
 mod tests {
2
 mod tests {
3
+    use pstree::Process;
4
+
3
     #[test]
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
 }

正在加载...
取消
保存