roplat 0.2.0

roplat: just a robot operation system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use std::marker::PhantomData;
use std::ops::{Add, Mul, Sub};

use num_traits::{CheckedDiv, NumOps, Zero};

use crate::Node;
use crate::error::RoplatError;

// --- 加法节点 ---
/// 加法节点。
pub struct OpAdd<T>(PhantomData<T>);

impl<T> Default for OpAdd<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpAdd<T>
where
    T: Add<Output = T> + Send + Sync + Copy + 'static,
{
    type Input = (T, T);
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        Ok(input.0 + input.1)
    }
}

// --- 减法节点 ---
/// 减法节点。
pub struct OpSub<T>(PhantomData<T>);

impl<T> Default for OpSub<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpSub<T>
where
    T: Sub<Output = T> + Send + Sync + Copy + 'static,
{
    type Input = (T, T);
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        Ok(input.0 - input.1)
    }
}

// --- 乘法节点 ---
/// 乘法节点。
pub struct OpMul<T>(PhantomData<T>);

impl<T> Default for OpMul<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpMul<T>
where
    T: Mul<Output = T> + Send + Sync + Copy + 'static,
{
    type Input = (T, T);
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        Ok(input.0 * input.1)
    }
}

// --- 安全除法节点 ---
/// 安全除法节点(除零返回错误)。
pub struct OpDiv<T>(PhantomData<T>);

impl<T> Default for OpDiv<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpDiv<T>
where
    T: CheckedDiv + Zero + Send + Sync + Copy + 'static,
{
    type Input = (T, T);
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        input
            .0
            .checked_div(&input.1)
            .ok_or_else(|| RoplatError::Arithmetic("除零错误".into()))
    }
}

// --- 取模节点 ---
/// 取模节点(模零返回错误)。
pub struct OpRem<T>(PhantomData<T>);

impl<T> Default for OpRem<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpRem<T>
where
    T: NumOps + Zero + PartialEq + Copy + Send + Sync + 'static,
    for<'a> &'a T: std::ops::Rem<Output = T>,
{
    type Input = (T, T);
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        if input.1 == T::zero() {
            return Err(RoplatError::Arithmetic("模零错误".into()));
        }
        Ok(input.0 % input.1)
    }
}

// --- 取负节点 ---
/// 取负节点。
pub struct OpNeg<T>(PhantomData<T>);

impl<T> Default for OpNeg<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpNeg<T>
where
    T: std::ops::Neg<Output = T> + Send + Sync + Copy + 'static,
{
    type Input = T;
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        Ok(-input)
    }
}

// --- 绝对值节点 ---
/// 绝对值节点。
pub struct OpAbs<T>(PhantomData<T>);

impl<T> Default for OpAbs<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpAbs<T>
where
    T: num_traits::Signed + Send + Sync + Copy + 'static,
{
    type Input = T;
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        Ok(input.abs())
    }
}

// --- 最大值节点 ---
/// 最大值节点。
pub struct OpMax<T>(PhantomData<T>);

impl<T> Default for OpMax<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpMax<T>
where
    T: Ord + Send + Sync + Copy + 'static,
{
    type Input = (T, T);
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        Ok(input.0.max(input.1))
    }
}

// --- 最小值节点 ---
/// 最小值节点。
pub struct OpMin<T>(PhantomData<T>);

impl<T> Default for OpMin<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpMin<T>
where
    T: Ord + Send + Sync + Copy + 'static,
{
    type Input = (T, T);
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        Ok(input.0.min(input.1))
    }
}

// --- 幂运算节点 (浮点数) ---
/// 幂运算节点。
pub struct OpPow<T>(PhantomData<T>);

impl<T> Default for OpPow<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpPow<T>
where
    T: num_traits::Float + Send + Sync + Copy + 'static,
{
    type Input = (T, T);
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        Ok(input.0.powf(input.1))
    }
}

// --- 平方根节点 ---
/// 平方根节点(负数输入返回错误)。
pub struct OpSqrt<T>(PhantomData<T>);

impl<T> Default for OpSqrt<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

impl<T> Node for OpSqrt<T>
where
    T: num_traits::Float + Send + Sync + Copy + 'static,
{
    type Input = T;
    type Output = Result<T, RoplatError>;
    type Error = RoplatError;

    async fn process(&mut self, input: Self::Input) -> Result<T, RoplatError> {
        if input < T::zero() {
            return Err(RoplatError::Arithmetic("负数无法开平方根".into()));
        }
        Ok(input.sqrt())
    }
}

// ==================== 测试 ====================

#[cfg(test)]
mod tests {
    use super::*;

    // ==================== 基本算术运算测试 ====================

    #[tokio::test]
    async fn test_op_add_integer() {
        let mut node = OpAdd::<i32>::default();

        assert_eq!(node.process((5, 3)).await.unwrap(), 8);
        assert_eq!(node.process((-5, 3)).await.unwrap(), -2);
        assert_eq!(node.process((0, 0)).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_op_add_float() {
        let mut node = OpAdd::<f64>::default();

        let result = node.process((1.5, 2.3)).await.unwrap();
        assert!((result - 3.8).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_op_sub_integer() {
        let mut node = OpSub::<i32>::default();

        assert_eq!(node.process((5, 3)).await.unwrap(), 2);
        assert_eq!(node.process((3, 5)).await.unwrap(), -2);
        assert_eq!(node.process((0, 0)).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_op_mul_integer() {
        let mut node = OpMul::<i32>::default();

        assert_eq!(node.process((5, 3)).await.unwrap(), 15);
        assert_eq!(node.process((-5, 3)).await.unwrap(), -15);
        assert_eq!(node.process((5, 0)).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_op_div_integer_success() {
        let mut node = OpDiv::<i32>::default();

        assert_eq!(node.process((10, 2)).await.unwrap(), 5);
        assert_eq!(node.process((-10, 2)).await.unwrap(), -5);
        assert_eq!(node.process((0, 5)).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_op_div_integer_zero_division() {
        let mut node = OpDiv::<i32>::default();

        let result = node.process((10, 0)).await;
        assert!(result.is_err());

        if let Err(RoplatError::Arithmetic(msg)) = result {
            assert!(msg.contains("除零"));
        } else {
            panic!("Expected Arithmetic error");
        }
    }

    // 注意:OpDiv 需要 CheckedDiv trait,f64 不支持
    // 测试已删除,因为 f64 不满足 trait bounds

    #[tokio::test]
    async fn test_op_rem_integer() {
        let mut node = OpRem::<i32>::default();

        assert_eq!(node.process((10, 3)).await.unwrap(), 1);
        assert_eq!(node.process((10, 2)).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_op_rem_zero_division() {
        let mut node = OpRem::<i32>::default();

        let result = node.process((10, 0)).await;
        assert!(result.is_err());

        if let Err(RoplatError::Arithmetic(msg)) = result {
            assert!(msg.contains("模零"));
        } else {
            panic!("Expected Arithmetic error");
        }
    }

    #[tokio::test]
    async fn test_op_neg_integer() {
        let mut node = OpNeg::<i32>::default();

        assert_eq!(node.process(5).await.unwrap(), -5);
        assert_eq!(node.process(-5).await.unwrap(), 5);
        assert_eq!(node.process(0).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_op_neg_float() {
        let mut node = OpNeg::<f64>::default();

        let result = node.process(std::f64::consts::PI).await.unwrap();
        assert!((result + std::f64::consts::PI).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_op_abs_integer() {
        let mut node = OpAbs::<i32>::default();

        assert_eq!(node.process(5).await.unwrap(), 5);
        assert_eq!(node.process(-5).await.unwrap(), 5);
        assert_eq!(node.process(0).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_op_abs_float() {
        let mut node = OpAbs::<f64>::default();

        assert_eq!(
            node.process(std::f64::consts::PI).await.unwrap(),
            std::f64::consts::PI
        );
        assert_eq!(
            node.process(-std::f64::consts::PI).await.unwrap(),
            std::f64::consts::PI
        );
    }

    // ==================== 比较运算测试 ====================

    #[tokio::test]
    async fn test_op_max_integer() {
        let mut node = OpMax::<i32>::default();

        assert_eq!(node.process((5, 3)).await.unwrap(), 5);
        assert_eq!(node.process((3, 5)).await.unwrap(), 5);
        assert_eq!(node.process((5, 5)).await.unwrap(), 5);
    }

    // 注意:OpMax 需要 Ord trait,f64 不完全支持(因为 NaN)
    // 测试已删除,因为 f64 不满足 trait bounds

    #[tokio::test]
    async fn test_op_min_integer() {
        let mut node = OpMin::<i32>::default();

        assert_eq!(node.process((5, 3)).await.unwrap(), 3);
        assert_eq!(node.process((3, 5)).await.unwrap(), 3);
        assert_eq!(node.process((5, 5)).await.unwrap(), 5);
    }

    // 注意:OpMin 需要 Ord trait,f64 不完全支持(因为 NaN)
    // 测试已删除,因为 f64 不满足 trait bounds

    // ==================== 浮点数运算测试 ====================

    #[tokio::test]
    async fn test_op_pow_float() {
        let mut node = OpPow::<f64>::default();

        let result = node.process((2.0, 3.0)).await.unwrap();
        assert!((result - 8.0).abs() < 1e-10);

        let result = node.process((10.0, 2.0)).await.unwrap();
        assert!((result - 100.0).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_op_pow_fractional() {
        let mut node = OpPow::<f64>::default();

        // 4^0.5 = sqrt(4) = 2
        let result = node.process((4.0, 0.5)).await.unwrap();
        assert!((result - 2.0).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_op_sqrt_positive() {
        let mut node = OpSqrt::<f64>::default();

        let result = node.process(9.0).await.unwrap();
        assert!((result - 3.0).abs() < 1e-10);

        let result = node.process(2.0).await.unwrap();
        assert!((result - std::f64::consts::SQRT_2).abs() < 1e-8);
    }

    #[tokio::test]
    async fn test_op_sqrt_zero() {
        let mut node = OpSqrt::<f64>::default();

        let result = node.process(0.0).await.unwrap();
        assert!((result - 0.0).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_op_sqrt_negative() {
        let mut node = OpSqrt::<f64>::default();

        let result = node.process(-1.0).await;
        assert!(result.is_err());

        if let Err(RoplatError::Arithmetic(msg)) = result {
            assert!(msg.contains("负数") || msg.contains("平方根"));
        } else {
            panic!("Expected Arithmetic error");
        }
    }

    // ==================== 边界情况测试 ====================

    #[tokio::test]
    async fn test_arithmetic_with_large_numbers() {
        let mut add = OpAdd::<i64>::default();
        let mut mul = OpMul::<i64>::default();

        assert_eq!(
            add.process((1_000_000, 2_000_000)).await.unwrap(),
            3_000_000
        );
        assert_eq!(mul.process((1000, 1000)).await.unwrap(), 1_000_000);
    }

    #[tokio::test]
    async fn test_arithmetic_with_negative_numbers() {
        let mut add = OpAdd::<i32>::default();
        let mut sub = OpSub::<i32>::default();
        let mut mul = OpMul::<i32>::default();

        assert_eq!(add.process((-5, -3)).await.unwrap(), -8);
        assert_eq!(add.process((-5, 3)).await.unwrap(), -2);
        assert_eq!(sub.process((-5, -3)).await.unwrap(), -2);
        assert_eq!(sub.process((5, -3)).await.unwrap(), 8);
        assert_eq!(mul.process((-5, 3)).await.unwrap(), -15);
        assert_eq!(mul.process((-5, -3)).await.unwrap(), 15);
    }

    #[tokio::test]
    async fn test_arithmetic_identity() {
        let mut add = OpAdd::<i32>::default();
        let mut mul = OpMul::<i32>::default();

        // 加法单位元: a + 0 = a
        assert_eq!(add.process((5, 0)).await.unwrap(), 5);
        // 乘法单位元: a * 1 = a
        assert_eq!(mul.process((5, 1)).await.unwrap(), 5);
    }

    #[tokio::test]
    async fn test_arithmetic_commutativity() {
        let mut add = OpAdd::<i32>::default();
        let mut mul = OpMul::<i32>::default();

        // 加法交换律: a + b = b + a
        assert_eq!(
            add.process((5, 3)).await.unwrap(),
            add.process((3, 5)).await.unwrap()
        );
        // 乘法交换律: a * b = b * a
        assert_eq!(
            mul.process((5, 3)).await.unwrap(),
            mul.process((3, 5)).await.unwrap()
        );
    }

    #[tokio::test]
    async fn test_float_precision() {
        let mut node = OpAdd::<f32>::default();

        let result = node.process((0.1, 0.2)).await.unwrap();
        // 浮点数精度问题,检查接近 0.3
        assert!((result - 0.3).abs() < 0.0001);
    }

    #[tokio::test]
    async fn test_chained_operations() {
        let mut add = OpAdd::<i32>::default();
        let mut mul = OpMul::<i32>::default();

        // (5 + 3) * 2 = 16
        let sum = add.process((5, 3)).await.unwrap();
        let result = mul.process((sum, 2)).await.unwrap();
        assert_eq!(result, 16);
    }

    #[tokio::test]
    async fn test_unsigned_operations() {
        let mut add = OpAdd::<u32>::default();
        let mut mul = OpMul::<u32>::default();

        assert_eq!(add.process((5, 3)).await.unwrap(), 8);
        assert_eq!(mul.process((5, 3)).await.unwrap(), 15);
    }
}