|
| 1 | +// Copyright 2024 The XLS Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// The proc should just pass incoming data as literals to LiteralsBuffer. |
| 16 | +// Packets of 0 length are not passed further and a warning is log instead. |
| 17 | + |
| 18 | +import xls.modules.zstd.common; |
| 19 | + |
| 20 | +type LiteralsData = common::LiteralsData; |
| 21 | +type LitData = common::LitData; |
| 22 | +type LitLength = common::LitLength; |
| 23 | + |
| 24 | +proc RawLiteralsDecoder { |
| 25 | + dispatcher_r: chan<LiteralsData> in; |
| 26 | + buffer_s: chan<LiteralsData> out; |
| 27 | + |
| 28 | + config(dispatcher_r: chan<LiteralsData> in, buffer_s: chan<LiteralsData> out) { |
| 29 | + (dispatcher_r, buffer_s) |
| 30 | + } |
| 31 | + |
| 32 | + init { } |
| 33 | + |
| 34 | + next(tok: token, state: ()) { |
| 35 | + let (tok, resp) = recv(tok, dispatcher_r); |
| 36 | + let do_send = if resp.length == LitLength:0 { |
| 37 | + trace_fmt!("[WARNING] Packet of 0 length received by RawLiteralsDecoder"); |
| 38 | + false |
| 39 | + } else { |
| 40 | + true |
| 41 | + }; |
| 42 | + let tok = send_if(tok, buffer_s, do_send, resp); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[test_proc] |
| 47 | +proc RawLiteralsDecoderTest { |
| 48 | + terminator: chan<bool> out; |
| 49 | + dispatcher_s: chan<LiteralsData> out; |
| 50 | + buffer_r: chan<LiteralsData> in; |
| 51 | + |
| 52 | + config(terminator: chan<bool> out) { |
| 53 | + let (dispatcher_s, dispatcher_r) = chan<LiteralsData>("dispatcher"); |
| 54 | + let (buffer_s, buffer_r) = chan<LiteralsData>("buffer"); |
| 55 | + |
| 56 | + spawn RawLiteralsDecoder(dispatcher_r, buffer_s); |
| 57 | + |
| 58 | + (terminator, dispatcher_s, buffer_r) |
| 59 | + } |
| 60 | + |
| 61 | + init { } |
| 62 | + |
| 63 | + next(tok: token, state: ()) { |
| 64 | + let data = LiteralsData { data: LitData:0x11_22_33_44_55_66, length: LitLength:6, last: true }; |
| 65 | + let tok = send(tok, dispatcher_s, data); |
| 66 | + let (tok, resp) = recv(tok, buffer_r); |
| 67 | + assert_eq(resp, data); |
| 68 | + |
| 69 | + let empty_data = LiteralsData { data: LitData:0, length: LitLength:0, last: true }; |
| 70 | + let tok = send(tok, dispatcher_s, empty_data); |
| 71 | + |
| 72 | + // Resend the first packet to verify that the empty packet is dropped correctly. |
| 73 | + let tok = send(tok, dispatcher_s, data); |
| 74 | + let (tok, resp) = recv(tok, buffer_r); |
| 75 | + assert_eq(resp, data); |
| 76 | + |
| 77 | + let tok = send(tok, terminator, true); |
| 78 | + } |
| 79 | +} |
0 commit comments