@@ -253,158 +253,6 @@ describe('Buffers', () => {
253
253
expect ( cursor . lineOffset ) . toEqual ( 1 ) ;
254
254
expect ( list . pos ( cursor . offset ) ) . toEqual ( { buf : cursor . line , offset : cursor . lineOffset } ) ;
255
255
} ) ;
256
-
257
- // 测试空缓冲区行为
258
- describe ( 'Empty Buffer Handling' , ( ) => {
259
- it ( 'should handle empty buffer operations' , ( ) => {
260
- const empty = new Buffers ( ) ;
261
-
262
- expect ( empty . byteLength ) . toBe ( 0 ) ;
263
- expect ( empty . slice ( ) ) . toEqual ( new Uint8Array ( 0 ) ) ;
264
- expect ( ( ) => empty . pos ( 0 ) ) . toThrow ( 'out of range' ) ;
265
- expect ( empty . splice ( 0 , 0 ) ) . toEqual ( expect . any ( Buffers ) ) ;
266
- } ) ;
267
- } ) ;
268
-
269
- // 测试边界slice操作
270
- describe ( 'Edge Case Slicing' , ( ) => {
271
- const buffer = create ( [ 0 , 1 , 2 , 3 , 4 , 5 ] , [ 3 , 3 ] ) ;
272
-
273
- it ( 'should handle start at chunk boundary' , ( ) => {
274
- expect ( buffer . slice ( 3 , 5 ) ) . toEqual ( new Uint8Array ( [ 3 , 4 ] ) ) ;
275
- } ) ;
276
-
277
- it ( 'should handle end at chunk boundary' , ( ) => {
278
- expect ( buffer . slice ( 2 , 3 ) ) . toEqual ( new Uint8Array ( [ 2 ] ) ) ;
279
- } ) ;
280
- } ) ;
281
- it ( 'should handle splice at exact chunk boundary' , ( ) => {
282
- const buffer = createEnhanced ( [ 0 , 1 , 2 , 3 , 4 , 5 ] , [ 3 , 3 ] ) ;
283
- buffer . splice ( 3 , 2 , new Uint8Array ( [ 99 ] ) ) ;
284
- expect ( buffer . slice ( ) ) . toEqual ( new Uint8Array ( [ 0 , 1 , 2 , 99 , 5 ] ) ) ;
285
- } ) ;
286
- // 测试非法索引访问
287
- describe ( 'Invalid Access Handling' , ( ) => {
288
- const buffer = create ( [ 1 , 2 , 3 ] , [ 3 ] ) ;
289
-
290
- it ( 'should throw on negative index' , ( ) => {
291
- expect ( ( ) => buffer . pos ( - 1 ) ) . toThrow ( 'out of range' ) ;
292
- } ) ;
293
-
294
- it ( 'should throw on overflow index' , ( ) => {
295
- expect ( ( ) => buffer . pos ( 4 ) ) . toThrow ( 'out of range' ) ;
296
- } ) ;
297
- } ) ;
298
-
299
- // 测试超大缓冲区
300
- describe ( 'Large Buffer Handling' , ( ) => {
301
- const MB1 = new Uint8Array ( 1024 * 1024 ) ;
302
- const buffer = new Buffers ( ) ;
303
-
304
- beforeAll ( ( ) => {
305
- // 填充1MB数据
306
- for ( let i = 0 ; i < 1024 ; i ++ ) {
307
- buffer . push ( MB1 . subarray ( 0 , 1024 ) ) ;
308
- }
309
- } ) ;
310
-
311
- it ( 'should handle 1GB data slicing' , ( ) => {
312
- const slice = buffer . slice ( 1024 * 512 , 1024 * 512 + 100 ) ;
313
- expect ( slice . byteLength ) . toBe ( 100 ) ;
314
- } ) ;
315
- } ) ;
316
-
317
- // 测试跨chunk的splice操作
318
- describe ( 'Cross-Chunk Splicing' , ( ) => {
319
- const buffer = create ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] , [ 3 , 3 , 4 ] ) ;
320
-
321
- it ( 'should splice across multiple chunks' , ( ) => {
322
- const removed = buffer . splice ( 2 , 5 , new Uint8Array ( [ 99 ] ) ) ;
323
-
324
- expect ( buffer . slice ( ) ) . toEqual ( new Uint8Array ( [ 0 , 1 , 99 , 7 , 8 , 9 ] ) ) ;
325
- expect ( removed . slice ( ) ) . toEqual ( new Uint8Array ( [ 2 , 3 , 4 , 5 , 6 ] ) ) ;
326
- } ) ;
327
- } ) ;
328
-
329
- // 测试slice4特殊方法
330
- describe ( 'slice4 Special Cases' , ( ) => {
331
- it ( 'should handle partial slice4' , ( ) => {
332
- const buffer = create ( [ 1 , 2 , 3 ] , [ 3 ] ) ;
333
- expect ( buffer . slice4 ( 2 ) ) . toEqual ( new Uint8Array ( [ 3 , 0 , 0 , 0 ] ) ) ;
334
- } ) ;
335
-
336
- it ( 'should handle edge slice4' , ( ) => {
337
- const buffer = create ( [ 1 , 2 , 3 , 4 , 5 ] , [ 5 ] ) ;
338
- expect ( buffer . slice4 ( 1 ) ) . toEqual ( new Uint8Array ( [ 2 , 3 , 4 , 5 ] ) ) ;
339
- } ) ;
340
- } ) ;
341
-
342
- // 测试Cursor高级功能
343
- describe ( 'Cursor Advanced Operations' , ( ) => {
344
- const buffer = create ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] , [ 3 , 3 , 4 ] ) ;
345
-
346
- it ( 'should handle moveTo across chunks' , ( ) => {
347
- const cursor = buffer . cursor ( ) ;
348
- cursor . moveTo ( 5 ) ;
349
- expect ( cursor . line ) . toBe ( 1 ) ;
350
- expect ( cursor . lineOffset ) . toBe ( 2 ) ;
351
- } ) ;
352
-
353
- it ( 'should reset correctly' , ( ) => {
354
- const cursor = buffer . cursor ( 5 ) ;
355
- cursor . reset ( ) ;
356
- expect ( cursor . offset ) . toBe ( 0 ) ;
357
- expect ( cursor . line ) . toBe ( 0 ) ;
358
- } ) ;
359
- } ) ;
360
-
361
- // 测试Dispose行为
362
- describe ( 'Resource Management' , ( ) => {
363
- it ( 'should clear resources on dispose' , ( ) => {
364
- const buffer = create ( [ 1 , 2 , 3 ] , [ 3 ] ) ;
365
- buffer . dispose ( ) ;
366
-
367
- expect ( buffer . buffers ) . toEqual ( [ ] ) ;
368
- expect ( buffer . byteLength ) . toBe ( 0 ) ;
369
- } ) ;
370
- } ) ;
371
-
372
- // 性能测试
373
- describe ( 'Performance Tests' , ( ) => {
374
- let largeBuffer : Buffers ;
375
-
376
- beforeAll ( ( ) => {
377
- largeBuffer = new Buffers ( ) ;
378
- // 创建包含1000个10KB chunk的缓冲区
379
- for ( let i = 0 ; i < 1000 ; i ++ ) {
380
- largeBuffer . push ( new Uint8Array ( 10 * 1024 ) ) ;
381
- }
382
- } ) ;
383
-
384
- it ( 'should handle slicing 1MB data under 50ms' , ( ) => {
385
- const start = performance . now ( ) ;
386
- const slice = largeBuffer . slice ( 0 , 1024 * 1024 ) ;
387
- const duration = performance . now ( ) - start ;
388
-
389
- expect ( duration ) . toBeLessThan ( 50 ) ;
390
- expect ( slice . byteLength ) . toBe ( 1024 * 1024 ) ;
391
- } ) ;
392
-
393
- it ( 'should handle 1000 splices under 1s' , ( ) => {
394
- const buf = createEnhanced (
395
- new Array ( 10000 ) . fill ( 0 ) . map ( ( _ , i ) => i ) ,
396
- [ 100 , 900 , 9000 ] ,
397
- ) ;
398
-
399
- const start = performance . now ( ) ;
400
- for ( let i = 0 ; i < 1000 ; i ++ ) {
401
- buf . splice ( i % 100 , 5 , new Uint8Array ( [ i ] ) ) ;
402
- }
403
- const duration = performance . now ( ) - start ;
404
-
405
- expect ( duration ) . toBeLessThan ( 1000 ) ;
406
- } ) ;
407
- } ) ;
408
256
} ) ;
409
257
410
258
function create ( xs : number [ ] , split : number [ ] ) {
@@ -416,16 +264,3 @@ function create(xs: number[], split: number[]) {
416
264
} ) ;
417
265
return bufs ;
418
266
}
419
-
420
- function createEnhanced ( xs : number [ ] , split : number [ ] ) : Buffers {
421
- const bufs = new Buffers ( ) ;
422
- let offset = 0 ;
423
- split . forEach ( ( chunkSize ) => {
424
- if ( chunkSize > 0 ) {
425
- const chunk = new Uint8Array ( xs . slice ( offset , offset + chunkSize ) ) ;
426
- bufs . push ( chunk ) ;
427
- offset += chunkSize ;
428
- }
429
- } ) ;
430
- return bufs ;
431
- }
0 commit comments