Files
core/test/performance/standard_reference.py
T
2026-11-27 06:03:56 +00:00

61 lines
2.0 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Unicode 文本处理和整数往返;独立 Rust/Go 对端不共享 Dever runtime。"""
def source(language):
if language == 'dever':
return '''rule required(value: Int?) (out: Int)
| value | out |
| null | fail(Invalid) |
| other | value |
roundtrip(value: Int) (out: Int) {
decorated = " " + int.toText(value) + "雪 "
trimmed = text.trim(decorated)
joined = text.join(text.split(trimmed, ""), "")
out = required(int.parse(text.replace(joined, "雪", "")))
}
loop measure(n: Int) (out: Int)
| each value in range(0, n) | result |
| out | sum roundtrip(value) |
cmd bench = app.measure
'''
if language == 'rust':
return '''fn roundtrip(value: i64) -> i64 {
let mut decorated = String::from(" ");
decorated.push_str(&value.to_string());
decorated.push_str("雪 ");
let trimmed = decorated.trim().to_owned();
let pieces: Vec<String> = trimmed.split("").filter(|piece| !piece.is_empty()).map(str::to_owned).collect();
let joined = pieces.join("");
joined.replace("雪", "").parse().expect("valid integer")
}
fn main() {
let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap();
let mut total = 0i64;
for value in 0..n { total = total.checked_add(roundtrip(value)).expect("sum overflow"); }
println!("{total}");
}
'''
return '''package main
import ("fmt"; "os"; "strconv"; "strings")
func roundtrip(value int64) int64 {
decorated := " " + strconv.FormatInt(value, 10) + "雪 "
trimmed := strings.TrimSpace(decorated)
joined := strings.Join(strings.Split(trimmed, ""), "")
result, err := strconv.ParseInt(strings.ReplaceAll(joined, "雪", ""), 10, 64)
if err != nil { panic(err) }
return result
}
func main() {
n, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil { panic(err) }
total := int64(0)
for value := int64(0); value < n; value++ {
next := roundtrip(value)
sum := total + next
if (next > 0 && sum < total) || (next < 0 && sum > total) { panic("sum overflow") }
total = sum
}
fmt.Println(total)
}
'''