summary refs log tree commit diff stats
path: root/tests/stdlib/tmath.nim
blob: fc9486093dde320486791416656396a695804479 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import math
import unittest
import sets

suite "random int":
  test "there might be some randomness":
    var set = initSet[int](128)
    randomize()
    for i in 1..1000:
      incl(set, random(high(int)))
    check len(set) == 1000
  test "single number bounds work":
    randomize()
    var rand: int
    for i in 1..1000:
      rand = random(1000)
      check rand < 1000
      check rand > -1
  test "slice bounds work":
    randomize()
    var rand: int
    for i in 1..1000:
      rand = random(100..1000)
      check rand < 1000
      check rand >= 100
  test "randomize() again gives new numbers":      
    randomize()
    var rand1 = random(1000000)
    randomize()
    var rand2 = random(1000000)
    check rand1 != rand2
    

suite "random float":
  test "there might be some randomness":
    var set = initSet[float](128)
    randomize()
    for i in 1..100:
      incl(set, random(1.0))
    check len(set) == 100
  test "single number bounds work":
    randomize()
    var rand: float
    for i in 1..1000:
      rand = random(1000.0)
      check rand < 1000.0
      check rand > -1.0
  test "slice bounds work":
    randomize()
    var rand: float
    for i in 1..1000:
      rand = random(100.0..1000.0)
      check rand < 1000.0
      check rand >= 100.0
  test "randomize() again gives new numbers":      
    randomize()
    var rand1:float = random(1000000.0)
    randomize()
    var rand2:float = random(1000000.0)
    check rand1 != rand2
rison <ben@gbmor.dev> 2020-05-28 03:31:43 -0400 committer Ben Morrison <ben@gbmor.dev> 2020-05-28 03:31:43 -0400 more testing' href='/gbmor/clinte/commit/src/db.rs?id=e4856f6ba83af8fc61c1eda1a2842ff353cd5b9d'>e4856f6 ^
bb327d3 ^
07511f9 ^
bb327d3 ^



e4856f6 ^


































07511f9 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149




                                    
 
                
                 
 




                                                       
 
                                               
                 


                       

 

                                        
                     

 
                
                 
                                    

 
           
                                     
                         
                                              

         
                                                                                
 




                                    
 

                                        
                         
                                              
         
 





                                                                                                    

     



                                                     

                                         
























                                                   
     



                                      
 



                 
                    

           

                                           
                                       
 



                                                                               


































                                                  

     
use fd_lock::FdLock;
use serde::{Deserialize, Serialize};

use std::fs;
use std::fs::File;

use crate::conf;
use crate::error;

#[cfg(test)]
pub const PATH: &str = "clinte.json";

#[cfg(not(test))]
pub const PATH: &str = "/usr/local/clinte/clinte.json";

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Post {
    pub title: String,
    pub author: String,
    pub body: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Posts {
    posts: Vec<Post>,
}

#[derive(Debug)]
pub struct Conn {
    pub conn: FdLock<std::fs::File>,
}

impl Conn {
    pub fn init(path: &str) -> Self {
        if *conf::DEBUG {
            log::info!("Opening clinte.json");
        }

        let file = error::helper(File::open(path), "Couldn't open clinte.json");

        Self {
            conn: FdLock::new(file),
        }
    }
}

impl Posts {
    pub fn get_all(path: &str) -> Self {
        if *conf::DEBUG {
            log::info!("Retrieving posts...");
        }

        let mut db = Conn::init(path);
        let _guard = error::helper(db.conn.try_lock(), "Couldn't acquire lock on clinte.json");
        let strdata = error::helper(fs::read_to_string(PATH), "Couldn't read clinte.json");
        let out: Self = error::helper(serde_json::from_str(&strdata), "Couldn't parse clinte.json");

        out
    }

    pub fn replace(&mut self, n: usize, post: Post) {
        self.posts[n] = post;
    }

    pub fn get(&self, n: usize) -> Post {
        self.posts[n].clone()
    }

    pub fn append(&mut self, post: Post) {
        self.posts.push(post);
    }

    pub fn delete(&mut self, n: usize) {
        self.posts.remove(n);
    }

    pub fn write(&self) {
        let strdata = error::helper(
            serde_json::to_string_pretty(&self),
            "Couldn't serialize posts",
        );

        let mut db_fd = Conn::init(PATH);
        let _guard = error::helper(
            db_fd.conn.try_lock(),
            "Couldn't acquire lock on clinte.json",
        );
        error::helper(
            fs::write(PATH, &strdata),
            "Couldn't write data to clinte.json",
        );
    }

    pub fn posts(&self) -> Vec<Post> {
        self.posts.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::user;

    #[test]
    fn retrieve_posts_and_crud() {
        let mut all = Posts::get_all(PATH);
        assert_eq!(all.posts.len(), 1);

        let post = all.get(0);
        assert_eq!(post.title, "Welcome to CLI NoTEs!");
        assert_eq!(post.author, "clinte!");
        assert_eq!(post.body, "Welcome to clinte! For usage, run 'clinte -h'");

        let user = &*user::NAME;

        all.append(Post {
            author: user.into(),
            title: String::from("TITLE_HERE"),
            body: String::from("BODY_HERE"),
        });

        all.write();
        let mut all = Posts::get_all(PATH);

        let post = all.get(1);
        assert_eq!(post.title, "TITLE_HERE");
        assert_eq!(post.author, *user);
        assert_eq!(post.body, "BODY_HERE");

        let post = Post {
            author: user.into(),
            title: "TITLE_GOES_HERE".into(),
            body: "BODY_GOES_HERE".into(),
        };

        all.replace(1, post);

        all.write();
        let mut all = Posts::get_all(PATH);

        let post = all.get(1);
        assert_eq!(post.title, "TITLE_GOES_HERE");
        assert_eq!(post.author, *user);
        assert_eq!(post.body, "BODY_GOES_HERE");

        all.delete(1);
        all.write();
    }
}