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).

set flag ByteArraysPartialInputStream_debug.

sclass Test_ByteArraysPartialInputStream {
  ByteArraysPartialInputStream stream;
  int n = 1000;
  byte[] data;

  void writeInRandomRanges(byte[] data) ctex {
    for (r : splitIntoRandomRangesOfApproximateLength(10, IntRange(0, l(data))))
      stream.write(subByteArray(data, r));
  }
  
  void writeAndReadInRandomRanges(byte[] data) ctex {
    int n = l(data), iRead = 0;
    for (r : splitIntoRandomRangesOfApproximateLength(10, IntRange(0, n))) {
      stream.write(subByteArray(data, r));
      int readTo = random_incl(iRead, r.end);
      //printVars(+n, +r, +readTo, +iRead);
      for (; iRead < r.end; iRead++)
        assertEquals("Byte " + iRead, ubyteToInt(data[iRead]), stream.read());
        
      // partially out of data
      if (iRead == r.end)
        assertEquals(-1, stream.read());
    }
    
    // check reading rest of data
    for (; iRead < n; iRead++)
      assertEquals("Byte " + iRead, ubyteToInt(data[iRead]), stream.read());
  }
  
  void checkRead(byte[] data) ctex {
    checkReadWithMark(data, -1);
  }
  
  void checkReadWithMark(byte[] data, int markPos) ctex {
    for i over data: {
      if (i == markPos) stream.mark(n);
      assertEquals("Byte " + i, ubyteToInt(data[i]), stream.read());
    }
    assertEquals(-1, stream.read());
  }
  
  void test2() ctex {
    stream = new ByteArraysPartialInputStream;
    writeAndReadInRandomRanges(randomBytes(n));
  }
  
  void test1() ctex {
    stream = new ByteArraysPartialInputStream;
    data = randomBytes(n);
    writeInRandomRanges(data);
    int markPos = random(n);
    
    checkReadWithMark(data, markPos);
    print("Resetting to " + markPos);
    stream.reset();
    checkRead(subByteArray(data, markPos));
    print("Read OK");
  }
}

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: 175 / 357
Version history: 24 change(s)
Referenced in: [show references]