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
150
151
152
153
154
155
156
157
158
159
160
161
use std::collections::HashMap;
use std::collections::linked_list::LinkedList;
use std::time::SystemTime;

use chrono::Local;
use serde_json::json;
use serde_json::Value;

use crate::engine::{parser, runtime};
use crate::engine::node::Node;
use crate::engine::node::NodeType::{NArg, NString};
//use crate::engines::RustExpressionEngine::parser::{parser,  ParserTokens};
use crate::engine::runtime::{is_number, OptMap};
use crate::utils::time_util;

//use test::Bencher;

#[derive(Clone, PartialEq)]
struct Eq<'a> {
    pub express: &'a str,
    pub eq: Value,
}

#[test]
fn test_node_run() {
    let john = json!({
        "a":1,
        "b":2,
        "c":"c",
        "d":null,
    });
    let expressions: Vec<Eq> = vec![
        Eq { express: "d.a == null", eq: json!(true) },
        Eq { express: "'2019-02-26' == '2019-02-26'", eq: json!(true) },
        Eq { express: "`f`+`s`", eq: json!("fs") },
        Eq { express: "a +1 > b * 8", eq: json!(false) },
        Eq { express: "a >= 0", eq: json!(true) },
        Eq { express: "'a'+c", eq: json!("ac") },
        Eq { express: "b", eq: json!(2) },
        Eq { express: "a < 1", eq: json!(false) },
        Eq { express: "a +1 > b*8", eq: json!(false) },
        Eq { express: "a * b == 2", eq: json!(true) },
        Eq { express: "a - b == 0", eq: json!(false) },
        Eq { express: "a >= 0 && a != 0", eq: json!(true) },
        Eq { express: "a == 1 && a != 0", eq: json!(true) },
        Eq { express: "1 > 3 ", eq: json!(false) },
        Eq { express: "1 + 2 != nil", eq: json!(true) },
        Eq { express: "1 != null", eq: json!(true) },
        Eq { express: "1 + 2 != nil && 1 > 0 ", eq: json!(true) },
        Eq { express: "1 + 2 != nil && 2 < b*8 ", eq: json!(true) },
    ];


    let mut index = 0;
    for item in expressions {
        println!("{}", item.express.clone());
        let box_node = parser::parse(item.express, &OptMap::new()).unwrap();
        let result = box_node.eval(&john).unwrap();
        println!("express: {} >>>>> {}", item.express, &result);
        let result_value = &item.eq.clone();
        if !result.eq(result_value) {
            // println!("exe express fail:".to_owned()+item);
            panic!("[rbatis] >>>>>>>>>>>>>>>>>>>>>exe fail express:'".to_owned() + item.clone().express + "'");
        }
        index += 1;
    }
}


#[test]
fn test_string_node() {
    let str_node = Node::new_string("sadf");
    str_node.eval(&Value::Null {});
    //println!("value:{}", result);
}

#[test]
fn test_arg_node() {
    let john = json!({
        "name": "John Doe",
        "age": Value::Null,
         "sex":{
            "a":"i'm a",
            "b":"i'm b",
         },
        "phones": [
            "+44 1234567",
            "+44 2345678"
        ]
    });

    let arg_node = Node::new_arg("sex.a");
    arg_node.eval(&john);
    //println!("value:{},error:{}", result, Error);
}

#[test]
fn benchmark_arg_node() {
    let john = json!({
        "name": "John Doe",
        "age": Value::Null,
         "sex":{
            "a":"i'm a",
            "b":"i'm b",
         },
        "phones": [
            "+44 1234567",
            "+44 2345678"
        ]
    });

    let arg_node = Node::new_arg("sex.a");

    let total = 100000;
    let now = SystemTime::now();
    for i in 0..total {
        arg_node.eval(&john);
    }
    time_util::count_time_qps("benchmark_arg_node", total, now);
}

#[test]
fn test_number_node() {
    let john = json!({
        "name": "John Doe",
        "age": 1,
         "sex":{
            "a":"i'm a",
            "b":"i'm b",
         },
        "phones": [
            "+44 1234567",
            "+44 2345678"
        ]
    });
    let numb = Node::new_number_f64(1.02 as f64);
    numb.eval(&john);
    // println!("{}", value);
}

#[test]
fn benchmark_parser_token() {
    let s = "'2019-02-26' == '2019-02-26'".to_string();
    let opt_map = OptMap::new();

    let total = 100000;
    let now = SystemTime::now();
    for i in 0..total {
        runtime::parser_tokens(&s, &opt_map);
    }
    time_util::count_time_qps("benchmark_parser_token", total, now);
}


//#[bench]
//fn bench_node_eval(b: &mut Bencher) {
//    let rc=Rc::new("asdf".to_string());
//    b.iter(|| {
//        rc.clone();
//    });
//}