Add an extra test for Rnd's distribution
authorMarcin Mielniczuk <marmistrz.dev@zoho.eu>
Fri, 06 Jul 2018 17:16:01 +0200
changeset 13451 dd2e51f7303d
parent 13450 d3c86ade3d4d
child 13452 93ba1826b8c2
child 13482 f748a72432f2
Add an extra test for Rnd's distribution
gameServer2/src/server/handlers/common.rs
--- a/gameServer2/src/server/handlers/common.rs	Fri Jul 06 16:59:52 2018 +0200
+++ b/gameServer2/src/server/handlers/common.rs	Fri Jul 06 17:16:01 2018 +0200
@@ -1,13 +1,8 @@
 use protocol::messages::{
-    HWProtocolMessage::{self, Rnd},
-    HWServerMessage::{self, ChatMsg},
+    HWProtocolMessage::{self, Rnd}, HWServerMessage::{self, ChatMsg},
 };
 use rand::{self, Rng};
-use server::{
-    actions::Action,
-    room::HWRoom,
-    server::HWServer
-};
+use server::{actions::Action, room::HWRoom, server::HWServer};
 
 pub fn rnd_action(options: Vec<String>, room: Option<&mut HWRoom>) -> Vec<Action> {
     if let Some(room) = room {
@@ -54,13 +49,32 @@
         }
     }
 
+    /// This test terminates almost surely.
     #[test]
     fn test_handle_rnd_empty() {
         run_handle_test(vec![])
     }
 
+    /// This test terminates almost surely.
     #[test]
     fn test_handle_rnd_nonempty() {
         run_handle_test(vec!["A".to_owned(), "B".to_owned(), "C".to_owned()])
     }
+
+    /// This test terminates almost surely (strong law of large numbers)
+    #[test]
+    fn test_distribution() {
+        let eps = 0.000001;
+        let lim = 0.5;
+        let opts = vec![0.to_string(), 1.to_string()];
+        let mut ones = 0;
+        let mut tries = 0;
+
+        while tries < 1000 || ((ones as f64 / tries as f64) - lim).abs() >= eps {
+            tries += 1;
+            if reply2string(rnd_reply(opts.clone())) == 1.to_string() {
+                ones += 1;
+            }
+        }
+    }
 }