File: format.rs

package info (click to toggle)
rust-ndarray 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,028 kB
  • sloc: sh: 30; makefile: 2
file content (76 lines) | stat: -rw-r--r-- 1,441 bytes parent folder | download
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
use ndarray::prelude::*;
use ndarray::rcarr1;

#[test]
fn formatting()
{
    let a = rcarr1::<f32>(&[1., 2., 3., 4.]);
    assert_eq!(format!("{}", a), "[1, 2, 3, 4]");
    assert_eq!(format!("{:4}", a), "[   1,    2,    3,    4]");
    let a = a.into_shape_clone((4, 1, 1)).unwrap();
    assert_eq!(
        format!("{}", a),
        "\
[[[1]],

 [[2]],

 [[3]],

 [[4]]]"
    );
    assert_eq!(
        format!("{:4}", a),
        "\
[[[   1]],

 [[   2]],

 [[   3]],

 [[   4]]]",
    );

    let a = a.into_shape_clone((2, 2)).unwrap();
    assert_eq!(
        format!("{}", a),
        "\
[[1, 2],
 [3, 4]]"
    );
    assert_eq!(
        format!("{:4}", a),
        "\
[[   1,    2],
 [   3,    4]]"
    );

    let b = arr0::<f32>(3.5);
    assert_eq!(format!("{}", b), "3.5");

    let s = format!("{:.3e}", aview1::<f32>(&[1.1, 2.2, 33., 440.]));
    assert_eq!(s, "[1.100e0, 2.200e0, 3.300e1, 4.400e2]");

    let s = format!("{:02x}", aview1::<u8>(&[1, 0xff, 0xfe]));
    assert_eq!(s, "[01, ff, fe]");
}

#[test]
fn debug_format()
{
    let a = Array2::<i32>::zeros((3, 4));
    assert_eq!(
        format!("{:?}", a),
        "\
[[0, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 0]], shape=[3, 4], strides=[4, 1], layout=Cc (0x5), const ndim=2"
    );
    assert_eq!(
        format!("{:?}", a.into_dyn()),
        "\
[[0, 0, 0, 0],
 [0, 0, 0, 0],
 [0, 0, 0, 0]], shape=[3, 4], strides=[4, 1], layout=Cc (0x5), dynamic ndim=2"
    );
}