(t *testing.T)
| 2451 | } |
| 2452 | |
| 2453 | func TestPrefixEncodeCode(t *testing.T) { |
| 2454 | tests := []struct { |
| 2455 | n int // input value |
| 2456 | expectedCode int // expected prefix code |
| 2457 | expectedRemainder int // expected remainder value |
| 2458 | }{ |
| 2459 | // n <= 5: code should be max(0, n-1) and remainder 0. |
| 2460 | {-1, 0, 0}, // even negative numbers fall in this branch |
| 2461 | {0, 0, 0}, |
| 2462 | {1, 0, 0}, |
| 2463 | {2, 1, 0}, |
| 2464 | {3, 2, 0}, |
| 2465 | {4, 3, 0}, |
| 2466 | {5, 4, 0}, |
| 2467 | |
| 2468 | // n > 5: calculations using shifts. |
| 2469 | // For n = 6: n-1 = 5, loop runs once (5 >> 1 = 2) → shift=1, rem=2, |
| 2470 | // so returns (2 + 2*1, 6 - (2<<1) - 1) = (4, 1). |
| 2471 | {6, 4, 1}, |
| 2472 | |
| 2473 | // For n = 7: n-1 = 6, loop: 6 >> 1 = 3 → shift=1, rem=3, |
| 2474 | // so returns (3 + 2*1, 7 - (3<<1) - 1) = (5, 0). |
| 2475 | {7, 5, 0}, |
| 2476 | |
| 2477 | // For n = 8: n-1 = 7, loop: 7 >> 1 = 3 → shift=1, rem=3, |
| 2478 | // returns (3 + 2*1, 8 - (3<<1) - 1) = (5, 1). |
| 2479 | {8, 5, 1}, |
| 2480 | |
| 2481 | // For n = 9: n-1 = 8, loop: |
| 2482 | // 8 >> 1 = 4, shift becomes 1; then 4 >> 1 = 2, shift becomes 2; |
| 2483 | // rem == 2 so returns (2 + 2*2, 9 - (2<<2) - 1) = (6, 0). |
| 2484 | {9, 6, 0}, |
| 2485 | |
| 2486 | // For n = 10: returns (6, 1) |
| 2487 | {10, 6, 1}, |
| 2488 | |
| 2489 | // For n = 11: returns (6, 2) |
| 2490 | {11, 6, 2}, |
| 2491 | |
| 2492 | // For n = 12: returns (6, 3) |
| 2493 | {12, 6, 3}, |
| 2494 | |
| 2495 | // For n = 13: n-1 = 12, loop: 12 >> 1 = 6 (shift=1), |
| 2496 | // then 6 >> 1 = 3 (shift=2), rem becomes 3 so returns (3+2*2, 13 - (3<<2) -1) = (7, 0). |
| 2497 | {13, 7, 0}, |
| 2498 | |
| 2499 | // For n = 14: returns (7, 1) |
| 2500 | {14, 7, 1}, |
| 2501 | |
| 2502 | // For n = 15: returns (7, 2) |
| 2503 | {15, 7, 2}, |
| 2504 | |
| 2505 | // For n = 16: returns (7, 3) |
| 2506 | {16, 7, 3}, |
| 2507 | } |
| 2508 | for idx, tt := range tests { |
| 2509 | code, remainder := prefixEncodeCode(tt.n) |
| 2510 |
nothing calls this directly
no test coverage detected
searching dependent graphs…