gleam-kicad-sexpr/test/kicad_sexpr_test.gleam

137 lines
3.9 KiB
Gleam

import gleam/float
import gleam/int
import gleam/io
import gleam/list
import gleam/result
import gleam/string
import gleam/time/duration
import gleam/time/timestamp.{type Timestamp}
import gleeunit
import kicad_sexpr/decode.{type Decoder, type NextFn}
import kicad_sexpr/parse
import kicad_sexpr/token
import simplifile
pub fn main() -> Nil {
io.println("\nTesting Footprints")
test_read_parse_decode("/usr/share/kicad/footprints", token.footprint_file)
io.println("\nTesting Symbol Libraries")
test_read_parse_decode("/usr/share/kicad/symbols", token.symbol_library)
gleeunit.main()
}
fn time_taken_string(start_time: Timestamp, end_time: Timestamp) -> String {
let #(s, us) =
timestamp.difference(start_time, end_time)
|> duration.to_seconds_and_nanoseconds
int.to_string(int.absolute_value(s))
<> "."
<> int.to_string(us / 1_000_000)
<> "s"
}
fn print_stats(
title: String,
successes: Int,
total: Int,
start_time: Timestamp,
end_time: Timestamp,
) -> Nil {
io.println(
title
<> int.to_string(successes)
<> "/"
<> int.to_string(total)
<> " ("
<> int.to_float(100 * successes) /. int.to_float(total)
|> float.to_precision(1)
|> float.to_string
|> string.pad_start(5, "0")
|> string.append("%")
<> ") in "
<> time_taken_string(start_time, end_time),
)
}
fn test_read_parse_decode(
path: String,
decoder: fn(NextFn(a, a)) -> Decoder(a),
) -> Nil {
let assert Ok(file_names) = simplifile.get_files(path)
// let #(file_names, _) = file_names |> list.drop(0) |> list.split(1000)
// let file_names = list.sample(file_names, 1000)
// let file_names = ["/usr/share/kicad/symbols/RF_Module.kicad_sym"]
let num_file_names = list.length(file_names)
io.println("Total: " <> int.to_string(num_file_names))
let time_before_read = timestamp.system_time()
let #(successfully_read, _failed_to_read) =
file_names
|> list.map(fn(file_name) {
simplifile.read_bits(file_name)
|> result.map(fn(res) { #(file_name, res) })
|> result.map_error(fn(res) { #(file_name, res) })
})
|> result.partition
let time_after_read = timestamp.system_time()
let num_successfully_read = list.length(successfully_read)
print_stats(
"Read: ",
num_successfully_read,
num_file_names,
time_before_read,
time_after_read,
)
let time_before_parse = timestamp.system_time()
let #(successfully_parsed, _failed_to_parse) =
successfully_read
|> list.map(fn(data) {
let #(file_name, file_contents) = data
parse.run(file_contents)
|> result.map(fn(res) { #(file_name, file_contents, res) })
|> result.map_error(fn(res) { #(file_name, file_contents, res) })
})
|> result.partition
let time_after_parse = timestamp.system_time()
let num_successfully_parsed = list.length(successfully_parsed)
print_stats(
"Parsed: ",
num_successfully_parsed,
num_successfully_read,
time_before_parse,
time_after_parse,
)
let time_before_decode = timestamp.system_time()
let #(successfully_decoded, failed_to_decode) =
successfully_parsed
|> list.map(fn(data) {
let #(file_name, file_contents, sexpr) = data
decode.run(decoder, sexpr)
|> result.map(fn(res) { #(file_name, file_contents, sexpr, res) })
|> result.map_error(fn(res) { #(file_name, file_contents, sexpr, res) })
})
|> result.partition
let time_after_decode = timestamp.system_time()
let num_successfully_decoded = list.length(successfully_decoded)
print_stats(
"Decoded: ",
num_successfully_decoded,
num_successfully_parsed,
time_before_decode,
time_after_decode,
)
list.each(failed_to_decode, fn(data) {
let #(file_name, _file_contents, _sexpr, error) = data
io.println(file_name)
echo error
// panic
})
io.println(
"Total Time Taken: "
<> time_taken_string(time_before_read, time_after_decode),
)
}