#![cfg(feature = "std")]
use graphitesql::{Connection, Value};
fn text(c: &Connection, sql: &str) -> String {
match c.query(sql).unwrap().rows[0][0].clone() {
Value::Text(s) => s,
other => panic!("expected text, got {other:?}"),
}
}
#[test]
fn high_precision_pads_with_zeros_not_true_digits() {
let c = Connection::open_memory().unwrap();
assert_eq!(text(&c, "SELECT printf('%.17g', 0.1)"), "0.1");
assert_eq!(text(&c, "SELECT printf('%.25g', 0.3)"), "0.3");
assert_eq!(text(&c, "SELECT printf('%.17g', 0.1+0.2)"), "0.3");
assert_eq!(
text(&c, "SELECT printf('%.20f', 0.1)"),
"0.10000000000000000000"
);
assert_eq!(
text(&c, "SELECT printf('%.20e', 0.1)"),
"1.00000000000000000000e-01"
);
}
#[test]
fn sixteen_significant_digits_are_kept() {
let c = Connection::open_memory().unwrap();
assert_eq!(
text(&c, "SELECT printf('%.20g', 1.0/3.0)"),
"0.3333333333333333"
);
assert_eq!(
text(&c, "SELECT printf('%.20e', 1.0/3.0)"),
"3.33333333333333300000e-01"
);
assert_eq!(
text(&c, "SELECT printf('%.20g', 1.2345678901234567)"),
"1.234567890123457"
);
}
#[test]
fn bang_flag_uses_full_precision() {
let c = Connection::open_memory().unwrap();
assert_eq!(
text(&c, "SELECT printf('%!.17g', 0.1)"),
"0.10000000000000001"
);
assert_eq!(text(&c, "SELECT printf('%.17g', 0.1)"), "0.1");
}
#[test]
fn normal_precision_is_unchanged() {
let c = Connection::open_memory().unwrap();
assert_eq!(text(&c, "SELECT printf('%g', 0.1)"), "0.1");
assert_eq!(text(&c, "SELECT printf('%e', 1.0)"), "1.000000e+00");
assert_eq!(text(&c, "SELECT printf('%f', 1.5)"), "1.500000");
assert_eq!(text(&c, "SELECT printf('%.6g', 123.456)"), "123.456");
assert_eq!(text(&c, "SELECT printf('%.2g', 0.666)"), "0.67");
}