File: 06_relay.t

package info (click to toggle)
libmce-perl 1.901-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,316 kB
  • sloc: perl: 14,091; makefile: 7
file content (136 lines) | stat: -rw-r--r-- 3,251 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl

use strict;
use warnings;
use utf8;
use open qw(:std :utf8);

use Test::More;

BEGIN {
   use_ok 'MCE::Flow';
}

###############################################################################

## Relay ARRAY

## input_data is not required to run mce_flow
##
## statement(s) between relay_recv and relay
## are processed serially and orderly

{
   my @ret = mce_flow {
      max_workers => 2,
      init_relay  => [ 1, 1 ],
   },
   sub {
      my $ind = MCE->wid - 1;
      for my $i ( 1 .. 4 ) {
         my @data = MCE->relay_recv;
         MCE->gather( $data[ $ind ] );
         MCE->relay( sub { $_->[ $ind ] += 1 } );
      }
   };

   MCE::Flow->finish;

   my @data = MCE->relay_final;

   is( join('', sort @ret), '11223344', 'check relayed data - array' );
   is( join('', @data), '55',           'check final value - array' );
}

###############################################################################

## Relay HASH

{
   my @ret = mce_flow {
      max_workers => 2,
      init_relay  => { 1 => 1, 2 => 1 },
   },
   sub {
      my $key = MCE->wid;
      for my $i ( 1 .. 4 ) {
         my %data = MCE->relay_recv;
         MCE->gather( $data{ $key } );
         MCE->relay( sub { $_->{ $key } += 1 } );
      }
   };

   MCE::Flow->finish;

   my %data = MCE->relay_final;

   is( join('', sort @ret), '11223344', 'check relayed data - hash' );
   is( join('', values %data), '55',    'check final value - hash' );
}

###############################################################################

## Relay SCALAR

{
   my @ret = mce_flow {
      max_workers => 2,
      init_relay  => 1,
   },
   sub {
      for my $i ( 1 .. 4 ) {
         my $n = MCE->relay_recv;
         MCE->gather( $n );
         MCE->relay( sub { $_ += 1 } );
      }
   };

   MCE::Flow->finish;

   my $val = MCE->relay_final;

   is( join('', sort @ret), '12345678', 'check relayed data - scalar' );
   is( $val, '9',                       'check final value - scalar' );
}

###############################################################################

## Relay UTF-8. This also tests gathering UTF-8 strings.

## https://sacred-texts.com/cla/usappho/sph02.htm (VII)

my $sappho_text =
   "ἔλθε μοι καὶ νῦν, χαλεπᾶν δὲ λῦσον\n".
   "ἐκ μερίμναν ὄσσα δέ μοι τέλεσσαι\n".
   "θῦμοσ ἰμμέρρει τέλεσον, σὐ δ᾽ αὔτα\n".
   "σύμμαχοσ ἔσσο.\n";

my $translation =
   "Come then, I pray, grant me surcease from sorrow,\n".
   "Drive away care, I beseech thee, O goddess\n".
   "Fulfil for me what I yearn to accomplish,\n".
   "Be thou my ally.\n";

{
   my @data = mce_flow {
      max_workers => 2,
      init_relay  => $sappho_text,
   },
   sub {
      MCE->relay( sub {
          $_ .= "ὲ";
          MCE->gather( "ἔλθε μοι καὶ νῦν".MCE->wid );
      });
   };

   MCE::Flow->finish;

   my $text = MCE->relay_final;

   is( $data[0], "ἔλθε μοι καὶ νῦν"."1" , 'check gathered data - worker 1' );
   is( $data[1], "ἔλθε μοι καὶ νῦν"."2" , 'check gathered data - worker 2' );
   is( $text   , $sappho_text."ὲὲ"      , 'check final value - utf8' );
}

done_testing;