Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

61
LINES

< > BotCompany Repo | #1032919 // Test_ByteArraysPartialInputStream

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5330L/30K).

1  
set flag ByteArraysPartialInputStream_debug.
2  
3  
sclass Test_ByteArraysPartialInputStream {
4  
  ByteArraysPartialInputStream stream;
5  
  int n = 1000;
6  
  byte[] data;
7  
8  
  void writeInRandomRanges(byte[] data) ctex {
9  
    for (r : splitIntoRandomRangesOfApproximateLength(10, IntRange(0, l(data))))
10  
      stream.write(subByteArray(data, r));
11  
  }
12  
  
13  
  void writeAndReadInRandomRanges(byte[] data) ctex {
14  
    int n = l(data), iRead = 0;
15  
    for (r : splitIntoRandomRangesOfApproximateLength(10, IntRange(0, n))) {
16  
      stream.write(subByteArray(data, r));
17  
      int readTo = random_incl(iRead, r.end);
18  
      //printVars(+n, +r, +readTo, +iRead);
19  
      for (; iRead < r.end; iRead++)
20  
        assertEquals("Byte " + iRead, ubyteToInt(data[iRead]), stream.read());
21  
        
22  
      // partially out of data
23  
      if (iRead == r.end)
24  
        assertEquals(-1, stream.read());
25  
    }
26  
    
27  
    // check reading rest of data
28  
    for (; iRead < n; iRead++)
29  
      assertEquals("Byte " + iRead, ubyteToInt(data[iRead]), stream.read());
30  
  }
31  
  
32  
  void checkRead(byte[] data) ctex {
33  
    checkReadWithMark(data, -1);
34  
  }
35  
  
36  
  void checkReadWithMark(byte[] data, int markPos) ctex {
37  
    for i over data: {
38  
      if (i == markPos) stream.mark(n);
39  
      assertEquals("Byte " + i, ubyteToInt(data[i]), stream.read());
40  
    }
41  
    assertEquals(-1, stream.read());
42  
  }
43  
  
44  
  void test2() ctex {
45  
    stream = new ByteArraysPartialInputStream;
46  
    writeAndReadInRandomRanges(randomBytes(n));
47  
  }
48  
  
49  
  void test1() ctex {
50  
    stream = new ByteArraysPartialInputStream;
51  
    data = randomBytes(n);
52  
    writeInRandomRanges(data);
53  
    int markPos = random(n);
54  
    
55  
    checkReadWithMark(data, markPos);
56  
    print("Resetting to " + markPos);
57  
    stream.reset();
58  
    checkRead(subByteArray(data, markPos));
59  
    print("Read OK");
60  
  }
61  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032919
Snippet name: Test_ByteArraysPartialInputStream
Eternal ID of this version: #1032919/25
Text MD5: 9a95b009a0b93a396cd7abe41d72befa
Transpilation MD5: fe58b49d11de9c3baa80c658579b1e0f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-10 23:21:16
Source code size: 1846 bytes / 61 lines
Pitched / IR pitched: No / No
Views / Downloads: 177 / 359
Version history: 24 change(s)
Referenced in: [show references]