use spg_engine::{Engine, QueryResult};
fn ascii_of(e: &mut Engine, hex: &str, enc: &str) -> i64 {
let sql = format!("SELECT ascii(convert_from('\\x{hex}'::bytea, '{enc}'))");
match e
.execute(&sql)
.unwrap_or_else(|err| panic!("{sql}: {err:?}"))
{
QueryResult::Rows { rows, .. } => match &rows[0].values[0] {
spg_storage::Value::Int(n) => i64::from(*n),
spg_storage::Value::BigInt(n) => *n,
other => panic!("expected int, got {other:?}"),
},
other => panic!("expected rows, got {other:?}"),
}
}
#[test]
fn win125x_decode_matches_pg() {
let mut e = Engine::new();
assert_eq!(ascii_of(&mut e, "8a", "WIN1250"), 352);
assert_eq!(ascii_of(&mut e, "c0", "WIN1251"), 1040);
assert_eq!(ascii_of(&mut e, "c1", "WIN1253"), 913);
assert_eq!(ascii_of(&mut e, "dd", "WIN1254"), 304);
assert_eq!(ascii_of(&mut e, "80", "WIN1250"), 8364);
assert_eq!(ascii_of(&mut e, "88", "WIN1251"), 8364);
assert_eq!(ascii_of(&mut e, "8a", "WINDOWS1250"), 352);
}
#[test]
fn win125x_round_trips() {
let mut e = Engine::new();
let r = e
.execute("SELECT convert_to(convert_from('\\x8a'::bytea, 'WIN1250'), 'WIN1250')")
.unwrap();
let QueryResult::Rows { rows, .. } = r else {
panic!("rows")
};
assert_eq!(
rows[0].values[0],
spg_storage::Value::Bytes(vec![0x8a].into())
);
}
#[test]
fn win125x_undefined_byte_errors_like_pg() {
let mut e = Engine::new();
assert!(
e.execute("SELECT convert_from('\\x81'::bytea, 'WIN1250')")
.is_err()
);
assert!(e.execute("SELECT convert_to('А', 'WIN1253')").is_err());
}