Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/GAP/tst/testinstall/hpc/   (GAP Algebra Version 4.15.1©)  Datei vom 18.9.2025 mit Größe 3 kB image not shown  

Quelle  channels.tst   Sprache: unbekannt

 
Spracherkennung für: .tst vermutete Sprache: Unknown {[0] [0] [0]} [Methode: Schwerpunktbildung, einfache Gewichte, sechs Dimensionen]

#@if IsHPCGAP
# Tests for HPC-GAP channels
#
# TODO: right now these tests are all using a single thread; add some which
# use multiple threads, and also non-dynamic (i.e., blocking) channels.
#
# create a dynamic channel
gap> ch := CreateChannel();;
gap> SendChannel(ch, 3);
gap> SendChannel(ch, 2);
gap> SendChannel(ch, 1);
gap> TallyChannel(ch);
3
gap> InspectChannel(ch);
321 ]
gap> ReceiveChannel(ch);
3
gap> InspectChannel(ch);
21 ]
gap> ReceiveChannel(ch);
2

# grow the channel till it is full at capacity (initially 10)
# note that since we already pre-filled the channel with some data,
# the loop causes it to "wrap around" (internally a channel is a
# queue, implemented as a ring buffer).
gap> for i in [2..9] do SendChannel(ch, i); od;
gap> TallyChannel(ch);
9

# empty the channel
gap> List([1..9], i -> ReceiveChannel(ch));
123456789 ]

# now fill it again, this time exceeding the initial capacity
gap> for i in [1..20] do SendChannel(ch, i); od;
gap> TallyChannel(ch);
20

# we are done with this channel
gap> DestroyChannel(ch);

# new one; this time, we test what happens if we expand when the head of
# the queue is near the end of the underlying plist
gap> ch := CreateChannel();;
gap> for i in [1..9] do SendChannel(ch, i); od;
gap> List([1..8], i -> ReceiveChannel(ch));
12345678 ]
gap> for i in [10..19] do SendChannel(ch, i); od;
gap> DestroyChannel(ch);

#
# Create a thread that blocks while trying to send to a channel
#
gap> chA := CreateChannel(5);;
gap> chB := CreateChannel();;
gap> for i in [1..5] do SendChannel(chA,i); od;
gap> [ InspectChannel(chA), InspectChannel(chB) ];
[ [ 12345 ], [  ] ]
gap> t:=CreateThread(
> function()
> local x;
> SendChannel(chA,42); # this blocks until another thread reads from chA
> x:=ReceiveChannel(chA);
> SendChannel(chB,x);
> end);;
gap> [ InspectChannel(chA), InspectChannel(chB) ];
[ [ 12345 ], [  ] ]
gap> ReceiveChannel(chA);
1
gap> WaitThread(t);
gap> [ InspectChannel(chA), InspectChannel(chB) ];
[ [ 34542 ], [ 2 ] ]

#
# Create a thread that blocks while trying to read from several channels
#
gap> chA := CreateChannel(5);
<channel with 0/5 elements, 0 waiting>
gap> chB := CreateChannel();
<channel with 0 elements, 0 waiting>
gap> [ InspectChannel(chA), InspectChannel(chB) ];
[ [  ], [  ] ]
gap> t:=CreateThread(function() ReceiveAnyChannel(chA,chB); end);;
gap> [ InspectChannel(chA), InspectChannel(chB) ];
[ [  ], [  ] ]
gap> MultiTransmitChannel(chA, [ 123 ]);
gap> WaitThread(t);
gap> [ InspectChannel(chA), InspectChannel(chB) ];
[ [ 23 ], [  ] ]
gap> TryMultiSendChannel(chA, [10..19]);
3
gap> TryMultiTransmitChannel(chB, [20..29]);
10
gap> [ InspectChannel(chA), InspectChannel(chB) ];
[ [ 23101112 ], [ 20212223242526272829 ] ]

#
# Some manual examples
#

#
gap> ch := CreateChannel(1);;
gap> TrySendChannel(ch, 1);
true
gap> TrySendChannel(ch, 2);
false

#
gap> ch := CreateChannel();;
gap> SendChannel(ch, 99);
gap> TryReceiveChannel(ch, fail);
99
gap> TryReceiveChannel(ch, fail);
fail

#
gap> ch:=CreateChannel();;
gap> MultiSendChannel(ch, [12345678910]);
gap> MultiReceiveChannel(ch,7);
1234567 ]
gap> MultiReceiveChannel(ch,7);
8910 ]
gap> MultiReceiveChannel(ch,7);
[  ]

#
gap> ch1 := CreateChannel();;
gap> ch2 := CreateChannel();;
gap> SendChannel(ch2, [123]);;
gap> ReceiveAnyChannel(ch1, ch2);
123 ]

#
gap> ch1 := CreateChannel();;
gap> ch2 := CreateChannel();;
gap> SendChannel(ch2, [123]);;
gap> ReceiveAnyChannelWithIndex(ch1, ch2);
[ [ 123 ], 2 ]

# TODO: add tests for these:
# TransmitChannel
# MultiTransmitChannel
# TryMultiSendChannel
# TryMultiTransmitChannel
# TryTransmitChannel
# ReceiveAnyChannel

#@fi

[Dauer der Verarbeitung: 0.11 Sekunden, vorverarbeitet 2026-06-17]