Phase 33 NORM canon test: enforce normalized dev route for P1/P2/JP mini
This commit is contained in:
@ -3,14 +3,14 @@
|
||||
//! Ring1-Core: core_required Box の Service trait 群。
|
||||
//! Phase 87 CoreBoxId の core_required (6個) 全てをカバー。
|
||||
|
||||
use std::sync::Arc;
|
||||
use crate::runtime::CoreBoxId;
|
||||
use crate::box_trait::NyashBox;
|
||||
use crate::runtime::CoreBoxId;
|
||||
use std::sync::Arc;
|
||||
|
||||
// Phase 96.5: Adapter実装で使用するBox型をトップレベルでimport
|
||||
use crate::box_trait::{BoolBox, IntegerBox, StringBox};
|
||||
use crate::boxes::array::ArrayBox;
|
||||
use crate::boxes::map_box::MapBox;
|
||||
use crate::box_trait::{IntegerBox, StringBox, BoolBox};
|
||||
|
||||
/// StringBox Service trait
|
||||
///
|
||||
@ -155,7 +155,6 @@ impl CoreServices {
|
||||
// Phase 91 では trait が空なので何もしない
|
||||
// Phase 92 以降で各 Service の初期化を検証
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@ -196,20 +195,20 @@ impl IntegerBoxAdapter {
|
||||
|
||||
impl IntegerService for IntegerBoxAdapter {
|
||||
fn add(&self, a: i64, b: i64) -> i64 {
|
||||
a.saturating_add(b) // オーバーフロー対策
|
||||
a.saturating_add(b) // オーバーフロー対策
|
||||
}
|
||||
|
||||
fn sub(&self, a: i64, b: i64) -> i64 {
|
||||
a.saturating_sub(b) // アンダーフロー対策
|
||||
a.saturating_sub(b) // アンダーフロー対策
|
||||
}
|
||||
|
||||
fn mul(&self, a: i64, b: i64) -> i64 {
|
||||
a.saturating_mul(b) // オーバーフロー対策
|
||||
a.saturating_mul(b) // オーバーフロー対策
|
||||
}
|
||||
|
||||
fn div(&self, a: i64, b: i64) -> Option<i64> {
|
||||
if b == 0 {
|
||||
None // ゼロ除算
|
||||
None // ゼロ除算
|
||||
} else {
|
||||
Some(a / b)
|
||||
}
|
||||
@ -259,9 +258,9 @@ impl ArrayBoxAdapter {
|
||||
impl ArrayService for ArrayBoxAdapter {
|
||||
fn len(&self, arr: &dyn NyashBox) -> i64 {
|
||||
arr.as_any()
|
||||
.downcast_ref::<ArrayBox>()
|
||||
.map(|a| a.len() as i64)
|
||||
.unwrap_or(0)
|
||||
.downcast_ref::<ArrayBox>()
|
||||
.map(|a| a.len() as i64)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn get(&self, arr: &dyn NyashBox, index: i64) -> Option<Box<dyn NyashBox>> {
|
||||
@ -271,7 +270,8 @@ impl ArrayService for ArrayBoxAdapter {
|
||||
}
|
||||
|
||||
fn set(&self, arr: &dyn NyashBox, index: i64, value: Box<dyn NyashBox>) -> Result<(), String> {
|
||||
let arr_box = arr.as_any()
|
||||
let arr_box = arr
|
||||
.as_any()
|
||||
.downcast_ref::<ArrayBox>()
|
||||
.ok_or("Not an ArrayBox")?;
|
||||
let index_box = Box::new(IntegerBox::new(index));
|
||||
@ -280,7 +280,8 @@ impl ArrayService for ArrayBoxAdapter {
|
||||
}
|
||||
|
||||
fn push(&self, arr: &dyn NyashBox, value: Box<dyn NyashBox>) -> Result<(), String> {
|
||||
let arr_box = arr.as_any()
|
||||
let arr_box = arr
|
||||
.as_any()
|
||||
.downcast_ref::<ArrayBox>()
|
||||
.ok_or("Not an ArrayBox")?;
|
||||
arr_box.push(value);
|
||||
@ -302,16 +303,17 @@ impl MapBoxAdapter {
|
||||
impl MapService for MapBoxAdapter {
|
||||
fn size(&self, map: &dyn NyashBox) -> i64 {
|
||||
map.as_any()
|
||||
.downcast_ref::<MapBox>()
|
||||
.map(|m| {
|
||||
// MapBox::size() は Box<dyn NyashBox> を返すため、IntegerBox に変換
|
||||
let size_box = m.size();
|
||||
size_box.as_any()
|
||||
.downcast_ref::<IntegerBox>()
|
||||
.map(|i| i.value)
|
||||
.unwrap_or(0)
|
||||
})
|
||||
.unwrap_or(0)
|
||||
.downcast_ref::<MapBox>()
|
||||
.map(|m| {
|
||||
// MapBox::size() は Box<dyn NyashBox> を返すため、IntegerBox に変換
|
||||
let size_box = m.size();
|
||||
size_box
|
||||
.as_any()
|
||||
.downcast_ref::<IntegerBox>()
|
||||
.map(|i| i.value)
|
||||
.unwrap_or(0)
|
||||
})
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
fn has(&self, map: &dyn NyashBox, key: &str) -> bool {
|
||||
@ -321,10 +323,11 @@ impl MapService for MapBoxAdapter {
|
||||
};
|
||||
let key_box = Box::new(StringBox::new(key));
|
||||
let result = map_box.has(key_box);
|
||||
result.as_any()
|
||||
.downcast_ref::<BoolBox>()
|
||||
.map(|b| b.value)
|
||||
.unwrap_or(false)
|
||||
result
|
||||
.as_any()
|
||||
.downcast_ref::<BoolBox>()
|
||||
.map(|b| b.value)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn get(&self, map: &dyn NyashBox, key: &str) -> Option<Box<dyn NyashBox>> {
|
||||
@ -334,7 +337,8 @@ impl MapService for MapBoxAdapter {
|
||||
}
|
||||
|
||||
fn set(&self, map: &dyn NyashBox, key: &str, value: Box<dyn NyashBox>) -> Result<(), String> {
|
||||
let map_box = map.as_any()
|
||||
let map_box = map
|
||||
.as_any()
|
||||
.downcast_ref::<MapBox>()
|
||||
.ok_or("Not a MapBox")?;
|
||||
let key_box = Box::new(StringBox::new(key));
|
||||
@ -461,8 +465,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_array_service_basic_operations() {
|
||||
use crate::boxes::array::ArrayBox;
|
||||
use crate::box_trait::IntegerBox;
|
||||
use crate::boxes::array::ArrayBox;
|
||||
|
||||
let arr = ArrayBox::new();
|
||||
let adapter = ArrayBoxAdapter::new();
|
||||
@ -482,8 +486,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_array_service_set() {
|
||||
use crate::boxes::array::ArrayBox;
|
||||
use crate::box_trait::IntegerBox;
|
||||
use crate::boxes::array::ArrayBox;
|
||||
|
||||
let arr = ArrayBox::new();
|
||||
let adapter = ArrayBoxAdapter::new();
|
||||
@ -502,8 +506,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_map_service_basic_operations() {
|
||||
use crate::boxes::map_box::MapBox;
|
||||
use crate::box_trait::StringBox;
|
||||
use crate::boxes::map_box::MapBox;
|
||||
|
||||
let map = MapBox::new();
|
||||
let adapter = MapBoxAdapter::new();
|
||||
@ -527,15 +531,19 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_map_service_multiple_keys() {
|
||||
use crate::boxes::map_box::MapBox;
|
||||
use crate::box_trait::{IntegerBox, StringBox};
|
||||
use crate::boxes::map_box::MapBox;
|
||||
|
||||
let map = MapBox::new();
|
||||
let adapter = MapBoxAdapter::new();
|
||||
|
||||
// set multiple keys
|
||||
adapter.set(&map, "name", Box::new(StringBox::new("Alice"))).unwrap();
|
||||
adapter.set(&map, "age", Box::new(IntegerBox::new(25))).unwrap();
|
||||
adapter
|
||||
.set(&map, "name", Box::new(StringBox::new("Alice")))
|
||||
.unwrap();
|
||||
adapter
|
||||
.set(&map, "age", Box::new(IntegerBox::new(25)))
|
||||
.unwrap();
|
||||
|
||||
// verify size
|
||||
assert_eq!(adapter.size(&map), 2);
|
||||
@ -556,20 +564,20 @@ mod tests {
|
||||
|
||||
// add
|
||||
assert_eq!(adapter.add(10, 20), 30);
|
||||
assert_eq!(adapter.add(i64::MAX, 1), i64::MAX); // saturating
|
||||
assert_eq!(adapter.add(i64::MAX, 1), i64::MAX); // saturating
|
||||
|
||||
// sub
|
||||
assert_eq!(adapter.sub(20, 10), 10);
|
||||
assert_eq!(adapter.sub(i64::MIN, 1), i64::MIN); // saturating
|
||||
assert_eq!(adapter.sub(i64::MIN, 1), i64::MIN); // saturating
|
||||
|
||||
// mul
|
||||
assert_eq!(adapter.mul(5, 6), 30);
|
||||
assert_eq!(adapter.mul(i64::MAX, 2), i64::MAX); // saturating
|
||||
assert_eq!(adapter.mul(i64::MAX, 2), i64::MAX); // saturating
|
||||
|
||||
// div
|
||||
assert_eq!(adapter.div(20, 5), Some(4));
|
||||
assert_eq!(adapter.div(10, 3), Some(3)); // 整数除算
|
||||
assert_eq!(adapter.div(10, 0), None); // ゼロ除算
|
||||
assert_eq!(adapter.div(10, 3), Some(3)); // 整数除算
|
||||
assert_eq!(adapter.div(10, 0), None); // ゼロ除算
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user