25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

118 satır
4.8KB

  1. # E02-S01: build the source-reader test PRG and a fresh D81 holding it plus
  2. # generated PETSCII fixtures. Writes build/source-test/expected.json with the
  3. # results the host computes independently of the C128 code.
  4. [CmdletBinding()]
  5. param()
  6. $ErrorActionPreference = "Stop"
  7. $project = Split-Path $PSScriptRoot -Parent
  8. Import-Module (Join-Path $PSScriptRoot "lib\Nova128Test.psm1") -Force
  9. $out = Join-Path $project "build\source-test"
  10. New-Item -ItemType Directory -Force -Path $out | Out-Null
  11. $link = Invoke-Nova128Link -Name "source-test" -BuildDir $out `
  12. -Config (Join-Path $project "cfg\compiler.cfg") -Sources @(
  13. (Join-Path $project "src\compiler\mmu.s"),
  14. (Join-Path $project "src\compiler\diskio.s"),
  15. (Join-Path $project "src\compiler\source.s"),
  16. (Join-Path $project "tests\source\source-test.s"))
  17. if ((Get-Nova128Label $link.Labels "test_entry") -ne 0x1C0D) { throw "test_entry is not at `$1C0D" }
  18. # ---------------------------------------------------------------- fixtures
  19. function Get-Lines([byte[]]$bytes) {
  20. # Split exactly as src_next_line does: $0D ends a line; trailing bytes
  21. # without $0D form a last line.
  22. $lines = New-Object System.Collections.Generic.List[byte[]]
  23. $start = 0
  24. for ($i = 0; $i -lt $bytes.Length; $i++) {
  25. if ($bytes[$i] -eq 13) {
  26. if ($i -gt $start) { $lines.Add([byte[]]$bytes[$start..($i - 1)]) }
  27. else { $lines.Add([byte[]]@()) }
  28. $start = $i + 1
  29. }
  30. }
  31. if ($start -lt $bytes.Length) { $lines.Add([byte[]]$bytes[$start..($bytes.Length - 1)]) }
  32. return ,$lines
  33. }
  34. function Get-Fletcher([byte[]]$bytes) {
  35. $s1 = 0; $s2 = 0
  36. foreach ($line in (Get-Lines $bytes)) {
  37. foreach ($b in (@($line) + 13)) {
  38. $s1 = ($s1 + $b) % 255
  39. $s2 = ($s2 + $s1) % 255
  40. }
  41. }
  42. return @($s1, $s2)
  43. }
  44. function New-Line([int]$length, [int]$seed) {
  45. # Mixed case so both PETSCII letter ranges appear.
  46. $alphabet = "abcdefghijKLMNOPQRST0123456789 =+()"
  47. $chars = for ($i = 0; $i -lt $length; $i++) { $alphabet[($i + $seed) % $alphabet.Length] }
  48. return (-join $chars)
  49. }
  50. $fixtures = [ordered]@{}
  51. $decls = ConvertTo-Nova128Petscii (Get-Content -Raw (Join-Path $project "tests\grammar\accepted\decls.n8"))
  52. if ($decls[-1] -ne 13) { throw "decls.n8 should end with a newline" }
  53. $fixtures["decls.n8"] = $decls
  54. $fixtures["nofinal.n8"] = [byte[]]$decls[0..($decls.Length - 2)]
  55. $edge = New-Object System.Text.StringBuilder
  56. [void]$edge.Append((New-Line 80 0) + "`n") # exactly the line limit
  57. for ($i = 0; $i -lt 598; $i++) {
  58. $len = if ($i -lt 380) { 31 } else { 30 } # totals exactly 18,999 bytes
  59. [void]$edge.Append((New-Line $len $i) + "`n")
  60. }
  61. $fixtures["edge.n8"] = ConvertTo-Nova128Petscii $edge.ToString()
  62. $long = "a = 1`nb = 2`n" + (New-Line 81 3) + "`nc = 3`n"
  63. $fixtures["longln.n8"] = ConvertTo-Nova128Petscii $long
  64. $many = (1..600 | ForEach-Object { "x = $_" }) -join "`n"
  65. $fixtures["manyln.n8"] = ConvertTo-Nova128Petscii ($many + "`n")
  66. $big = (1..500 | ForEach-Object { New-Line 37 $_ }) -join "`n"
  67. $fixtures["big.n8"] = ConvertTo-Nova128Petscii ($big + "`n")
  68. # Boundary sanity: these are the facts the expectations below rely on.
  69. $edgeLines = Get-Lines $fixtures["edge.n8"]
  70. if ($fixtures["edge.n8"].Length -ne 18999 -or $edgeLines.Count -ne 599 -or $edgeLines[0].Length -ne 80) {
  71. throw "edge.n8 is not at the exact limits"
  72. }
  73. if ($fixtures["big.n8"].Length -ne 19000) { throw "big.n8 is not 19,000 bytes" }
  74. $hasLow = $decls | Where-Object { $_ -ge 0x41 -and $_ -le 0x5A }
  75. $hasHigh = $decls | Where-Object { $_ -ge 0xC1 -and $_ -le 0xDA }
  76. if (-not $hasLow -or -not $hasHigh) { throw "decls.n8 lacks one PETSCII letter range" }
  77. $files = @(@{ Name = "source-test"; Path = $link.Program; Type = "prg" })
  78. foreach ($name in $fixtures.Keys) {
  79. $path = Join-Path $out ($name -replace '\.n8$', '.pet')
  80. [IO.File]::WriteAllBytes($path, $fixtures[$name])
  81. $files += @{ Name = $name; Path = $path; Type = "seq" }
  82. }
  83. $disk = Join-Path $out "source-test.d81"
  84. New-Nova128Disk -Path $disk -Files $files
  85. # ------------------------------------------------------------ expectations
  86. function Ok([string]$name) {
  87. $b = $fixtures[$name]; $f = Get-Fletcher $b
  88. return [ordered]@{ name = $name; code = 0; lines = (Get-Lines $b).Count; len = $b.Length;
  89. s1 = $f[0]; s2 = $f[1] }
  90. }
  91. $expected = @(
  92. (Ok "decls.n8"),
  93. (Ok "nofinal.n8"),
  94. (Ok "edge.n8"),
  95. [ordered]@{ name = "longln.n8"; code = 3; line = 3; col = 81 },
  96. [ordered]@{ name = "manyln.n8"; code = 4; line = 600; col = 1 },
  97. [ordered]@{ name = "big.n8"; code = 2; line = 500; col = 38 },
  98. [ordered]@{ name = "missing.n8"; code = 1; dos = 62 },
  99. (Ok "decls.n8")
  100. )
  101. $expected | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath (Join-Path $out "expected.json")
  102. Write-Host ("built {0}" -f $link.Program)
  103. Write-Host ("disk {0} ({1} fixtures)" -f $disk, $fixtures.Count)

Powered by TurnKey Linux.