1function test02
2%TEST02 test GrB_*_dup
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7[~, ~, ~, types, ~, ~] = GB_spec_opsall ;
8GB_builtin_complex_set (1) ;
9types = types.all ;
10
11rng ('default') ;
12format long g
13
14for k1 = 1:length (types)
15    atype = types {k1} ;
16
17    for is_hyper = 0:1
18        for is_csc = 0:1
19
20            % create a random A
21            A = GB_spec_random (4, 4, 0.8, 128, atype, is_csc, is_hyper) ;
22            A.matrix (1,1) = -1 ;
23            A.pattern (1,1) = true ;
24            A_matrix = full (A.matrix) ;
25            A_pattern = full (A.pattern) ;
26            assert (GB_spok (1*A.matrix) == 1) ;
27            assert (GB_spok (A.pattern) == 1) ;
28
29            for k2 = 1:length (types)
30                ctype = types {k2} ;
31                % typecast to type of C
32
33                C = GB_mex_dup (A, ctype) ;
34                C_matrix = full (C.matrix) ;
35                C_pattern = full (GB_spones_mex (C.matrix)) ;
36                assert (GB_spok (1*C.matrix) == 1) ;
37
38                if (k1 == k2)
39                    % also try another method
40                    assert (isequal (A_pattern, C_pattern)) ;
41                    assert (isequal (A.class, C.class)) ;
42
43                    C2 = GB_mex_dup (A, ctype, 1) ;
44                    C2_matrix = full (C2.matrix) ;
45                    C2_pattern = full (GB_spones_mex (C2.matrix)) ;
46                    assert (isequal (C, C2))  ;
47                    assert (GB_spok (1*C2.matrix) == 1) ;
48                end
49
50            end
51        end
52    end
53end
54
55% try with both built-in and user-defined 'double complex' types:
56for k = [false true]
57    GB_builtin_complex_set (k) ;
58
59    % duplicate a complex matrix (user-defined can't be typecasted)
60    A = GB_mex_random (4, 4, 10, 1) ;
61    assert (GB_spok (1*A) == 1) ;
62
63    C = GB_mex_dup (A) ;
64    % C_matrix = full (C.matrix) ;
65    assert (isequal (A, C.matrix))  ;
66    assert (GB_spok (1*C.matrix) == 1) ;
67
68    C = GB_mex_dup (A, 'double complex', 1) ;
69    % C_matrix = full (C.matrix) ;
70    assert (isequal (A, C.matrix))  ;
71    assert (GB_spok (1*C.matrix) == 1) ;
72end
73
74format
75
76fprintf ('test02: all typecast and copy tests passed\n') ;
77
78