mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-14 09:52:23 -07:00
Merge remote-tracking branch 'offical/master'
This commit is contained in:
@@ -10,7 +10,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build, Check, and Test
|
||||
timeout-minutes: 25
|
||||
timeout-minutes: 15
|
||||
uses: vmactions/netbsd-vm@v1
|
||||
with:
|
||||
release: "10.0"
|
||||
@@ -24,16 +24,17 @@ jobs:
|
||||
/usr/sbin/pkg_add https://github.com/andreas-jonsson/llvm17-netbsd-bin/releases/download/pkgsrc-current/llvm-17.0.6.tgz
|
||||
/usr/sbin/pkg_add https://github.com/andreas-jonsson/llvm17-netbsd-bin/releases/download/pkgsrc-current/clang-17.0.6.tgz
|
||||
ln -s /usr/pkg/bin/python3.11 /usr/bin/python3
|
||||
ln -s /usr/pkg/bin/bash /bin/bash
|
||||
run: |
|
||||
git config --global --add safe.directory $(pwd)
|
||||
gmake release
|
||||
./odin version
|
||||
./odin report
|
||||
./odin check examples/all -vet -strict-style -target:netbsd_amd64
|
||||
./odin check examples/all -vet -strict-style -target:netbsd_arm64
|
||||
(cd tests/core; gmake all_bsd)
|
||||
(cd tests/internal; gmake all_bsd)
|
||||
(cd tests/issues; ./run.sh)
|
||||
(cd tests/benchmark; gmake all)
|
||||
build_linux:
|
||||
name: Ubuntu Build, Check, and Test
|
||||
runs-on: ubuntu-latest
|
||||
@@ -53,6 +54,12 @@ jobs:
|
||||
- name: Odin report
|
||||
run: ./odin report
|
||||
timeout-minutes: 1
|
||||
- name: Compile needed Vendor
|
||||
run: |
|
||||
make -C $(./odin root)/vendor/stb/src
|
||||
make -C $(./odin root)/vendor/cgltf/src
|
||||
make -C $(./odin root)/vendor/miniaudio/src
|
||||
timeout-minutes: 10
|
||||
- name: Odin check
|
||||
run: ./odin check examples/demo -vet
|
||||
timeout-minutes: 10
|
||||
@@ -80,6 +87,11 @@ jobs:
|
||||
cd tests/internal
|
||||
make
|
||||
timeout-minutes: 10
|
||||
- name: Odin core library benchmarks
|
||||
run: |
|
||||
cd tests/benchmark
|
||||
make
|
||||
timeout-minutes: 10
|
||||
- name: Odin check examples/all for Linux i386
|
||||
run: ./odin check examples/all -vet -strict-style -target:linux_i386
|
||||
timeout-minutes: 10
|
||||
@@ -109,6 +121,12 @@ jobs:
|
||||
- name: Odin report
|
||||
run: ./odin report
|
||||
timeout-minutes: 1
|
||||
- name: Compile needed Vendor
|
||||
run: |
|
||||
make -C $(./odin root)/vendor/stb/src
|
||||
make -C $(./odin root)/vendor/cgltf/src
|
||||
make -C $(./odin root)/vendor/miniaudio/src
|
||||
timeout-minutes: 10
|
||||
- name: Odin check
|
||||
run: ./odin check examples/demo -vet
|
||||
timeout-minutes: 10
|
||||
@@ -131,6 +149,11 @@ jobs:
|
||||
cd tests/internal
|
||||
make
|
||||
timeout-minutes: 10
|
||||
- name: Odin core library benchmarks
|
||||
run: |
|
||||
cd tests/benchmark
|
||||
make
|
||||
timeout-minutes: 10
|
||||
build_macOS_arm:
|
||||
name: MacOS ARM Build, Check, and Test
|
||||
runs-on: macos-14 # This is an arm/m1 runner.
|
||||
@@ -148,6 +171,12 @@ jobs:
|
||||
- name: Odin report
|
||||
run: ./odin report
|
||||
timeout-minutes: 1
|
||||
- name: Compile needed Vendor
|
||||
run: |
|
||||
make -C $(./odin root)/vendor/stb/src
|
||||
make -C $(./odin root)/vendor/cgltf/src
|
||||
make -C $(./odin root)/vendor/miniaudio/src
|
||||
timeout-minutes: 10
|
||||
- name: Odin check
|
||||
run: ./odin check examples/demo -vet
|
||||
timeout-minutes: 10
|
||||
@@ -170,6 +199,11 @@ jobs:
|
||||
cd tests/internal
|
||||
make
|
||||
timeout-minutes: 10
|
||||
- name: Odin core library benchmarks
|
||||
run: |
|
||||
cd tests/benchmark
|
||||
make
|
||||
timeout-minutes: 10
|
||||
build_windows:
|
||||
name: Windows Build, Check, and Test
|
||||
runs-on: windows-2022
|
||||
@@ -217,6 +251,13 @@ jobs:
|
||||
cd tests\core
|
||||
call build.bat
|
||||
timeout-minutes: 10
|
||||
- name: Core library benchmarks
|
||||
shell: cmd
|
||||
run: |
|
||||
call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvars64.bat
|
||||
cd tests\benchmark
|
||||
call build.bat
|
||||
timeout-minutes: 10
|
||||
- name: Vendor library tests
|
||||
shell: cmd
|
||||
run: |
|
||||
|
||||
@@ -151,11 +151,11 @@ jobs:
|
||||
with:
|
||||
python-version: '3.8.x'
|
||||
|
||||
- name: Install B2 CLI
|
||||
- name: Install B2 SDK
|
||||
shell: bash
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install --upgrade b2
|
||||
pip install --upgrade b2sdk
|
||||
|
||||
- name: Display Python version
|
||||
run: python -c "import sys; print(sys.version)"
|
||||
@@ -188,24 +188,9 @@ jobs:
|
||||
BUCKET: ${{ secrets.B2_BUCKET }}
|
||||
DAYS_TO_KEEP: ${{ secrets.B2_DAYS_TO_KEEP }}
|
||||
run: |
|
||||
echo Authorizing B2 account
|
||||
b2 authorize-account "$APPID" "$APPKEY"
|
||||
|
||||
echo Uploading artifcates to B2
|
||||
chmod +x ./ci/upload_create_nightly.sh
|
||||
./ci/upload_create_nightly.sh "$BUCKET" windows-amd64 windows_artifacts/
|
||||
./ci/upload_create_nightly.sh "$BUCKET" ubuntu-amd64 ubuntu_artifacts/dist.zip
|
||||
./ci/upload_create_nightly.sh "$BUCKET" macos-amd64 macos_artifacts/dist.zip
|
||||
./ci/upload_create_nightly.sh "$BUCKET" macos-arm64 macos_arm_artifacts/dist.zip
|
||||
|
||||
echo Deleting old artifacts in B2
|
||||
python3 ci/delete_old_binaries.py "$BUCKET" "$DAYS_TO_KEEP"
|
||||
|
||||
echo Creating nightly.json
|
||||
python3 ci/create_nightly_json.py "$BUCKET" > nightly.json
|
||||
|
||||
echo Uploading nightly.json
|
||||
b2 upload-file "$BUCKET" nightly.json nightly.json
|
||||
|
||||
echo Clear B2 account info
|
||||
b2 clear-account
|
||||
python3 ci/nightly.py artifact windows-amd64 windows_artifacts/
|
||||
python3 ci/nightly.py artifact ubuntu-amd64 ubuntu_artifacts/dist.zip
|
||||
python3 ci/nightly.py artifact macos-amd64 macos_artifacts/dist.zip
|
||||
python3 ci/nightly.py artifact macos-arm64 macos_arm_artifacts/dist.zip
|
||||
python3 ci/nightly.py prune
|
||||
python3 ci/nightly.py json
|
||||
@@ -126,3 +126,5 @@ clamp :: proc(value, minimum, maximum: T) -> T ---
|
||||
|
||||
soa_zip :: proc(slices: ...) -> #soa[]Struct ---
|
||||
soa_unzip :: proc(value: $S/#soa[]$E) -> (slices: ...) ---
|
||||
|
||||
unreachable :: proc() -> ! ---
|
||||
|
||||
@@ -470,6 +470,15 @@ Raw_Soa_Pointer :: struct {
|
||||
index: int,
|
||||
}
|
||||
|
||||
Raw_Complex32 :: struct {real, imag: f16}
|
||||
Raw_Complex64 :: struct {real, imag: f32}
|
||||
Raw_Complex128 :: struct {real, imag: f64}
|
||||
Raw_Quaternion64 :: struct {imag, jmag, kmag: f16, real: f16}
|
||||
Raw_Quaternion128 :: struct {imag, jmag, kmag: f32, real: f32}
|
||||
Raw_Quaternion256 :: struct {imag, jmag, kmag: f64, real: f64}
|
||||
Raw_Quaternion64_Vector_Scalar :: struct {vector: [3]f16, scalar: f16}
|
||||
Raw_Quaternion128_Vector_Scalar :: struct {vector: [3]f32, scalar: f32}
|
||||
Raw_Quaternion256_Vector_Scalar :: struct {vector: [3]f64, scalar: f64}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
@@ -157,7 +157,7 @@ __dynamic_map_get // dynamic map calls
|
||||
__dynamic_map_set // dynamic map calls
|
||||
|
||||
|
||||
## Dynamic literals ([dymamic]T and map[K]V) (can be disabled with -no-dynamic-literals)
|
||||
## Dynamic literals ([dynamic]T and map[K]V) (can be disabled with -no-dynamic-literals)
|
||||
|
||||
__dynamic_array_reserve
|
||||
__dynamic_array_append
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import json
|
||||
import datetime
|
||||
import urllib.parse
|
||||
import sys
|
||||
|
||||
def main():
|
||||
files_by_date = {}
|
||||
bucket = sys.argv[1]
|
||||
|
||||
files_lines = execute_cli(f"b2 ls --long {bucket} nightly").split("\n")
|
||||
for x in files_lines:
|
||||
parts = x.split(" ", 1)
|
||||
if parts[0]:
|
||||
json_str = execute_cli(f"b2 get-file-info {parts[0]}")
|
||||
data = json.loads(json_str)
|
||||
name = remove_prefix(data['fileName'], "nightly/")
|
||||
url = f"https://f001.backblazeb2.com/file/{bucket}/nightly/{urllib.parse.quote_plus(name)}"
|
||||
sha1 = data['contentSha1']
|
||||
size = int(data['size'])
|
||||
ts = int(data['fileInfo']['src_last_modified_millis'])
|
||||
date = datetime.datetime.fromtimestamp(ts/1000).strftime('%Y-%m-%d')
|
||||
|
||||
if date not in files_by_date.keys():
|
||||
files_by_date[date] = []
|
||||
|
||||
files_by_date[date].append({
|
||||
'name': name,
|
||||
'url': url,
|
||||
'sha1': sha1,
|
||||
'sizeInBytes': size,
|
||||
})
|
||||
|
||||
now = datetime.datetime.utcnow().isoformat()
|
||||
|
||||
print(json.dumps({
|
||||
'last_updated' : now,
|
||||
'files': files_by_date
|
||||
}, sort_keys=True, indent=4))
|
||||
|
||||
def remove_prefix(text, prefix):
|
||||
return text[text.startswith(prefix) and len(prefix):]
|
||||
|
||||
def execute_cli(command):
|
||||
sb = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
|
||||
return sb.stdout.read().decode("utf-8");
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import json
|
||||
import datetime
|
||||
import urllib.parse
|
||||
import sys
|
||||
|
||||
def main():
|
||||
files_by_date = {}
|
||||
bucket = sys.argv[1]
|
||||
days_to_keep = int(sys.argv[2])
|
||||
print(f"Looking for binaries to delete older than {days_to_keep} days")
|
||||
|
||||
files_lines = execute_cli(f"b2 ls --long --versions {bucket} nightly").split("\n")
|
||||
for x in files_lines:
|
||||
parts = [y for y in x.split(' ') if y]
|
||||
|
||||
if parts and parts[0]:
|
||||
date = datetime.datetime.strptime(parts[2], '%Y-%m-%d').replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
now = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
delta = now - date
|
||||
|
||||
if delta.days > days_to_keep:
|
||||
print(f'Deleting {parts[5]}')
|
||||
execute_cli(f'b2 delete-file-version {parts[0]}')
|
||||
|
||||
|
||||
def execute_cli(command):
|
||||
sb = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
|
||||
return sb.stdout.read().decode("utf-8");
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
import os
|
||||
import sys
|
||||
from zipfile import ZipFile, ZIP_DEFLATED
|
||||
from b2sdk.v2 import InMemoryAccountInfo, B2Api
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
UPLOAD_FOLDER = "nightly/"
|
||||
|
||||
info = InMemoryAccountInfo()
|
||||
b2_api = B2Api(info)
|
||||
application_key_id = os.environ['APPID']
|
||||
application_key = os.environ['APPKEY']
|
||||
bucket_name = os.environ['BUCKET']
|
||||
days_to_keep = os.environ['DAYS_TO_KEEP']
|
||||
|
||||
def auth() -> bool:
|
||||
try:
|
||||
realm = b2_api.account_info.get_realm()
|
||||
return True # Already authenticated
|
||||
except:
|
||||
pass # Not yet authenticated
|
||||
|
||||
err = b2_api.authorize_account("production", application_key_id, application_key)
|
||||
return err == None
|
||||
|
||||
def get_bucket():
|
||||
if not auth(): sys.exit(1)
|
||||
return b2_api.get_bucket_by_name(bucket_name)
|
||||
|
||||
def remove_prefix(text: str, prefix: str) -> str:
|
||||
return text[text.startswith(prefix) and len(prefix):]
|
||||
|
||||
def create_and_upload_artifact_zip(platform: str, artifact: str) -> int:
|
||||
now = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
destination_zip_name = "odin-{}-nightly+{}.zip".format(platform, now.strftime("%Y-%m-%d"))
|
||||
|
||||
source_zip_name = artifact
|
||||
if not artifact.endswith(".zip"):
|
||||
print(f"Creating archive {destination_zip_name} from {artifact} and uploading to {bucket_name}")
|
||||
|
||||
source_zip_name = destination_zip_name
|
||||
with ZipFile(source_zip_name, mode='w', compression=ZIP_DEFLATED, compresslevel=9) as z:
|
||||
for root, directory, filenames in os.walk(artifact):
|
||||
for file in filenames:
|
||||
file_path = os.path.join(root, file)
|
||||
zip_path = os.path.join("dist", os.path.relpath(file_path, artifact))
|
||||
z.write(file_path, zip_path)
|
||||
|
||||
if not os.path.exists(source_zip_name):
|
||||
print(f"Error: Newly created ZIP archive {source_zip_name} not found.")
|
||||
return 1
|
||||
|
||||
print("Uploading {} to {}".format(source_zip_name, UPLOAD_FOLDER + destination_zip_name))
|
||||
bucket = get_bucket()
|
||||
res = bucket.upload_local_file(
|
||||
source_zip_name, # Local file to upload
|
||||
"nightly/" + destination_zip_name, # B2 destination path
|
||||
)
|
||||
return 0
|
||||
|
||||
def prune_artifacts():
|
||||
print(f"Looking for binaries to delete older than {days_to_keep} days")
|
||||
|
||||
bucket = get_bucket()
|
||||
for file, _ in bucket.ls(UPLOAD_FOLDER, latest_only=False):
|
||||
# Timestamp is in milliseconds
|
||||
date = datetime.fromtimestamp(file.upload_timestamp / 1_000.0).replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
now = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
delta = now - date
|
||||
|
||||
if delta.days > int(days_to_keep):
|
||||
print("Deleting {}".format(file.file_name))
|
||||
file.delete()
|
||||
|
||||
return 0
|
||||
|
||||
def update_nightly_json():
|
||||
print(f"Updating nightly.json with files {days_to_keep} days or newer")
|
||||
|
||||
files_by_date = {}
|
||||
|
||||
bucket = get_bucket()
|
||||
|
||||
for file, _ in bucket.ls(UPLOAD_FOLDER, latest_only=True):
|
||||
# Timestamp is in milliseconds
|
||||
date = datetime.fromtimestamp(file.upload_timestamp / 1_000.0).replace(hour=0, minute=0, second=0, microsecond=0).strftime('%Y-%m-%d')
|
||||
name = remove_prefix(file.file_name, UPLOAD_FOLDER)
|
||||
sha1 = file.content_sha1
|
||||
size = file.size
|
||||
url = bucket.get_download_url(file.file_name)
|
||||
|
||||
if date not in files_by_date.keys():
|
||||
files_by_date[date] = []
|
||||
|
||||
files_by_date[date].append({
|
||||
'name': name,
|
||||
'url': url,
|
||||
'sha1': sha1,
|
||||
'sizeInBytes': size,
|
||||
})
|
||||
|
||||
now = datetime.utcnow().isoformat()
|
||||
|
||||
nightly = json.dumps({
|
||||
'last_updated' : now,
|
||||
'files': files_by_date
|
||||
}, sort_keys=True, indent=4, ensure_ascii=False).encode('utf-8')
|
||||
|
||||
res = bucket.upload_bytes(
|
||||
nightly, # JSON bytes
|
||||
"nightly.json", # B2 destination path
|
||||
)
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) == 1:
|
||||
print("Usage: {} <verb> [arguments]".format(sys.argv[0]))
|
||||
print("\tartifact <platform prefix> <artifact path>\n\t\tCreates and uploads a platform artifact zip.")
|
||||
print("\tprune\n\t\tDeletes old artifacts from bucket")
|
||||
print("\tjson\n\t\tUpdate and upload nightly.json")
|
||||
sys.exit(1)
|
||||
else:
|
||||
command = sys.argv[1].lower()
|
||||
if command == "artifact":
|
||||
if len(sys.argv) != 4:
|
||||
print("Usage: {} artifact <platform prefix> <artifact path>".format(sys.argv[0]))
|
||||
print("Error: Expected artifact command to be given platform prefix and artifact path.\n")
|
||||
sys.exit(1)
|
||||
|
||||
res = create_and_upload_artifact_zip(sys.argv[2], sys.argv[3])
|
||||
sys.exit(res)
|
||||
|
||||
elif command == "prune":
|
||||
res = prune_artifacts()
|
||||
sys.exit(res)
|
||||
|
||||
elif command == "json":
|
||||
res = update_nightly_json()
|
||||
sys.exit(res)
|
||||
@@ -1,25 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
bucket=$1
|
||||
platform=$2
|
||||
artifact=$3
|
||||
|
||||
now=$(date +'%Y-%m-%d')
|
||||
filename="odin-$platform-nightly+$now.zip"
|
||||
|
||||
echo "Creating archive $filename from $artifact and uploading to $bucket"
|
||||
|
||||
# If this is already zipped up (done before artifact upload to keep permissions in tact), just move it.
|
||||
if [ "${artifact: -4}" == ".zip" ]
|
||||
then
|
||||
echo "Artifact already a zip"
|
||||
mkdir -p "output"
|
||||
mv "$artifact" "output/$filename"
|
||||
else
|
||||
echo "Artifact needs to be zipped"
|
||||
7z a -bd "output/$filename" -r "$artifact"
|
||||
fi
|
||||
|
||||
b2 upload-file --noProgress "$bucket" "output/$filename" "nightly/$filename"
|
||||
@@ -29,12 +29,12 @@ MIN_READ_BUFFER_SIZE :: 16
|
||||
@(private)
|
||||
DEFAULT_MAX_CONSECUTIVE_EMPTY_READS :: 128
|
||||
|
||||
reader_init :: proc(b: ^Reader, rd: io.Reader, size: int = DEFAULT_BUF_SIZE, allocator := context.allocator) {
|
||||
reader_init :: proc(b: ^Reader, rd: io.Reader, size: int = DEFAULT_BUF_SIZE, allocator := context.allocator, loc := #caller_location) {
|
||||
size := size
|
||||
size = max(size, MIN_READ_BUFFER_SIZE)
|
||||
reader_reset(b, rd)
|
||||
b.buf_allocator = allocator
|
||||
b.buf = make([]byte, size, allocator)
|
||||
b.buf = make([]byte, size, allocator, loc)
|
||||
}
|
||||
|
||||
reader_init_with_buf :: proc(b: ^Reader, rd: io.Reader, buf: []byte) {
|
||||
|
||||
+35
-35
@@ -27,19 +27,19 @@ Read_Op :: enum i8 {
|
||||
}
|
||||
|
||||
|
||||
buffer_init :: proc(b: ^Buffer, buf: []byte) {
|
||||
resize(&b.buf, len(buf))
|
||||
buffer_init :: proc(b: ^Buffer, buf: []byte, loc := #caller_location) {
|
||||
resize(&b.buf, len(buf), loc=loc)
|
||||
copy(b.buf[:], buf)
|
||||
}
|
||||
|
||||
buffer_init_string :: proc(b: ^Buffer, s: string) {
|
||||
resize(&b.buf, len(s))
|
||||
buffer_init_string :: proc(b: ^Buffer, s: string, loc := #caller_location) {
|
||||
resize(&b.buf, len(s), loc=loc)
|
||||
copy(b.buf[:], s)
|
||||
}
|
||||
|
||||
buffer_init_allocator :: proc(b: ^Buffer, len, cap: int, allocator := context.allocator) {
|
||||
buffer_init_allocator :: proc(b: ^Buffer, len, cap: int, allocator := context.allocator, loc := #caller_location) {
|
||||
if b.buf == nil {
|
||||
b.buf = make([dynamic]byte, len, cap, allocator)
|
||||
b.buf = make([dynamic]byte, len, cap, allocator, loc)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -96,28 +96,28 @@ buffer_truncate :: proc(b: ^Buffer, n: int) {
|
||||
}
|
||||
|
||||
@(private)
|
||||
_buffer_try_grow :: proc(b: ^Buffer, n: int) -> (int, bool) {
|
||||
_buffer_try_grow :: proc(b: ^Buffer, n: int, loc := #caller_location) -> (int, bool) {
|
||||
if l := len(b.buf); n <= cap(b.buf)-l {
|
||||
resize(&b.buf, l+n)
|
||||
resize(&b.buf, l+n, loc=loc)
|
||||
return l, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
@(private)
|
||||
_buffer_grow :: proc(b: ^Buffer, n: int) -> int {
|
||||
_buffer_grow :: proc(b: ^Buffer, n: int, loc := #caller_location) -> int {
|
||||
m := buffer_length(b)
|
||||
if m == 0 && b.off != 0 {
|
||||
buffer_reset(b)
|
||||
}
|
||||
if i, ok := _buffer_try_grow(b, n); ok {
|
||||
if i, ok := _buffer_try_grow(b, n, loc=loc); ok {
|
||||
return i
|
||||
}
|
||||
|
||||
if b.buf == nil && n <= SMALL_BUFFER_SIZE {
|
||||
// Fixes #2756 by preserving allocator if already set on Buffer via init_buffer_allocator
|
||||
reserve(&b.buf, SMALL_BUFFER_SIZE)
|
||||
resize(&b.buf, n)
|
||||
reserve(&b.buf, SMALL_BUFFER_SIZE, loc=loc)
|
||||
resize(&b.buf, n, loc=loc)
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -127,31 +127,31 @@ _buffer_grow :: proc(b: ^Buffer, n: int) -> int {
|
||||
} else if c > max(int) - c - n {
|
||||
panic("bytes.Buffer: too large")
|
||||
} else {
|
||||
resize(&b.buf, 2*c + n)
|
||||
resize(&b.buf, 2*c + n, loc=loc)
|
||||
copy(b.buf[:], b.buf[b.off:])
|
||||
}
|
||||
b.off = 0
|
||||
resize(&b.buf, m+n)
|
||||
resize(&b.buf, m+n, loc=loc)
|
||||
return m
|
||||
}
|
||||
|
||||
buffer_grow :: proc(b: ^Buffer, n: int) {
|
||||
buffer_grow :: proc(b: ^Buffer, n: int, loc := #caller_location) {
|
||||
if n < 0 {
|
||||
panic("bytes.buffer_grow: negative count")
|
||||
}
|
||||
m := _buffer_grow(b, n)
|
||||
resize(&b.buf, m)
|
||||
m := _buffer_grow(b, n, loc=loc)
|
||||
resize(&b.buf, m, loc=loc)
|
||||
}
|
||||
|
||||
buffer_write_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.Error) {
|
||||
buffer_write_at :: proc(b: ^Buffer, p: []byte, offset: int, loc := #caller_location) -> (n: int, err: io.Error) {
|
||||
b.last_read = .Invalid
|
||||
if offset < 0 {
|
||||
err = .Invalid_Offset
|
||||
return
|
||||
}
|
||||
_, ok := _buffer_try_grow(b, offset+len(p))
|
||||
_, ok := _buffer_try_grow(b, offset+len(p), loc=loc)
|
||||
if !ok {
|
||||
_ = _buffer_grow(b, offset+len(p))
|
||||
_ = _buffer_grow(b, offset+len(p), loc=loc)
|
||||
}
|
||||
if len(b.buf) <= offset {
|
||||
return 0, .Short_Write
|
||||
@@ -160,47 +160,47 @@ buffer_write_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.
|
||||
}
|
||||
|
||||
|
||||
buffer_write :: proc(b: ^Buffer, p: []byte) -> (n: int, err: io.Error) {
|
||||
buffer_write :: proc(b: ^Buffer, p: []byte, loc := #caller_location) -> (n: int, err: io.Error) {
|
||||
b.last_read = .Invalid
|
||||
m, ok := _buffer_try_grow(b, len(p))
|
||||
m, ok := _buffer_try_grow(b, len(p), loc=loc)
|
||||
if !ok {
|
||||
m = _buffer_grow(b, len(p))
|
||||
m = _buffer_grow(b, len(p), loc=loc)
|
||||
}
|
||||
return copy(b.buf[m:], p), nil
|
||||
}
|
||||
|
||||
buffer_write_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int) -> (n: int, err: io.Error) {
|
||||
return buffer_write(b, ([^]byte)(ptr)[:size])
|
||||
buffer_write_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int, loc := #caller_location) -> (n: int, err: io.Error) {
|
||||
return buffer_write(b, ([^]byte)(ptr)[:size], loc=loc)
|
||||
}
|
||||
|
||||
buffer_write_string :: proc(b: ^Buffer, s: string) -> (n: int, err: io.Error) {
|
||||
buffer_write_string :: proc(b: ^Buffer, s: string, loc := #caller_location) -> (n: int, err: io.Error) {
|
||||
b.last_read = .Invalid
|
||||
m, ok := _buffer_try_grow(b, len(s))
|
||||
m, ok := _buffer_try_grow(b, len(s), loc=loc)
|
||||
if !ok {
|
||||
m = _buffer_grow(b, len(s))
|
||||
m = _buffer_grow(b, len(s), loc=loc)
|
||||
}
|
||||
return copy(b.buf[m:], s), nil
|
||||
}
|
||||
|
||||
buffer_write_byte :: proc(b: ^Buffer, c: byte) -> io.Error {
|
||||
buffer_write_byte :: proc(b: ^Buffer, c: byte, loc := #caller_location) -> io.Error {
|
||||
b.last_read = .Invalid
|
||||
m, ok := _buffer_try_grow(b, 1)
|
||||
m, ok := _buffer_try_grow(b, 1, loc=loc)
|
||||
if !ok {
|
||||
m = _buffer_grow(b, 1)
|
||||
m = _buffer_grow(b, 1, loc=loc)
|
||||
}
|
||||
b.buf[m] = c
|
||||
return nil
|
||||
}
|
||||
|
||||
buffer_write_rune :: proc(b: ^Buffer, r: rune) -> (n: int, err: io.Error) {
|
||||
buffer_write_rune :: proc(b: ^Buffer, r: rune, loc := #caller_location) -> (n: int, err: io.Error) {
|
||||
if r < utf8.RUNE_SELF {
|
||||
buffer_write_byte(b, byte(r))
|
||||
buffer_write_byte(b, byte(r), loc=loc)
|
||||
return 1, nil
|
||||
}
|
||||
b.last_read = .Invalid
|
||||
m, ok := _buffer_try_grow(b, utf8.UTF_MAX)
|
||||
m, ok := _buffer_try_grow(b, utf8.UTF_MAX, loc=loc)
|
||||
if !ok {
|
||||
m = _buffer_grow(b, utf8.UTF_MAX)
|
||||
m = _buffer_grow(b, utf8.UTF_MAX, loc=loc)
|
||||
}
|
||||
res: [4]byte
|
||||
res, n = utf8.encode_rune(r)
|
||||
|
||||
@@ -34,7 +34,7 @@ when ODIN_OS == .Windows {
|
||||
SIGTERM :: 15
|
||||
}
|
||||
|
||||
when ODIN_OS == .Linux || ODIN_OS == .FreeBSD {
|
||||
when ODIN_OS == .Linux || ODIN_OS == .FreeBSD || ODIN_OS == .Haiku || ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD {
|
||||
SIG_ERR :: rawptr(~uintptr(0))
|
||||
SIG_DFL :: rawptr(uintptr(0))
|
||||
SIG_IGN :: rawptr(uintptr(1))
|
||||
|
||||
@@ -102,10 +102,12 @@ when ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD {
|
||||
SEEK_END :: 2
|
||||
|
||||
foreign libc {
|
||||
stderr: ^FILE
|
||||
stdin: ^FILE
|
||||
stdout: ^FILE
|
||||
__sF: [3]FILE
|
||||
}
|
||||
|
||||
stdin: ^FILE = &__sF[0]
|
||||
stdout: ^FILE = &__sF[1]
|
||||
stderr: ^FILE = &__sF[2]
|
||||
}
|
||||
|
||||
when ODIN_OS == .FreeBSD {
|
||||
@@ -127,9 +129,9 @@ when ODIN_OS == .FreeBSD {
|
||||
SEEK_END :: 2
|
||||
|
||||
foreign libc {
|
||||
stderr: ^FILE
|
||||
stdin: ^FILE
|
||||
stdout: ^FILE
|
||||
@(link_name="__stderrp") stderr: ^FILE
|
||||
@(link_name="__stdinp") stdin: ^FILE
|
||||
@(link_name="__stdoutp") stdout: ^FILE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package _aes
|
||||
|
||||
// KEY_SIZE_128 is the AES-128 key size in bytes.
|
||||
KEY_SIZE_128 :: 16
|
||||
// KEY_SIZE_192 is the AES-192 key size in bytes.
|
||||
KEY_SIZE_192 :: 24
|
||||
// KEY_SIZE_256 is the AES-256 key size in bytes.
|
||||
KEY_SIZE_256 :: 32
|
||||
|
||||
// BLOCK_SIZE is the AES block size in bytes.
|
||||
BLOCK_SIZE :: 16
|
||||
|
||||
// ROUNDS_128 is the number of rounds for AES-128.
|
||||
ROUNDS_128 :: 10
|
||||
// ROUNDS_192 is the number of rounds for AES-192.
|
||||
ROUNDS_192 :: 12
|
||||
// ROUNDS_256 is the number of rounds for AES-256.
|
||||
ROUNDS_256 :: 14
|
||||
|
||||
// GHASH_KEY_SIZE is the GHASH key size in bytes.
|
||||
GHASH_KEY_SIZE :: 16
|
||||
// GHASH_BLOCK_SIZE is the GHASH block size in bytes.
|
||||
GHASH_BLOCK_SIZE :: 16
|
||||
// GHASH_TAG_SIZE is the GHASH tag size in bytes.
|
||||
GHASH_TAG_SIZE :: 16
|
||||
|
||||
// RCON is the AES keyschedule round constants.
|
||||
RCON := [10]byte{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36}
|
||||
@@ -0,0 +1,96 @@
|
||||
package aes_ct64
|
||||
|
||||
import "base:intrinsics"
|
||||
import "core:mem"
|
||||
|
||||
STRIDE :: 4
|
||||
|
||||
// Context is a keyed AES (ECB) instance.
|
||||
Context :: struct {
|
||||
_sk_exp: [120]u64,
|
||||
_num_rounds: int,
|
||||
_is_initialized: bool,
|
||||
}
|
||||
|
||||
// init initializes a context for AES with the provided key.
|
||||
init :: proc(ctx: ^Context, key: []byte) {
|
||||
skey: [30]u64 = ---
|
||||
|
||||
ctx._num_rounds = keysched(skey[:], key)
|
||||
skey_expand(ctx._sk_exp[:], skey[:], ctx._num_rounds)
|
||||
ctx._is_initialized = true
|
||||
}
|
||||
|
||||
// encrypt_block sets `dst` to `AES-ECB-Encrypt(src)`.
|
||||
encrypt_block :: proc(ctx: ^Context, dst, src: []byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
q: [8]u64
|
||||
load_blockx1(&q, src)
|
||||
_encrypt(&q, ctx._sk_exp[:], ctx._num_rounds)
|
||||
store_blockx1(dst, &q)
|
||||
}
|
||||
|
||||
// encrypt_block sets `dst` to `AES-ECB-Decrypt(src)`.
|
||||
decrypt_block :: proc(ctx: ^Context, dst, src: []byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
q: [8]u64
|
||||
load_blockx1(&q, src)
|
||||
_decrypt(&q, ctx._sk_exp[:], ctx._num_rounds)
|
||||
store_blockx1(dst, &q)
|
||||
}
|
||||
|
||||
// encrypt_blocks sets `dst` to `AES-ECB-Encrypt(src[0], .. src[n])`.
|
||||
encrypt_blocks :: proc(ctx: ^Context, dst, src: [][]byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
q: [8]u64 = ---
|
||||
src, dst := src, dst
|
||||
|
||||
n := len(src)
|
||||
for n > 4 {
|
||||
load_blocks(&q, src[0:4])
|
||||
_encrypt(&q, ctx._sk_exp[:], ctx._num_rounds)
|
||||
store_blocks(dst[0:4], &q)
|
||||
|
||||
src = src[4:]
|
||||
dst = dst[4:]
|
||||
n -= 4
|
||||
}
|
||||
if n > 0 {
|
||||
load_blocks(&q, src)
|
||||
_encrypt(&q, ctx._sk_exp[:], ctx._num_rounds)
|
||||
store_blocks(dst, &q)
|
||||
}
|
||||
}
|
||||
|
||||
// decrypt_blocks sets dst to `AES-ECB-Decrypt(src[0], .. src[n])`.
|
||||
decrypt_blocks :: proc(ctx: ^Context, dst, src: [][]byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
q: [8]u64 = ---
|
||||
src, dst := src, dst
|
||||
|
||||
n := len(src)
|
||||
for n > 4 {
|
||||
load_blocks(&q, src[0:4])
|
||||
_decrypt(&q, ctx._sk_exp[:], ctx._num_rounds)
|
||||
store_blocks(dst[0:4], &q)
|
||||
|
||||
src = src[4:]
|
||||
dst = dst[4:]
|
||||
n -= 4
|
||||
}
|
||||
if n > 0 {
|
||||
load_blocks(&q, src)
|
||||
_decrypt(&q, ctx._sk_exp[:], ctx._num_rounds)
|
||||
store_blocks(dst, &q)
|
||||
}
|
||||
}
|
||||
|
||||
// reset sanitizes the Context. The Context must be re-initialized to
|
||||
// be used again.
|
||||
reset :: proc(ctx: ^Context) {
|
||||
mem.zero_explicit(ctx, size_of(ctx))
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
// Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS “AS IS” AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package aes_ct64
|
||||
|
||||
import "base:intrinsics"
|
||||
|
||||
// Bitsliced AES for 64-bit general purpose (integer) registers. Each
|
||||
// invocation will process up to 4 blocks at a time. This implementation
|
||||
// is derived from the BearSSL ct64 code, and distributed under a 1-clause
|
||||
// BSD license with permission from the original author.
|
||||
//
|
||||
// WARNING: "hic sunt dracones"
|
||||
//
|
||||
// This package also deliberately exposes enough internals to be able to
|
||||
// function as a replacement for `AESENC` and `AESDEC` from AES-NI, to
|
||||
// allow the implementation of non-AES primitives that use the AES round
|
||||
// function such as AEGIS and Deoxys-II. This should ONLY be done when
|
||||
// implementing something other than AES itself.
|
||||
|
||||
sub_bytes :: proc "contextless" (q: ^[8]u64) {
|
||||
// This S-box implementation is a straightforward translation of
|
||||
// the circuit described by Boyar and Peralta in "A new
|
||||
// combinational logic minimization technique with applications
|
||||
// to cryptology" (https://eprint.iacr.org/2009/191.pdf).
|
||||
//
|
||||
// Note that variables x* (input) and s* (output) are numbered
|
||||
// in "reverse" order (x0 is the high bit, x7 is the low bit).
|
||||
|
||||
x0 := q[7]
|
||||
x1 := q[6]
|
||||
x2 := q[5]
|
||||
x3 := q[4]
|
||||
x4 := q[3]
|
||||
x5 := q[2]
|
||||
x6 := q[1]
|
||||
x7 := q[0]
|
||||
|
||||
// Top linear transformation.
|
||||
y14 := x3 ~ x5
|
||||
y13 := x0 ~ x6
|
||||
y9 := x0 ~ x3
|
||||
y8 := x0 ~ x5
|
||||
t0 := x1 ~ x2
|
||||
y1 := t0 ~ x7
|
||||
y4 := y1 ~ x3
|
||||
y12 := y13 ~ y14
|
||||
y2 := y1 ~ x0
|
||||
y5 := y1 ~ x6
|
||||
y3 := y5 ~ y8
|
||||
t1 := x4 ~ y12
|
||||
y15 := t1 ~ x5
|
||||
y20 := t1 ~ x1
|
||||
y6 := y15 ~ x7
|
||||
y10 := y15 ~ t0
|
||||
y11 := y20 ~ y9
|
||||
y7 := x7 ~ y11
|
||||
y17 := y10 ~ y11
|
||||
y19 := y10 ~ y8
|
||||
y16 := t0 ~ y11
|
||||
y21 := y13 ~ y16
|
||||
y18 := x0 ~ y16
|
||||
|
||||
// Non-linear section.
|
||||
t2 := y12 & y15
|
||||
t3 := y3 & y6
|
||||
t4 := t3 ~ t2
|
||||
t5 := y4 & x7
|
||||
t6 := t5 ~ t2
|
||||
t7 := y13 & y16
|
||||
t8 := y5 & y1
|
||||
t9 := t8 ~ t7
|
||||
t10 := y2 & y7
|
||||
t11 := t10 ~ t7
|
||||
t12 := y9 & y11
|
||||
t13 := y14 & y17
|
||||
t14 := t13 ~ t12
|
||||
t15 := y8 & y10
|
||||
t16 := t15 ~ t12
|
||||
t17 := t4 ~ t14
|
||||
t18 := t6 ~ t16
|
||||
t19 := t9 ~ t14
|
||||
t20 := t11 ~ t16
|
||||
t21 := t17 ~ y20
|
||||
t22 := t18 ~ y19
|
||||
t23 := t19 ~ y21
|
||||
t24 := t20 ~ y18
|
||||
|
||||
t25 := t21 ~ t22
|
||||
t26 := t21 & t23
|
||||
t27 := t24 ~ t26
|
||||
t28 := t25 & t27
|
||||
t29 := t28 ~ t22
|
||||
t30 := t23 ~ t24
|
||||
t31 := t22 ~ t26
|
||||
t32 := t31 & t30
|
||||
t33 := t32 ~ t24
|
||||
t34 := t23 ~ t33
|
||||
t35 := t27 ~ t33
|
||||
t36 := t24 & t35
|
||||
t37 := t36 ~ t34
|
||||
t38 := t27 ~ t36
|
||||
t39 := t29 & t38
|
||||
t40 := t25 ~ t39
|
||||
|
||||
t41 := t40 ~ t37
|
||||
t42 := t29 ~ t33
|
||||
t43 := t29 ~ t40
|
||||
t44 := t33 ~ t37
|
||||
t45 := t42 ~ t41
|
||||
z0 := t44 & y15
|
||||
z1 := t37 & y6
|
||||
z2 := t33 & x7
|
||||
z3 := t43 & y16
|
||||
z4 := t40 & y1
|
||||
z5 := t29 & y7
|
||||
z6 := t42 & y11
|
||||
z7 := t45 & y17
|
||||
z8 := t41 & y10
|
||||
z9 := t44 & y12
|
||||
z10 := t37 & y3
|
||||
z11 := t33 & y4
|
||||
z12 := t43 & y13
|
||||
z13 := t40 & y5
|
||||
z14 := t29 & y2
|
||||
z15 := t42 & y9
|
||||
z16 := t45 & y14
|
||||
z17 := t41 & y8
|
||||
|
||||
// Bottom linear transformation.
|
||||
t46 := z15 ~ z16
|
||||
t47 := z10 ~ z11
|
||||
t48 := z5 ~ z13
|
||||
t49 := z9 ~ z10
|
||||
t50 := z2 ~ z12
|
||||
t51 := z2 ~ z5
|
||||
t52 := z7 ~ z8
|
||||
t53 := z0 ~ z3
|
||||
t54 := z6 ~ z7
|
||||
t55 := z16 ~ z17
|
||||
t56 := z12 ~ t48
|
||||
t57 := t50 ~ t53
|
||||
t58 := z4 ~ t46
|
||||
t59 := z3 ~ t54
|
||||
t60 := t46 ~ t57
|
||||
t61 := z14 ~ t57
|
||||
t62 := t52 ~ t58
|
||||
t63 := t49 ~ t58
|
||||
t64 := z4 ~ t59
|
||||
t65 := t61 ~ t62
|
||||
t66 := z1 ~ t63
|
||||
s0 := t59 ~ t63
|
||||
s6 := t56 ~ ~t62
|
||||
s7 := t48 ~ ~t60
|
||||
t67 := t64 ~ t65
|
||||
s3 := t53 ~ t66
|
||||
s4 := t51 ~ t66
|
||||
s5 := t47 ~ t65
|
||||
s1 := t64 ~ ~s3
|
||||
s2 := t55 ~ ~t67
|
||||
|
||||
q[7] = s0
|
||||
q[6] = s1
|
||||
q[5] = s2
|
||||
q[4] = s3
|
||||
q[3] = s4
|
||||
q[2] = s5
|
||||
q[1] = s6
|
||||
q[0] = s7
|
||||
}
|
||||
|
||||
orthogonalize :: proc "contextless" (q: ^[8]u64) {
|
||||
CL2 :: 0x5555555555555555
|
||||
CH2 :: 0xAAAAAAAAAAAAAAAA
|
||||
q[0], q[1] = (q[0] & CL2) | ((q[1] & CL2) << 1), ((q[0] & CH2) >> 1) | (q[1] & CH2)
|
||||
q[2], q[3] = (q[2] & CL2) | ((q[3] & CL2) << 1), ((q[2] & CH2) >> 1) | (q[3] & CH2)
|
||||
q[4], q[5] = (q[4] & CL2) | ((q[5] & CL2) << 1), ((q[4] & CH2) >> 1) | (q[5] & CH2)
|
||||
q[6], q[7] = (q[6] & CL2) | ((q[7] & CL2) << 1), ((q[6] & CH2) >> 1) | (q[7] & CH2)
|
||||
|
||||
CL4 :: 0x3333333333333333
|
||||
CH4 :: 0xCCCCCCCCCCCCCCCC
|
||||
q[0], q[2] = (q[0] & CL4) | ((q[2] & CL4) << 2), ((q[0] & CH4) >> 2) | (q[2] & CH4)
|
||||
q[1], q[3] = (q[1] & CL4) | ((q[3] & CL4) << 2), ((q[1] & CH4) >> 2) | (q[3] & CH4)
|
||||
q[4], q[6] = (q[4] & CL4) | ((q[6] & CL4) << 2), ((q[4] & CH4) >> 2) | (q[6] & CH4)
|
||||
q[5], q[7] = (q[5] & CL4) | ((q[7] & CL4) << 2), ((q[5] & CH4) >> 2) | (q[7] & CH4)
|
||||
|
||||
CL8 :: 0x0F0F0F0F0F0F0F0F
|
||||
CH8 :: 0xF0F0F0F0F0F0F0F0
|
||||
q[0], q[4] = (q[0] & CL8) | ((q[4] & CL8) << 4), ((q[0] & CH8) >> 4) | (q[4] & CH8)
|
||||
q[1], q[5] = (q[1] & CL8) | ((q[5] & CL8) << 4), ((q[1] & CH8) >> 4) | (q[5] & CH8)
|
||||
q[2], q[6] = (q[2] & CL8) | ((q[6] & CL8) << 4), ((q[2] & CH8) >> 4) | (q[6] & CH8)
|
||||
q[3], q[7] = (q[3] & CL8) | ((q[7] & CL8) << 4), ((q[3] & CH8) >> 4) | (q[7] & CH8)
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
interleave_in :: proc "contextless" (w: []u32) -> (q0, q1: u64) #no_bounds_check {
|
||||
if len(w) < 4 {
|
||||
intrinsics.trap()
|
||||
}
|
||||
x0, x1, x2, x3 := u64(w[0]), u64(w[1]), u64(w[2]), u64(w[3])
|
||||
x0 |= (x0 << 16)
|
||||
x1 |= (x1 << 16)
|
||||
x2 |= (x2 << 16)
|
||||
x3 |= (x3 << 16)
|
||||
x0 &= 0x0000FFFF0000FFFF
|
||||
x1 &= 0x0000FFFF0000FFFF
|
||||
x2 &= 0x0000FFFF0000FFFF
|
||||
x3 &= 0x0000FFFF0000FFFF
|
||||
x0 |= (x0 << 8)
|
||||
x1 |= (x1 << 8)
|
||||
x2 |= (x2 << 8)
|
||||
x3 |= (x3 << 8)
|
||||
x0 &= 0x00FF00FF00FF00FF
|
||||
x1 &= 0x00FF00FF00FF00FF
|
||||
x2 &= 0x00FF00FF00FF00FF
|
||||
x3 &= 0x00FF00FF00FF00FF
|
||||
q0 = x0 | (x2 << 8)
|
||||
q1 = x1 | (x3 << 8)
|
||||
return
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
interleave_out :: proc "contextless" (q0, q1: u64) -> (w0, w1, w2, w3: u32) {
|
||||
x0 := q0 & 0x00FF00FF00FF00FF
|
||||
x1 := q1 & 0x00FF00FF00FF00FF
|
||||
x2 := (q0 >> 8) & 0x00FF00FF00FF00FF
|
||||
x3 := (q1 >> 8) & 0x00FF00FF00FF00FF
|
||||
x0 |= (x0 >> 8)
|
||||
x1 |= (x1 >> 8)
|
||||
x2 |= (x2 >> 8)
|
||||
x3 |= (x3 >> 8)
|
||||
x0 &= 0x0000FFFF0000FFFF
|
||||
x1 &= 0x0000FFFF0000FFFF
|
||||
x2 &= 0x0000FFFF0000FFFF
|
||||
x3 &= 0x0000FFFF0000FFFF
|
||||
w0 = u32(x0) | u32(x0 >> 16)
|
||||
w1 = u32(x1) | u32(x1 >> 16)
|
||||
w2 = u32(x2) | u32(x2 >> 16)
|
||||
w3 = u32(x3) | u32(x3 >> 16)
|
||||
return
|
||||
}
|
||||
|
||||
@(private)
|
||||
rotr32 :: #force_inline proc "contextless" (x: u64) -> u64 {
|
||||
return (x << 32) | (x >> 32)
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
// Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS “AS IS” AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package aes_ct64
|
||||
|
||||
import "base:intrinsics"
|
||||
|
||||
inv_sub_bytes :: proc "contextless" (q: ^[8]u64) {
|
||||
// AES S-box is:
|
||||
// S(x) = A(I(x)) ^ 0x63
|
||||
// where I() is inversion in GF(256), and A() is a linear
|
||||
// transform (0 is formally defined to be its own inverse).
|
||||
// Since inversion is an involution, the inverse S-box can be
|
||||
// computed from the S-box as:
|
||||
// iS(x) = B(S(B(x ^ 0x63)) ^ 0x63)
|
||||
// where B() is the inverse of A(). Indeed, for any y in GF(256):
|
||||
// iS(S(y)) = B(A(I(B(A(I(y)) ^ 0x63 ^ 0x63))) ^ 0x63 ^ 0x63) = y
|
||||
//
|
||||
// Note: we reuse the implementation of the forward S-box,
|
||||
// instead of duplicating it here, so that total code size is
|
||||
// lower. By merging the B() transforms into the S-box circuit
|
||||
// we could make faster CBC decryption, but CBC decryption is
|
||||
// already quite faster than CBC encryption because we can
|
||||
// process four blocks in parallel.
|
||||
|
||||
q0 := ~q[0]
|
||||
q1 := ~q[1]
|
||||
q2 := q[2]
|
||||
q3 := q[3]
|
||||
q4 := q[4]
|
||||
q5 := ~q[5]
|
||||
q6 := ~q[6]
|
||||
q7 := q[7]
|
||||
q[7] = q1 ~ q4 ~ q6
|
||||
q[6] = q0 ~ q3 ~ q5
|
||||
q[5] = q7 ~ q2 ~ q4
|
||||
q[4] = q6 ~ q1 ~ q3
|
||||
q[3] = q5 ~ q0 ~ q2
|
||||
q[2] = q4 ~ q7 ~ q1
|
||||
q[1] = q3 ~ q6 ~ q0
|
||||
q[0] = q2 ~ q5 ~ q7
|
||||
|
||||
sub_bytes(q)
|
||||
|
||||
q0 = ~q[0]
|
||||
q1 = ~q[1]
|
||||
q2 = q[2]
|
||||
q3 = q[3]
|
||||
q4 = q[4]
|
||||
q5 = ~q[5]
|
||||
q6 = ~q[6]
|
||||
q7 = q[7]
|
||||
q[7] = q1 ~ q4 ~ q6
|
||||
q[6] = q0 ~ q3 ~ q5
|
||||
q[5] = q7 ~ q2 ~ q4
|
||||
q[4] = q6 ~ q1 ~ q3
|
||||
q[3] = q5 ~ q0 ~ q2
|
||||
q[2] = q4 ~ q7 ~ q1
|
||||
q[1] = q3 ~ q6 ~ q0
|
||||
q[0] = q2 ~ q5 ~ q7
|
||||
}
|
||||
|
||||
inv_shift_rows :: proc "contextless" (q: ^[8]u64) {
|
||||
for x, i in q {
|
||||
q[i] =
|
||||
(x & 0x000000000000FFFF) |
|
||||
((x & 0x000000000FFF0000) << 4) |
|
||||
((x & 0x00000000F0000000) >> 12) |
|
||||
((x & 0x000000FF00000000) << 8) |
|
||||
((x & 0x0000FF0000000000) >> 8) |
|
||||
((x & 0x000F000000000000) << 12) |
|
||||
((x & 0xFFF0000000000000) >> 4)
|
||||
}
|
||||
}
|
||||
|
||||
inv_mix_columns :: proc "contextless" (q: ^[8]u64) {
|
||||
q0 := q[0]
|
||||
q1 := q[1]
|
||||
q2 := q[2]
|
||||
q3 := q[3]
|
||||
q4 := q[4]
|
||||
q5 := q[5]
|
||||
q6 := q[6]
|
||||
q7 := q[7]
|
||||
r0 := (q0 >> 16) | (q0 << 48)
|
||||
r1 := (q1 >> 16) | (q1 << 48)
|
||||
r2 := (q2 >> 16) | (q2 << 48)
|
||||
r3 := (q3 >> 16) | (q3 << 48)
|
||||
r4 := (q4 >> 16) | (q4 << 48)
|
||||
r5 := (q5 >> 16) | (q5 << 48)
|
||||
r6 := (q6 >> 16) | (q6 << 48)
|
||||
r7 := (q7 >> 16) | (q7 << 48)
|
||||
|
||||
q[0] = q5 ~ q6 ~ q7 ~ r0 ~ r5 ~ r7 ~ rotr32(q0 ~ q5 ~ q6 ~ r0 ~ r5)
|
||||
q[1] = q0 ~ q5 ~ r0 ~ r1 ~ r5 ~ r6 ~ r7 ~ rotr32(q1 ~ q5 ~ q7 ~ r1 ~ r5 ~ r6)
|
||||
q[2] = q0 ~ q1 ~ q6 ~ r1 ~ r2 ~ r6 ~ r7 ~ rotr32(q0 ~ q2 ~ q6 ~ r2 ~ r6 ~ r7)
|
||||
q[3] = q0 ~ q1 ~ q2 ~ q5 ~ q6 ~ r0 ~ r2 ~ r3 ~ r5 ~ rotr32(q0 ~ q1 ~ q3 ~ q5 ~ q6 ~ q7 ~ r0 ~ r3 ~ r5 ~ r7)
|
||||
q[4] = q1 ~ q2 ~ q3 ~ q5 ~ r1 ~ r3 ~ r4 ~ r5 ~ r6 ~ r7 ~ rotr32(q1 ~ q2 ~ q4 ~ q5 ~ q7 ~ r1 ~ r4 ~ r5 ~ r6)
|
||||
q[5] = q2 ~ q3 ~ q4 ~ q6 ~ r2 ~ r4 ~ r5 ~ r6 ~ r7 ~ rotr32(q2 ~ q3 ~ q5 ~ q6 ~ r2 ~ r5 ~ r6 ~ r7)
|
||||
q[6] = q3 ~ q4 ~ q5 ~ q7 ~ r3 ~ r5 ~ r6 ~ r7 ~ rotr32(q3 ~ q4 ~ q6 ~ q7 ~ r3 ~ r6 ~ r7)
|
||||
q[7] = q4 ~ q5 ~ q6 ~ r4 ~ r6 ~ r7 ~ rotr32(q4 ~ q5 ~ q7 ~ r4 ~ r7)
|
||||
}
|
||||
|
||||
@(private)
|
||||
_decrypt :: proc "contextless" (q: ^[8]u64, skey: []u64, num_rounds: int) {
|
||||
add_round_key(q, skey[num_rounds << 3:])
|
||||
for u := num_rounds - 1; u > 0; u -= 1 {
|
||||
inv_shift_rows(q)
|
||||
inv_sub_bytes(q)
|
||||
add_round_key(q, skey[u << 3:])
|
||||
inv_mix_columns(q)
|
||||
}
|
||||
inv_shift_rows(q)
|
||||
inv_sub_bytes(q)
|
||||
add_round_key(q, skey)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS “AS IS” AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package aes_ct64
|
||||
|
||||
import "base:intrinsics"
|
||||
|
||||
add_round_key :: proc "contextless" (q: ^[8]u64, sk: []u64) #no_bounds_check {
|
||||
if len(sk) < 8 {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
q[0] ~= sk[0]
|
||||
q[1] ~= sk[1]
|
||||
q[2] ~= sk[2]
|
||||
q[3] ~= sk[3]
|
||||
q[4] ~= sk[4]
|
||||
q[5] ~= sk[5]
|
||||
q[6] ~= sk[6]
|
||||
q[7] ~= sk[7]
|
||||
}
|
||||
|
||||
shift_rows :: proc "contextless" (q: ^[8]u64) {
|
||||
for x, i in q {
|
||||
q[i] =
|
||||
(x & 0x000000000000FFFF) |
|
||||
((x & 0x00000000FFF00000) >> 4) |
|
||||
((x & 0x00000000000F0000) << 12) |
|
||||
((x & 0x0000FF0000000000) >> 8) |
|
||||
((x & 0x000000FF00000000) << 8) |
|
||||
((x & 0xF000000000000000) >> 12) |
|
||||
((x & 0x0FFF000000000000) << 4)
|
||||
}
|
||||
}
|
||||
|
||||
mix_columns :: proc "contextless" (q: ^[8]u64) {
|
||||
q0 := q[0]
|
||||
q1 := q[1]
|
||||
q2 := q[2]
|
||||
q3 := q[3]
|
||||
q4 := q[4]
|
||||
q5 := q[5]
|
||||
q6 := q[6]
|
||||
q7 := q[7]
|
||||
r0 := (q0 >> 16) | (q0 << 48)
|
||||
r1 := (q1 >> 16) | (q1 << 48)
|
||||
r2 := (q2 >> 16) | (q2 << 48)
|
||||
r3 := (q3 >> 16) | (q3 << 48)
|
||||
r4 := (q4 >> 16) | (q4 << 48)
|
||||
r5 := (q5 >> 16) | (q5 << 48)
|
||||
r6 := (q6 >> 16) | (q6 << 48)
|
||||
r7 := (q7 >> 16) | (q7 << 48)
|
||||
|
||||
q[0] = q7 ~ r7 ~ r0 ~ rotr32(q0 ~ r0)
|
||||
q[1] = q0 ~ r0 ~ q7 ~ r7 ~ r1 ~ rotr32(q1 ~ r1)
|
||||
q[2] = q1 ~ r1 ~ r2 ~ rotr32(q2 ~ r2)
|
||||
q[3] = q2 ~ r2 ~ q7 ~ r7 ~ r3 ~ rotr32(q3 ~ r3)
|
||||
q[4] = q3 ~ r3 ~ q7 ~ r7 ~ r4 ~ rotr32(q4 ~ r4)
|
||||
q[5] = q4 ~ r4 ~ r5 ~ rotr32(q5 ~ r5)
|
||||
q[6] = q5 ~ r5 ~ r6 ~ rotr32(q6 ~ r6)
|
||||
q[7] = q6 ~ r6 ~ r7 ~ rotr32(q7 ~ r7)
|
||||
}
|
||||
|
||||
@(private)
|
||||
_encrypt :: proc "contextless" (q: ^[8]u64, skey: []u64, num_rounds: int) {
|
||||
add_round_key(q, skey)
|
||||
for u in 1 ..< num_rounds {
|
||||
sub_bytes(q)
|
||||
shift_rows(q)
|
||||
mix_columns(q)
|
||||
add_round_key(q, skey[u << 3:])
|
||||
}
|
||||
sub_bytes(q)
|
||||
shift_rows(q)
|
||||
add_round_key(q, skey[num_rounds << 3:])
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
// Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS “AS IS” AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package aes_ct64
|
||||
|
||||
import "base:intrinsics"
|
||||
import "core:crypto/_aes"
|
||||
import "core:encoding/endian"
|
||||
import "core:mem"
|
||||
|
||||
@(private, require_results)
|
||||
sub_word :: proc "contextless" (x: u32) -> u32 {
|
||||
q := [8]u64{u64(x), 0, 0, 0, 0, 0, 0, 0}
|
||||
|
||||
orthogonalize(&q)
|
||||
sub_bytes(&q)
|
||||
orthogonalize(&q)
|
||||
ret := u32(q[0])
|
||||
|
||||
mem.zero_explicit(&q[0], size_of(u64))
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
@(private, require_results)
|
||||
keysched :: proc(comp_skey: []u64, key: []byte) -> int {
|
||||
num_rounds, key_len := 0, len(key)
|
||||
switch key_len {
|
||||
case _aes.KEY_SIZE_128:
|
||||
num_rounds = _aes.ROUNDS_128
|
||||
case _aes.KEY_SIZE_192:
|
||||
num_rounds = _aes.ROUNDS_192
|
||||
case _aes.KEY_SIZE_256:
|
||||
num_rounds = _aes.ROUNDS_256
|
||||
case:
|
||||
panic("crypto/aes: invalid AES key size")
|
||||
}
|
||||
|
||||
skey: [60]u32 = ---
|
||||
nk, nkf := key_len >> 2, (num_rounds + 1) << 2
|
||||
for i in 0 ..< nk {
|
||||
skey[i] = endian.unchecked_get_u32le(key[i << 2:])
|
||||
}
|
||||
tmp := skey[(key_len >> 2) - 1]
|
||||
for i, j, k := nk, 0, 0; i < nkf; i += 1 {
|
||||
if j == 0 {
|
||||
tmp = (tmp << 24) | (tmp >> 8)
|
||||
tmp = sub_word(tmp) ~ u32(_aes.RCON[k])
|
||||
} else if nk > 6 && j == 4 {
|
||||
tmp = sub_word(tmp)
|
||||
}
|
||||
tmp ~= skey[i - nk]
|
||||
skey[i] = tmp
|
||||
if j += 1; j == nk {
|
||||
j = 0
|
||||
k += 1
|
||||
}
|
||||
}
|
||||
|
||||
q: [8]u64 = ---
|
||||
for i, j := 0, 0; i < nkf; i, j = i + 4, j + 2 {
|
||||
q[0], q[4] = interleave_in(skey[i:])
|
||||
q[1] = q[0]
|
||||
q[2] = q[0]
|
||||
q[3] = q[0]
|
||||
q[5] = q[4]
|
||||
q[6] = q[4]
|
||||
q[7] = q[4]
|
||||
orthogonalize(&q)
|
||||
comp_skey[j + 0] =
|
||||
(q[0] & 0x1111111111111111) |
|
||||
(q[1] & 0x2222222222222222) |
|
||||
(q[2] & 0x4444444444444444) |
|
||||
(q[3] & 0x8888888888888888)
|
||||
comp_skey[j + 1] =
|
||||
(q[4] & 0x1111111111111111) |
|
||||
(q[5] & 0x2222222222222222) |
|
||||
(q[6] & 0x4444444444444444) |
|
||||
(q[7] & 0x8888888888888888)
|
||||
}
|
||||
|
||||
mem.zero_explicit(&skey, size_of(skey))
|
||||
mem.zero_explicit(&q, size_of(q))
|
||||
|
||||
return num_rounds
|
||||
}
|
||||
|
||||
@(private)
|
||||
skey_expand :: proc "contextless" (skey, comp_skey: []u64, num_rounds: int) {
|
||||
n := (num_rounds + 1) << 1
|
||||
for u, v := 0, 0; u < n; u, v = u + 1, v + 4 {
|
||||
x0 := comp_skey[u]
|
||||
x1, x2, x3 := x0, x0, x0
|
||||
x0 &= 0x1111111111111111
|
||||
x1 &= 0x2222222222222222
|
||||
x2 &= 0x4444444444444444
|
||||
x3 &= 0x8888888888888888
|
||||
x1 >>= 1
|
||||
x2 >>= 2
|
||||
x3 >>= 3
|
||||
skey[v + 0] = (x0 << 4) - x0
|
||||
skey[v + 1] = (x1 << 4) - x1
|
||||
skey[v + 2] = (x2 << 4) - x2
|
||||
skey[v + 3] = (x3 << 4) - x3
|
||||
}
|
||||
}
|
||||
|
||||
orthogonalize_roundkey :: proc "contextless" (qq: []u64, key: []byte) {
|
||||
if len(qq) < 8 || len(key) != 16 {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
skey: [4]u32 = ---
|
||||
skey[0] = endian.unchecked_get_u32le(key[0:])
|
||||
skey[1] = endian.unchecked_get_u32le(key[4:])
|
||||
skey[2] = endian.unchecked_get_u32le(key[8:])
|
||||
skey[3] = endian.unchecked_get_u32le(key[12:])
|
||||
|
||||
q: [8]u64 = ---
|
||||
q[0], q[4] = interleave_in(skey[:])
|
||||
q[1] = q[0]
|
||||
q[2] = q[0]
|
||||
q[3] = q[0]
|
||||
q[5] = q[4]
|
||||
q[6] = q[4]
|
||||
q[7] = q[4]
|
||||
orthogonalize(&q)
|
||||
|
||||
comp_skey: [2]u64 = ---
|
||||
comp_skey[0] =
|
||||
(q[0] & 0x1111111111111111) |
|
||||
(q[1] & 0x2222222222222222) |
|
||||
(q[2] & 0x4444444444444444) |
|
||||
(q[3] & 0x8888888888888888)
|
||||
comp_skey[1] =
|
||||
(q[4] & 0x1111111111111111) |
|
||||
(q[5] & 0x2222222222222222) |
|
||||
(q[6] & 0x4444444444444444) |
|
||||
(q[7] & 0x8888888888888888)
|
||||
|
||||
for x, u in comp_skey {
|
||||
x0 := x
|
||||
x1, x2, x3 := x0, x0, x0
|
||||
x0 &= 0x1111111111111111
|
||||
x1 &= 0x2222222222222222
|
||||
x2 &= 0x4444444444444444
|
||||
x3 &= 0x8888888888888888
|
||||
x1 >>= 1
|
||||
x2 >>= 2
|
||||
x3 >>= 3
|
||||
qq[u * 4 + 0] = (x0 << 4) - x0
|
||||
qq[u * 4 + 1] = (x1 << 4) - x1
|
||||
qq[u * 4 + 2] = (x2 << 4) - x2
|
||||
qq[u * 4 + 3] = (x3 << 4) - x3
|
||||
}
|
||||
|
||||
mem.zero_explicit(&skey, size_of(skey))
|
||||
mem.zero_explicit(&q, size_of(q))
|
||||
mem.zero_explicit(&comp_skey, size_of(comp_skey))
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
// Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS “AS IS” AND ANY EXPRESS OR
|
||||
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
||||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
||||
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package aes_ct64
|
||||
|
||||
import "base:intrinsics"
|
||||
import "core:crypto/_aes"
|
||||
import "core:encoding/endian"
|
||||
|
||||
@(private = "file")
|
||||
bmul64 :: proc "contextless" (x, y: u64) -> u64 {
|
||||
x0 := x & 0x1111111111111111
|
||||
x1 := x & 0x2222222222222222
|
||||
x2 := x & 0x4444444444444444
|
||||
x3 := x & 0x8888888888888888
|
||||
y0 := y & 0x1111111111111111
|
||||
y1 := y & 0x2222222222222222
|
||||
y2 := y & 0x4444444444444444
|
||||
y3 := y & 0x8888888888888888
|
||||
z0 := (x0 * y0) ~ (x1 * y3) ~ (x2 * y2) ~ (x3 * y1)
|
||||
z1 := (x0 * y1) ~ (x1 * y0) ~ (x2 * y3) ~ (x3 * y2)
|
||||
z2 := (x0 * y2) ~ (x1 * y1) ~ (x2 * y0) ~ (x3 * y3)
|
||||
z3 := (x0 * y3) ~ (x1 * y2) ~ (x2 * y1) ~ (x3 * y0)
|
||||
z0 &= 0x1111111111111111
|
||||
z1 &= 0x2222222222222222
|
||||
z2 &= 0x4444444444444444
|
||||
z3 &= 0x8888888888888888
|
||||
return z0 | z1 | z2 | z3
|
||||
}
|
||||
|
||||
@(private = "file")
|
||||
rev64 :: proc "contextless" (x: u64) -> u64 {
|
||||
x := x
|
||||
x = ((x & 0x5555555555555555) << 1) | ((x >> 1) & 0x5555555555555555)
|
||||
x = ((x & 0x3333333333333333) << 2) | ((x >> 2) & 0x3333333333333333)
|
||||
x = ((x & 0x0F0F0F0F0F0F0F0F) << 4) | ((x >> 4) & 0x0F0F0F0F0F0F0F0F)
|
||||
x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x >> 8) & 0x00FF00FF00FF00FF)
|
||||
x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x >> 16) & 0x0000FFFF0000FFFF)
|
||||
return (x << 32) | (x >> 32)
|
||||
}
|
||||
|
||||
// ghash calculates the GHASH of data, with the key `key`, and input `dst`
|
||||
// and `data`, and stores the resulting digest in `dst`.
|
||||
//
|
||||
// Note: `dst` is both an input and an output, to support easy implementation
|
||||
// of GCM.
|
||||
ghash :: proc "contextless" (dst, key, data: []byte) {
|
||||
if len(dst) != _aes.GHASH_BLOCK_SIZE || len(key) != _aes.GHASH_BLOCK_SIZE {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
buf := data
|
||||
l := len(buf)
|
||||
|
||||
y1 := endian.unchecked_get_u64be(dst[0:])
|
||||
y0 := endian.unchecked_get_u64be(dst[8:])
|
||||
h1 := endian.unchecked_get_u64be(key[0:])
|
||||
h0 := endian.unchecked_get_u64be(key[8:])
|
||||
h0r := rev64(h0)
|
||||
h1r := rev64(h1)
|
||||
h2 := h0 ~ h1
|
||||
h2r := h0r ~ h1r
|
||||
|
||||
src: []byte
|
||||
for l > 0 {
|
||||
if l >= _aes.GHASH_BLOCK_SIZE {
|
||||
src = buf
|
||||
buf = buf[_aes.GHASH_BLOCK_SIZE:]
|
||||
l -= _aes.GHASH_BLOCK_SIZE
|
||||
} else {
|
||||
tmp: [_aes.GHASH_BLOCK_SIZE]byte
|
||||
copy(tmp[:], buf)
|
||||
src = tmp[:]
|
||||
l = 0
|
||||
}
|
||||
y1 ~= endian.unchecked_get_u64be(src)
|
||||
y0 ~= endian.unchecked_get_u64be(src[8:])
|
||||
|
||||
y0r := rev64(y0)
|
||||
y1r := rev64(y1)
|
||||
y2 := y0 ~ y1
|
||||
y2r := y0r ~ y1r
|
||||
|
||||
z0 := bmul64(y0, h0)
|
||||
z1 := bmul64(y1, h1)
|
||||
z2 := bmul64(y2, h2)
|
||||
z0h := bmul64(y0r, h0r)
|
||||
z1h := bmul64(y1r, h1r)
|
||||
z2h := bmul64(y2r, h2r)
|
||||
z2 ~= z0 ~ z1
|
||||
z2h ~= z0h ~ z1h
|
||||
z0h = rev64(z0h) >> 1
|
||||
z1h = rev64(z1h) >> 1
|
||||
z2h = rev64(z2h) >> 1
|
||||
|
||||
v0 := z0
|
||||
v1 := z0h ~ z2
|
||||
v2 := z1 ~ z2h
|
||||
v3 := z1h
|
||||
|
||||
v3 = (v3 << 1) | (v2 >> 63)
|
||||
v2 = (v2 << 1) | (v1 >> 63)
|
||||
v1 = (v1 << 1) | (v0 >> 63)
|
||||
v0 = (v0 << 1)
|
||||
|
||||
v2 ~= v0 ~ (v0 >> 1) ~ (v0 >> 2) ~ (v0 >> 7)
|
||||
v1 ~= (v0 << 63) ~ (v0 << 62) ~ (v0 << 57)
|
||||
v3 ~= v1 ~ (v1 >> 1) ~ (v1 >> 2) ~ (v1 >> 7)
|
||||
v2 ~= (v1 << 63) ~ (v1 << 62) ~ (v1 << 57)
|
||||
|
||||
y0 = v2
|
||||
y1 = v3
|
||||
}
|
||||
|
||||
endian.unchecked_put_u64be(dst[0:], y1)
|
||||
endian.unchecked_put_u64be(dst[8:], y0)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package aes_ct64
|
||||
|
||||
import "base:intrinsics"
|
||||
import "core:crypto/_aes"
|
||||
import "core:encoding/endian"
|
||||
|
||||
load_blockx1 :: proc "contextless" (q: ^[8]u64, src: []byte) {
|
||||
if len(src) != _aes.BLOCK_SIZE {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
w: [4]u32 = ---
|
||||
w[0] = endian.unchecked_get_u32le(src[0:])
|
||||
w[1] = endian.unchecked_get_u32le(src[4:])
|
||||
w[2] = endian.unchecked_get_u32le(src[8:])
|
||||
w[3] = endian.unchecked_get_u32le(src[12:])
|
||||
q[0], q[4] = interleave_in(w[:])
|
||||
orthogonalize(q)
|
||||
}
|
||||
|
||||
store_blockx1 :: proc "contextless" (dst: []byte, q: ^[8]u64) {
|
||||
if len(dst) != _aes.BLOCK_SIZE {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
orthogonalize(q)
|
||||
w0, w1, w2, w3 := interleave_out(q[0], q[4])
|
||||
endian.unchecked_put_u32le(dst[0:], w0)
|
||||
endian.unchecked_put_u32le(dst[4:], w1)
|
||||
endian.unchecked_put_u32le(dst[8:], w2)
|
||||
endian.unchecked_put_u32le(dst[12:], w3)
|
||||
}
|
||||
|
||||
load_blocks :: proc "contextless" (q: ^[8]u64, src: [][]byte) {
|
||||
if n := len(src); n > STRIDE || n == 0 {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
w: [4]u32 = ---
|
||||
for s, i in src {
|
||||
if len(s) != _aes.BLOCK_SIZE {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
w[0] = endian.unchecked_get_u32le(s[0:])
|
||||
w[1] = endian.unchecked_get_u32le(s[4:])
|
||||
w[2] = endian.unchecked_get_u32le(s[8:])
|
||||
w[3] = endian.unchecked_get_u32le(s[12:])
|
||||
q[i], q[i + 4] = interleave_in(w[:])
|
||||
}
|
||||
orthogonalize(q)
|
||||
}
|
||||
|
||||
store_blocks :: proc "contextless" (dst: [][]byte, q: ^[8]u64) {
|
||||
if n := len(dst); n > STRIDE || n == 0 {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
orthogonalize(q)
|
||||
for d, i in dst {
|
||||
// Allow storing [0,4] blocks.
|
||||
if d == nil {
|
||||
break
|
||||
}
|
||||
if len(d) != _aes.BLOCK_SIZE {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
w0, w1, w2, w3 := interleave_out(q[i], q[i + 4])
|
||||
endian.unchecked_put_u32le(d[0:], w0)
|
||||
endian.unchecked_put_u32le(d[4:], w1)
|
||||
endian.unchecked_put_u32le(d[8:], w2)
|
||||
endian.unchecked_put_u32le(d[12:], w3)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
package aes implements the AES block cipher and some common modes.
|
||||
|
||||
See:
|
||||
- https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197-upd1.pdf
|
||||
- https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf
|
||||
- https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
|
||||
*/
|
||||
|
||||
package aes
|
||||
|
||||
import "core:crypto/_aes"
|
||||
|
||||
// KEY_SIZE_128 is the AES-128 key size in bytes.
|
||||
KEY_SIZE_128 :: _aes.KEY_SIZE_128
|
||||
// KEY_SIZE_192 is the AES-192 key size in bytes.
|
||||
KEY_SIZE_192 :: _aes.KEY_SIZE_192
|
||||
// KEY_SIZE_256 is the AES-256 key size in bytes.
|
||||
KEY_SIZE_256 :: _aes.KEY_SIZE_256
|
||||
|
||||
// BLOCK_SIZE is the AES block size in bytes.
|
||||
BLOCK_SIZE :: _aes.BLOCK_SIZE
|
||||
@@ -0,0 +1,199 @@
|
||||
package aes
|
||||
|
||||
import "core:crypto/_aes/ct64"
|
||||
import "core:encoding/endian"
|
||||
import "core:math/bits"
|
||||
import "core:mem"
|
||||
|
||||
// CTR_IV_SIZE is the size of the CTR mode IV in bytes.
|
||||
CTR_IV_SIZE :: 16
|
||||
|
||||
// Context_CTR is a keyed AES-CTR instance.
|
||||
Context_CTR :: struct {
|
||||
_impl: Context_Impl,
|
||||
_buffer: [BLOCK_SIZE]byte,
|
||||
_off: int,
|
||||
_ctr_hi: u64,
|
||||
_ctr_lo: u64,
|
||||
_is_initialized: bool,
|
||||
}
|
||||
|
||||
// init_ctr initializes a Context_CTR with the provided key and IV.
|
||||
init_ctr :: proc(ctx: ^Context_CTR, key, iv: []byte, impl := Implementation.Hardware) {
|
||||
if len(iv) != CTR_IV_SIZE {
|
||||
panic("crypto/aes: invalid CTR IV size")
|
||||
}
|
||||
|
||||
init_impl(&ctx._impl, key, impl)
|
||||
ctx._off = BLOCK_SIZE
|
||||
ctx._ctr_hi = endian.unchecked_get_u64be(iv[0:])
|
||||
ctx._ctr_lo = endian.unchecked_get_u64be(iv[8:])
|
||||
ctx._is_initialized = true
|
||||
}
|
||||
|
||||
// xor_bytes_ctr XORs each byte in src with bytes taken from the AES-CTR
|
||||
// keystream, and writes the resulting output to dst. dst and src MUST
|
||||
// alias exactly or not at all.
|
||||
xor_bytes_ctr :: proc(ctx: ^Context_CTR, dst, src: []byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
// TODO: Enforcing that dst and src alias exactly or not at all
|
||||
// is a good idea, though odd aliasing should be extremely uncommon.
|
||||
|
||||
src, dst := src, dst
|
||||
if dst_len := len(dst); dst_len < len(src) {
|
||||
src = src[:dst_len]
|
||||
}
|
||||
|
||||
for remaining := len(src); remaining > 0; {
|
||||
// Process multiple blocks at once
|
||||
if ctx._off == BLOCK_SIZE {
|
||||
if nr_blocks := remaining / BLOCK_SIZE; nr_blocks > 0 {
|
||||
direct_bytes := nr_blocks * BLOCK_SIZE
|
||||
ctr_blocks(ctx, dst, src, nr_blocks)
|
||||
remaining -= direct_bytes
|
||||
if remaining == 0 {
|
||||
return
|
||||
}
|
||||
dst = dst[direct_bytes:]
|
||||
src = src[direct_bytes:]
|
||||
}
|
||||
|
||||
// If there is a partial block, generate and buffer 1 block
|
||||
// worth of keystream.
|
||||
ctr_blocks(ctx, ctx._buffer[:], nil, 1)
|
||||
ctx._off = 0
|
||||
}
|
||||
|
||||
// Process partial blocks from the buffered keystream.
|
||||
to_xor := min(BLOCK_SIZE - ctx._off, remaining)
|
||||
buffered_keystream := ctx._buffer[ctx._off:]
|
||||
for i := 0; i < to_xor; i = i + 1 {
|
||||
dst[i] = buffered_keystream[i] ~ src[i]
|
||||
}
|
||||
ctx._off += to_xor
|
||||
dst = dst[to_xor:]
|
||||
src = src[to_xor:]
|
||||
remaining -= to_xor
|
||||
}
|
||||
}
|
||||
|
||||
// keystream_bytes_ctr fills dst with the raw AES-CTR keystream output.
|
||||
keystream_bytes_ctr :: proc(ctx: ^Context_CTR, dst: []byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
dst := dst
|
||||
for remaining := len(dst); remaining > 0; {
|
||||
// Process multiple blocks at once
|
||||
if ctx._off == BLOCK_SIZE {
|
||||
if nr_blocks := remaining / BLOCK_SIZE; nr_blocks > 0 {
|
||||
direct_bytes := nr_blocks * BLOCK_SIZE
|
||||
ctr_blocks(ctx, dst, nil, nr_blocks)
|
||||
remaining -= direct_bytes
|
||||
if remaining == 0 {
|
||||
return
|
||||
}
|
||||
dst = dst[direct_bytes:]
|
||||
}
|
||||
|
||||
// If there is a partial block, generate and buffer 1 block
|
||||
// worth of keystream.
|
||||
ctr_blocks(ctx, ctx._buffer[:], nil, 1)
|
||||
ctx._off = 0
|
||||
}
|
||||
|
||||
// Process partial blocks from the buffered keystream.
|
||||
to_copy := min(BLOCK_SIZE - ctx._off, remaining)
|
||||
buffered_keystream := ctx._buffer[ctx._off:]
|
||||
copy(dst[:to_copy], buffered_keystream[:to_copy])
|
||||
ctx._off += to_copy
|
||||
dst = dst[to_copy:]
|
||||
remaining -= to_copy
|
||||
}
|
||||
}
|
||||
|
||||
// reset_ctr sanitizes the Context_CTR. The Context_CTR must be
|
||||
// re-initialized to be used again.
|
||||
reset_ctr :: proc "contextless" (ctx: ^Context_CTR) {
|
||||
reset_impl(&ctx._impl)
|
||||
ctx._off = 0
|
||||
ctx._ctr_hi = 0
|
||||
ctx._ctr_lo = 0
|
||||
mem.zero_explicit(&ctx._buffer, size_of(ctx._buffer))
|
||||
ctx._is_initialized = false
|
||||
}
|
||||
|
||||
@(private)
|
||||
ctr_blocks :: proc(ctx: ^Context_CTR, dst, src: []byte, nr_blocks: int) {
|
||||
// Use the optimized hardware implementation if available.
|
||||
if _, is_hw := ctx._impl.(Context_Impl_Hardware); is_hw {
|
||||
ctr_blocks_hw(ctx, dst, src, nr_blocks)
|
||||
return
|
||||
}
|
||||
|
||||
// Portable implementation.
|
||||
ct64_inc_ctr := #force_inline proc "contextless" (dst: []byte, hi, lo: u64) -> (u64, u64) {
|
||||
endian.unchecked_put_u64be(dst[0:], hi)
|
||||
endian.unchecked_put_u64be(dst[8:], lo)
|
||||
|
||||
hi, lo := hi, lo
|
||||
carry: u64
|
||||
lo, carry = bits.add_u64(lo, 1, 0)
|
||||
hi, _ = bits.add_u64(hi, 0, carry)
|
||||
return hi, lo
|
||||
}
|
||||
|
||||
impl := &ctx._impl.(ct64.Context)
|
||||
src, dst := src, dst
|
||||
nr_blocks := nr_blocks
|
||||
ctr_hi, ctr_lo := ctx._ctr_hi, ctx._ctr_lo
|
||||
|
||||
tmp: [ct64.STRIDE][BLOCK_SIZE]byte = ---
|
||||
ctrs: [ct64.STRIDE][]byte = ---
|
||||
for i in 0 ..< ct64.STRIDE {
|
||||
ctrs[i] = tmp[i][:]
|
||||
}
|
||||
for nr_blocks > 0 {
|
||||
n := min(ct64.STRIDE, nr_blocks)
|
||||
blocks := ctrs[:n]
|
||||
|
||||
for i in 0 ..< n {
|
||||
ctr_hi, ctr_lo = ct64_inc_ctr(blocks[i], ctr_hi, ctr_lo)
|
||||
}
|
||||
ct64.encrypt_blocks(impl, blocks, blocks)
|
||||
|
||||
xor_blocks(dst, src, blocks)
|
||||
|
||||
if src != nil {
|
||||
src = src[n * BLOCK_SIZE:]
|
||||
}
|
||||
dst = dst[n * BLOCK_SIZE:]
|
||||
nr_blocks -= n
|
||||
}
|
||||
|
||||
// Write back the counter.
|
||||
ctx._ctr_hi, ctx._ctr_lo = ctr_hi, ctr_lo
|
||||
|
||||
mem.zero_explicit(&tmp, size_of(tmp))
|
||||
}
|
||||
|
||||
@(private)
|
||||
xor_blocks :: #force_inline proc "contextless" (dst, src: []byte, blocks: [][]byte) {
|
||||
// Note: This would be faster `core:simd` was used, however if
|
||||
// performance of this implementation matters to where that
|
||||
// optimization would be worth it, use chacha20poly1305, or a
|
||||
// CPU that isn't e-waste.
|
||||
if src != nil {
|
||||
#no_bounds_check {
|
||||
for i in 0 ..< len(blocks) {
|
||||
off := i * BLOCK_SIZE
|
||||
for j in 0 ..< BLOCK_SIZE {
|
||||
blocks[i][j] ~= src[off + j]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in 0 ..< len(blocks) {
|
||||
copy(dst[i * BLOCK_SIZE:], blocks[i])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package aes
|
||||
|
||||
import "core:crypto/_aes/ct64"
|
||||
|
||||
// Context_ECB is a keyed AES-ECB instance.
|
||||
//
|
||||
// WARNING: Using ECB mode is strongly discouraged unless it is being
|
||||
// used to implement higher level constructs.
|
||||
Context_ECB :: struct {
|
||||
_impl: Context_Impl,
|
||||
_is_initialized: bool,
|
||||
}
|
||||
|
||||
// init_ecb initializes a Context_ECB with the provided key.
|
||||
init_ecb :: proc(ctx: ^Context_ECB, key: []byte, impl := Implementation.Hardware) {
|
||||
init_impl(&ctx._impl, key, impl)
|
||||
ctx._is_initialized = true
|
||||
}
|
||||
|
||||
// encrypt_ecb encrypts the BLOCK_SIZE buffer src, and writes the result to dst.
|
||||
encrypt_ecb :: proc(ctx: ^Context_ECB, dst, src: []byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
if len(dst) != BLOCK_SIZE || len(src) != BLOCK_SIZE {
|
||||
panic("crypto/aes: invalid buffer size(s)")
|
||||
}
|
||||
|
||||
switch &impl in ctx._impl {
|
||||
case ct64.Context:
|
||||
ct64.encrypt_block(&impl, dst, src)
|
||||
case Context_Impl_Hardware:
|
||||
encrypt_block_hw(&impl, dst, src)
|
||||
}
|
||||
}
|
||||
|
||||
// decrypt_ecb decrypts the BLOCK_SIZE buffer src, and writes the result to dst.
|
||||
decrypt_ecb :: proc(ctx: ^Context_ECB, dst, src: []byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
if len(dst) != BLOCK_SIZE || len(src) != BLOCK_SIZE {
|
||||
panic("crypto/aes: invalid buffer size(s)")
|
||||
}
|
||||
|
||||
switch &impl in ctx._impl {
|
||||
case ct64.Context:
|
||||
ct64.decrypt_block(&impl, dst, src)
|
||||
case Context_Impl_Hardware:
|
||||
decrypt_block_hw(&impl, dst, src)
|
||||
}
|
||||
}
|
||||
|
||||
// reset_ecb sanitizes the Context_ECB. The Context_ECB must be
|
||||
// re-initialized to be used again.
|
||||
reset_ecb :: proc "contextless" (ctx: ^Context_ECB) {
|
||||
reset_impl(&ctx._impl)
|
||||
ctx._is_initialized = false
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
package aes
|
||||
|
||||
import "core:crypto"
|
||||
import "core:crypto/_aes"
|
||||
import "core:crypto/_aes/ct64"
|
||||
import "core:encoding/endian"
|
||||
import "core:mem"
|
||||
|
||||
// GCM_NONCE_SIZE is the size of the GCM nonce in bytes.
|
||||
GCM_NONCE_SIZE :: 12
|
||||
// GCM_TAG_SIZE is the size of a GCM tag in bytes.
|
||||
GCM_TAG_SIZE :: _aes.GHASH_TAG_SIZE
|
||||
|
||||
@(private)
|
||||
GCM_A_MAX :: max(u64) / 8 // 2^64 - 1 bits -> bytes
|
||||
@(private)
|
||||
GCM_P_MAX :: 0xfffffffe0 // 2^39 - 256 bits -> bytes
|
||||
|
||||
// Context_GCM is a keyed AES-GCM instance.
|
||||
Context_GCM :: struct {
|
||||
_impl: Context_Impl,
|
||||
_is_initialized: bool,
|
||||
}
|
||||
|
||||
// init_gcm initializes a Context_GCM with the provided key.
|
||||
init_gcm :: proc(ctx: ^Context_GCM, key: []byte, impl := Implementation.Hardware) {
|
||||
init_impl(&ctx._impl, key, impl)
|
||||
ctx._is_initialized = true
|
||||
}
|
||||
|
||||
// seal_gcm encrypts the plaintext and authenticates the aad and ciphertext,
|
||||
// with the provided Context_GCM and nonce, stores the output in dst and tag.
|
||||
//
|
||||
// dst and plaintext MUST alias exactly or not at all.
|
||||
seal_gcm :: proc(ctx: ^Context_GCM, dst, tag, nonce, aad, plaintext: []byte) {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
gcm_validate_common_slice_sizes(tag, nonce, aad, plaintext)
|
||||
if len(dst) != len(plaintext) {
|
||||
panic("crypto/aes: invalid destination ciphertext size")
|
||||
}
|
||||
|
||||
if impl, is_hw := ctx._impl.(Context_Impl_Hardware); is_hw {
|
||||
gcm_seal_hw(&impl, dst, tag, nonce, aad, plaintext)
|
||||
return
|
||||
}
|
||||
|
||||
h: [_aes.GHASH_KEY_SIZE]byte
|
||||
j0: [_aes.GHASH_BLOCK_SIZE]byte
|
||||
s: [_aes.GHASH_TAG_SIZE]byte
|
||||
init_ghash_ct64(ctx, &h, &j0, nonce)
|
||||
|
||||
// Note: Our GHASH implementation handles appending padding.
|
||||
ct64.ghash(s[:], h[:], aad)
|
||||
gctr_ct64(ctx, dst, &s, plaintext, &h, nonce, true)
|
||||
final_ghash_ct64(&s, &h, &j0, len(aad), len(plaintext))
|
||||
copy(tag, s[:])
|
||||
|
||||
mem.zero_explicit(&h, len(h))
|
||||
mem.zero_explicit(&j0, len(j0))
|
||||
}
|
||||
|
||||
// open_gcm authenticates the aad and ciphertext, and decrypts the ciphertext,
|
||||
// with the provided Context_GCM, nonce, and tag, and stores the output in dst,
|
||||
// returning true iff the authentication was successful. If authentication
|
||||
// fails, the destination buffer will be zeroed.
|
||||
//
|
||||
// dst and plaintext MUST alias exactly or not at all.
|
||||
open_gcm :: proc(ctx: ^Context_GCM, dst, nonce, aad, ciphertext, tag: []byte) -> bool {
|
||||
assert(ctx._is_initialized)
|
||||
|
||||
gcm_validate_common_slice_sizes(tag, nonce, aad, ciphertext)
|
||||
if len(dst) != len(ciphertext) {
|
||||
panic("crypto/aes: invalid destination plaintext size")
|
||||
}
|
||||
|
||||
if impl, is_hw := ctx._impl.(Context_Impl_Hardware); is_hw {
|
||||
return gcm_open_hw(&impl, dst, nonce, aad, ciphertext, tag)
|
||||
}
|
||||
|
||||
h: [_aes.GHASH_KEY_SIZE]byte
|
||||
j0: [_aes.GHASH_BLOCK_SIZE]byte
|
||||
s: [_aes.GHASH_TAG_SIZE]byte
|
||||
init_ghash_ct64(ctx, &h, &j0, nonce)
|
||||
|
||||
ct64.ghash(s[:], h[:], aad)
|
||||
gctr_ct64(ctx, dst, &s, ciphertext, &h, nonce, false)
|
||||
final_ghash_ct64(&s, &h, &j0, len(aad), len(ciphertext))
|
||||
|
||||
ok := crypto.compare_constant_time(s[:], tag) == 1
|
||||
if !ok {
|
||||
mem.zero_explicit(raw_data(dst), len(dst))
|
||||
}
|
||||
|
||||
mem.zero_explicit(&h, len(h))
|
||||
mem.zero_explicit(&j0, len(j0))
|
||||
mem.zero_explicit(&s, len(s))
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
// reset_ctr sanitizes the Context_GCM. The Context_GCM must be
|
||||
// re-initialized to be used again.
|
||||
reset_gcm :: proc "contextless" (ctx: ^Context_GCM) {
|
||||
reset_impl(&ctx._impl)
|
||||
ctx._is_initialized = false
|
||||
}
|
||||
|
||||
@(private)
|
||||
gcm_validate_common_slice_sizes :: proc(tag, nonce, aad, text: []byte) {
|
||||
if len(tag) != GCM_TAG_SIZE {
|
||||
panic("crypto/aes: invalid GCM tag size")
|
||||
}
|
||||
|
||||
// The specification supports nonces in the range [1, 2^64) bits
|
||||
// however per NIST SP 800-38D 5.2.1.1:
|
||||
//
|
||||
// > For IVs, it is recommended that implementations restrict support
|
||||
// > to the length of 96 bits, to promote interoperability, efficiency,
|
||||
// > and simplicity of design.
|
||||
if len(nonce) != GCM_NONCE_SIZE {
|
||||
panic("crypto/aes: invalid GCM nonce size")
|
||||
}
|
||||
|
||||
if aad_len := u64(len(aad)); aad_len > GCM_A_MAX {
|
||||
panic("crypto/aes: oversized GCM aad")
|
||||
}
|
||||
if text_len := u64(len(text)); text_len > GCM_P_MAX {
|
||||
panic("crypto/aes: oversized GCM src data")
|
||||
}
|
||||
}
|
||||
|
||||
@(private = "file")
|
||||
init_ghash_ct64 :: proc(
|
||||
ctx: ^Context_GCM,
|
||||
h: ^[_aes.GHASH_KEY_SIZE]byte,
|
||||
j0: ^[_aes.GHASH_BLOCK_SIZE]byte,
|
||||
nonce: []byte,
|
||||
) {
|
||||
impl := &ctx._impl.(ct64.Context)
|
||||
|
||||
// 1. Let H = CIPH(k, 0^128)
|
||||
ct64.encrypt_block(impl, h[:], h[:])
|
||||
|
||||
// ECB encrypt j0, so that we can just XOR with the tag. In theory
|
||||
// this could be processed along with the final GCTR block, to
|
||||
// potentially save a call to AES-ECB, but... just use AES-NI.
|
||||
copy(j0[:], nonce)
|
||||
j0[_aes.GHASH_BLOCK_SIZE - 1] = 1
|
||||
ct64.encrypt_block(impl, j0[:], j0[:])
|
||||
}
|
||||
|
||||
@(private = "file")
|
||||
final_ghash_ct64 :: proc(
|
||||
s: ^[_aes.GHASH_BLOCK_SIZE]byte,
|
||||
h: ^[_aes.GHASH_KEY_SIZE]byte,
|
||||
j0: ^[_aes.GHASH_BLOCK_SIZE]byte,
|
||||
a_len: int,
|
||||
t_len: int,
|
||||
) {
|
||||
blk: [_aes.GHASH_BLOCK_SIZE]byte
|
||||
endian.unchecked_put_u64be(blk[0:], u64(a_len) * 8)
|
||||
endian.unchecked_put_u64be(blk[8:], u64(t_len) * 8)
|
||||
|
||||
ct64.ghash(s[:], h[:], blk[:])
|
||||
for i in 0 ..< len(s) {
|
||||
s[i] ~= j0[i]
|
||||
}
|
||||
}
|
||||
|
||||
@(private = "file")
|
||||
gctr_ct64 :: proc(
|
||||
ctx: ^Context_GCM,
|
||||
dst: []byte,
|
||||
s: ^[_aes.GHASH_BLOCK_SIZE]byte,
|
||||
src: []byte,
|
||||
h: ^[_aes.GHASH_KEY_SIZE]byte,
|
||||
nonce: []byte,
|
||||
is_seal: bool,
|
||||
) {
|
||||
ct64_inc_ctr32 := #force_inline proc "contextless" (dst: []byte, ctr: u32) -> u32 {
|
||||
endian.unchecked_put_u32be(dst[12:], ctr)
|
||||
return ctr + 1
|
||||
}
|
||||
|
||||
// 2. Define a block J_0 as follows:
|
||||
// if len(IV) = 96, then let J0 = IV || 0^31 || 1
|
||||
//
|
||||
// Note: We only support 96 bit IVs.
|
||||
tmp, tmp2: [ct64.STRIDE][BLOCK_SIZE]byte = ---, ---
|
||||
ctrs, blks: [ct64.STRIDE][]byte = ---, ---
|
||||
ctr: u32 = 2
|
||||
for i in 0 ..< ct64.STRIDE {
|
||||
// Setup scratch space for the keystream.
|
||||
blks[i] = tmp2[i][:]
|
||||
|
||||
// Pre-copy the IV to all the counter blocks.
|
||||
ctrs[i] = tmp[i][:]
|
||||
copy(ctrs[i], nonce)
|
||||
}
|
||||
|
||||
// We stitch the GCTR and GHASH operations together, so that only
|
||||
// one pass over the ciphertext is required.
|
||||
|
||||
impl := &ctx._impl.(ct64.Context)
|
||||
src, dst := src, dst
|
||||
|
||||
nr_blocks := len(src) / BLOCK_SIZE
|
||||
for nr_blocks > 0 {
|
||||
n := min(ct64.STRIDE, nr_blocks)
|
||||
l := n * BLOCK_SIZE
|
||||
|
||||
if !is_seal {
|
||||
ct64.ghash(s[:], h[:], src[:l])
|
||||
}
|
||||
|
||||
// The keystream is written to a separate buffer, as we will
|
||||
// reuse the first 96-bits of each counter.
|
||||
for i in 0 ..< n {
|
||||
ctr = ct64_inc_ctr32(ctrs[i], ctr)
|
||||
}
|
||||
ct64.encrypt_blocks(impl, blks[:n], ctrs[:n])
|
||||
|
||||
xor_blocks(dst, src, blks[:n])
|
||||
|
||||
if is_seal {
|
||||
ct64.ghash(s[:], h[:], dst[:l])
|
||||
}
|
||||
|
||||
src = src[l:]
|
||||
dst = dst[l:]
|
||||
nr_blocks -= n
|
||||
}
|
||||
if l := len(src); l > 0 {
|
||||
if !is_seal {
|
||||
ct64.ghash(s[:], h[:], src[:l])
|
||||
}
|
||||
|
||||
ct64_inc_ctr32(ctrs[0], ctr)
|
||||
ct64.encrypt_block(impl, ctrs[0], ctrs[0])
|
||||
|
||||
for i in 0 ..< l {
|
||||
dst[i] = src[i] ~ ctrs[0][i]
|
||||
}
|
||||
|
||||
if is_seal {
|
||||
ct64.ghash(s[:], h[:], dst[:l])
|
||||
}
|
||||
}
|
||||
|
||||
mem.zero_explicit(&tmp, size_of(tmp))
|
||||
mem.zero_explicit(&tmp2, size_of(tmp2))
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package aes
|
||||
|
||||
import "core:crypto/_aes/ct64"
|
||||
import "core:mem"
|
||||
import "core:reflect"
|
||||
|
||||
@(private)
|
||||
Context_Impl :: union {
|
||||
ct64.Context,
|
||||
Context_Impl_Hardware,
|
||||
}
|
||||
|
||||
// Implementation is an AES implementation. Most callers will not need
|
||||
// to use this as the package will automatically select the most performant
|
||||
// implementation available (See `is_hardware_accelerated()`).
|
||||
Implementation :: enum {
|
||||
Portable,
|
||||
Hardware,
|
||||
}
|
||||
|
||||
@(private)
|
||||
init_impl :: proc(ctx: ^Context_Impl, key: []byte, impl: Implementation) {
|
||||
impl := impl
|
||||
if !is_hardware_accelerated() {
|
||||
impl = .Portable
|
||||
}
|
||||
|
||||
switch impl {
|
||||
case .Portable:
|
||||
reflect.set_union_variant_typeid(ctx^, typeid_of(ct64.Context))
|
||||
ct64.init(&ctx.(ct64.Context), key)
|
||||
case .Hardware:
|
||||
reflect.set_union_variant_typeid(ctx^, typeid_of(Context_Impl_Hardware))
|
||||
init_impl_hw(&ctx.(Context_Impl_Hardware), key)
|
||||
}
|
||||
}
|
||||
|
||||
@(private)
|
||||
reset_impl :: proc "contextless" (ctx: ^Context_Impl) {
|
||||
mem.zero_explicit(ctx, size_of(Context_Impl))
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package aes
|
||||
|
||||
@(private = "file")
|
||||
ERR_HW_NOT_SUPPORTED :: "crypto/aes: hardware implementation unsupported"
|
||||
|
||||
// is_hardware_accelerated returns true iff hardware accelerated AES
|
||||
// is supported.
|
||||
is_hardware_accelerated :: proc "contextless" () -> bool {
|
||||
return false
|
||||
}
|
||||
|
||||
@(private)
|
||||
Context_Impl_Hardware :: struct {}
|
||||
|
||||
@(private)
|
||||
init_impl_hw :: proc(ctx: ^Context_Impl_Hardware, key: []byte) {
|
||||
panic(ERR_HW_NOT_SUPPORTED)
|
||||
}
|
||||
|
||||
@(private)
|
||||
encrypt_block_hw :: proc(ctx: ^Context_Impl_Hardware, dst, src: []byte) {
|
||||
panic(ERR_HW_NOT_SUPPORTED)
|
||||
}
|
||||
|
||||
@(private)
|
||||
decrypt_block_hw :: proc(ctx: ^Context_Impl_Hardware, dst, src: []byte) {
|
||||
panic(ERR_HW_NOT_SUPPORTED)
|
||||
}
|
||||
|
||||
@(private)
|
||||
ctr_blocks_hw :: proc(ctx: ^Context_CTR, dst, src: []byte, nr_blocks: int) {
|
||||
panic(ERR_HW_NOT_SUPPORTED)
|
||||
}
|
||||
|
||||
@(private)
|
||||
gcm_seal_hw :: proc(ctx: ^Context_Impl_Hardware, dst, tag, nonce, aad, plaintext: []byte) {
|
||||
panic(ERR_HW_NOT_SUPPORTED)
|
||||
}
|
||||
|
||||
@(private)
|
||||
gcm_open_hw :: proc(ctx: ^Context_Impl_Hardware, dst, nonce, aad, ciphertext, tag: []byte) -> bool {
|
||||
panic(ERR_HW_NOT_SUPPORTED)
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package ansi
|
||||
|
||||
BEL :: "\a" // Bell
|
||||
BS :: "\b" // Backspace
|
||||
ESC :: "\e" // Escape
|
||||
|
||||
// Fe Escape sequences
|
||||
|
||||
CSI :: ESC + "[" // Control Sequence Introducer
|
||||
OSC :: ESC + "]" // Operating System Command
|
||||
ST :: ESC + "\\" // String Terminator
|
||||
|
||||
// CSI sequences
|
||||
|
||||
CUU :: "A" // Cursor Up
|
||||
CUD :: "B" // Cursor Down
|
||||
CUF :: "C" // Cursor Forward
|
||||
CUB :: "D" // Cursor Back
|
||||
CNL :: "E" // Cursor Next Line
|
||||
CPL :: "F" // Cursor Previous Line
|
||||
CHA :: "G" // Cursor Horizontal Absolute
|
||||
CUP :: "H" // Cursor Position
|
||||
ED :: "J" // Erase in Display
|
||||
EL :: "K" // Erase in Line
|
||||
SU :: "S" // Scroll Up
|
||||
SD :: "T" // Scroll Down
|
||||
HVP :: "f" // Horizontal Vertical Position
|
||||
SGR :: "m" // Select Graphic Rendition
|
||||
AUX_ON :: "5i" // AUX Port On
|
||||
AUX_OFF :: "4i" // AUX Port Off
|
||||
DSR :: "6n" // Device Status Report
|
||||
|
||||
// CSI: private sequences
|
||||
|
||||
SCP :: "s" // Save Current Cursor Position
|
||||
RCP :: "u" // Restore Saved Cursor Position
|
||||
DECAWM_ON :: "?7h" // Auto Wrap Mode (Enabled)
|
||||
DECAWM_OFF :: "?7l" // Auto Wrap Mode (Disabled)
|
||||
DECTCEM_SHOW :: "?25h" // Text Cursor Enable Mode (Visible)
|
||||
DECTCEM_HIDE :: "?25l" // Text Cursor Enable Mode (Invisible)
|
||||
|
||||
// SGR sequences
|
||||
|
||||
RESET :: "0"
|
||||
BOLD :: "1"
|
||||
FAINT :: "2"
|
||||
ITALIC :: "3" // Not widely supported.
|
||||
UNDERLINE :: "4"
|
||||
BLINK_SLOW :: "5"
|
||||
BLINK_RAPID :: "6" // Not widely supported.
|
||||
INVERT :: "7" // Also known as reverse video.
|
||||
HIDE :: "8" // Not widely supported.
|
||||
STRIKE :: "9"
|
||||
FONT_PRIMARY :: "10"
|
||||
FONT_ALT1 :: "11"
|
||||
FONT_ALT2 :: "12"
|
||||
FONT_ALT3 :: "13"
|
||||
FONT_ALT4 :: "14"
|
||||
FONT_ALT5 :: "15"
|
||||
FONT_ALT6 :: "16"
|
||||
FONT_ALT7 :: "17"
|
||||
FONT_ALT8 :: "18"
|
||||
FONT_ALT9 :: "19"
|
||||
FONT_FRAKTUR :: "20" // Rarely supported.
|
||||
UNDERLINE_DOUBLE :: "21" // May be interpreted as "disable bold."
|
||||
NO_BOLD_FAINT :: "22"
|
||||
NO_ITALIC_BLACKLETTER :: "23"
|
||||
NO_UNDERLINE :: "24"
|
||||
NO_BLINK :: "25"
|
||||
PROPORTIONAL_SPACING :: "26"
|
||||
NO_REVERSE :: "27"
|
||||
NO_HIDE :: "28"
|
||||
NO_STRIKE :: "29"
|
||||
|
||||
FG_BLACK :: "30"
|
||||
FG_RED :: "31"
|
||||
FG_GREEN :: "32"
|
||||
FG_YELLOW :: "33"
|
||||
FG_BLUE :: "34"
|
||||
FG_MAGENTA :: "35"
|
||||
FG_CYAN :: "36"
|
||||
FG_WHITE :: "37"
|
||||
FG_COLOR :: "38"
|
||||
FG_COLOR_8_BIT :: "38;5" // Followed by ";n" where n is in 0..=255
|
||||
FG_COLOR_24_BIT :: "38;2" // Followed by ";r;g;b" where r,g,b are in 0..=255
|
||||
FG_DEFAULT :: "39"
|
||||
|
||||
BG_BLACK :: "40"
|
||||
BG_RED :: "41"
|
||||
BG_GREEN :: "42"
|
||||
BG_YELLOW :: "43"
|
||||
BG_BLUE :: "44"
|
||||
BG_MAGENTA :: "45"
|
||||
BG_CYAN :: "46"
|
||||
BG_WHITE :: "47"
|
||||
BG_COLOR :: "48"
|
||||
BG_COLOR_8_BIT :: "48;5" // Followed by ";n" where n is in 0..=255
|
||||
BG_COLOR_24_BIT :: "48;2" // Followed by ";r;g;b" where r,g,b are in 0..=255
|
||||
BG_DEFAULT :: "49"
|
||||
|
||||
NO_PROPORTIONAL_SPACING :: "50"
|
||||
FRAMED :: "51"
|
||||
ENCIRCLED :: "52"
|
||||
OVERLINED :: "53"
|
||||
NO_FRAME_ENCIRCLE :: "54"
|
||||
NO_OVERLINE :: "55"
|
||||
|
||||
// SGR: non-standard bright colors
|
||||
|
||||
FG_BRIGHT_BLACK :: "90" // Also known as grey.
|
||||
FG_BRIGHT_RED :: "91"
|
||||
FG_BRIGHT_GREEN :: "92"
|
||||
FG_BRIGHT_YELLOW :: "93"
|
||||
FG_BRIGHT_BLUE :: "94"
|
||||
FG_BRIGHT_MAGENTA :: "95"
|
||||
FG_BRIGHT_CYAN :: "96"
|
||||
FG_BRIGHT_WHITE :: "97"
|
||||
|
||||
BG_BRIGHT_BLACK :: "100" // Also known as grey.
|
||||
BG_BRIGHT_RED :: "101"
|
||||
BG_BRIGHT_GREEN :: "102"
|
||||
BG_BRIGHT_YELLOW :: "103"
|
||||
BG_BRIGHT_BLUE :: "104"
|
||||
BG_BRIGHT_MAGENTA :: "105"
|
||||
BG_BRIGHT_CYAN :: "106"
|
||||
BG_BRIGHT_WHITE :: "107"
|
||||
|
||||
// Fp Escape sequences
|
||||
|
||||
DECSC :: ESC + "7" // DEC Save Cursor
|
||||
DECRC :: ESC + "8" // DEC Restore Cursor
|
||||
|
||||
// OSC sequences
|
||||
|
||||
WINDOW_TITLE :: "2" // Followed by ";<text>" ST.
|
||||
HYPERLINK :: "8" // Followed by ";[params];<URI>" ST. Closed by OSC HYPERLINK ";;" ST.
|
||||
CLIPBOARD :: "52" // Followed by ";c;<Base64-encoded string>" ST.
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
package ansi implements constant references to many widely-supported ANSI
|
||||
escape codes, primarily used in terminal emulators for enhanced graphics, such
|
||||
as colors, text styling, and animated displays.
|
||||
|
||||
For example, you can print out a line of cyan text like this:
|
||||
fmt.println(ansi.CSI + ansi.FG_CYAN + ansi.SGR + "Hellope!" + ansi.CSI + ansi.RESET + ansi.SGR)
|
||||
|
||||
Multiple SGR (Select Graphic Rendition) codes can be joined by semicolons:
|
||||
fmt.println(ansi.CSI + ansi.BOLD + ";" + ansi.FG_BLUE + ansi.SGR + "Hellope!" + ansi.CSI + ansi.RESET + ansi.SGR)
|
||||
|
||||
If your terminal supports 24-bit true color mode, you can also do this:
|
||||
fmt.println(ansi.CSI + ansi.FG_COLOR_24_BIT + ";0;255;255" + ansi.SGR + "Hellope!" + ansi.CSI + ansi.RESET + ansi.SGR)
|
||||
|
||||
For more information, see:
|
||||
1. https://en.wikipedia.org/wiki/ANSI_escape_code
|
||||
2. https://www.vt100.net/docs/vt102-ug/chapter5.html
|
||||
3. https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
||||
*/
|
||||
package ansi
|
||||
@@ -320,8 +320,8 @@ to_diagnostic_format :: proc {
|
||||
|
||||
// Turns the given CBOR value into a human-readable string.
|
||||
// See docs on the proc group `diagnose` for more info.
|
||||
to_diagnostic_format_string :: proc(val: Value, padding := 0, allocator := context.allocator) -> (string, mem.Allocator_Error) #optional_allocator_error {
|
||||
b := strings.builder_make(allocator)
|
||||
to_diagnostic_format_string :: proc(val: Value, padding := 0, allocator := context.allocator, loc := #caller_location) -> (string, mem.Allocator_Error) #optional_allocator_error {
|
||||
b := strings.builder_make(allocator, loc)
|
||||
w := strings.to_stream(&b)
|
||||
err := to_diagnostic_format_writer(w, val, padding)
|
||||
if err == .EOF {
|
||||
|
||||
@@ -95,24 +95,25 @@ decode :: decode_from
|
||||
|
||||
// Decodes the given string as CBOR.
|
||||
// See docs on the proc group `decode` for more information.
|
||||
decode_from_string :: proc(s: string, flags: Decoder_Flags = {}, allocator := context.allocator) -> (v: Value, err: Decode_Error) {
|
||||
decode_from_string :: proc(s: string, flags: Decoder_Flags = {}, allocator := context.allocator, loc := #caller_location) -> (v: Value, err: Decode_Error) {
|
||||
r: strings.Reader
|
||||
strings.reader_init(&r, s)
|
||||
return decode_from_reader(strings.reader_to_stream(&r), flags, allocator)
|
||||
return decode_from_reader(strings.reader_to_stream(&r), flags, allocator, loc)
|
||||
}
|
||||
|
||||
// Reads a CBOR value from the given reader.
|
||||
// See docs on the proc group `decode` for more information.
|
||||
decode_from_reader :: proc(r: io.Reader, flags: Decoder_Flags = {}, allocator := context.allocator) -> (v: Value, err: Decode_Error) {
|
||||
decode_from_reader :: proc(r: io.Reader, flags: Decoder_Flags = {}, allocator := context.allocator, loc := #caller_location) -> (v: Value, err: Decode_Error) {
|
||||
return decode_from_decoder(
|
||||
Decoder{ DEFAULT_MAX_PRE_ALLOC, flags, r },
|
||||
allocator=allocator,
|
||||
loc = loc,
|
||||
)
|
||||
}
|
||||
|
||||
// Reads a CBOR value from the given decoder.
|
||||
// See docs on the proc group `decode` for more information.
|
||||
decode_from_decoder :: proc(d: Decoder, allocator := context.allocator) -> (v: Value, err: Decode_Error) {
|
||||
decode_from_decoder :: proc(d: Decoder, allocator := context.allocator, loc := #caller_location) -> (v: Value, err: Decode_Error) {
|
||||
context.allocator = allocator
|
||||
|
||||
d := d
|
||||
@@ -121,13 +122,13 @@ decode_from_decoder :: proc(d: Decoder, allocator := context.allocator) -> (v: V
|
||||
d.max_pre_alloc = DEFAULT_MAX_PRE_ALLOC
|
||||
}
|
||||
|
||||
v, err = _decode_from_decoder(d)
|
||||
v, err = _decode_from_decoder(d, {}, allocator, loc)
|
||||
// Normal EOF does not exist here, we try to read the exact amount that is said to be provided.
|
||||
if err == .EOF { err = .Unexpected_EOF }
|
||||
return
|
||||
}
|
||||
|
||||
_decode_from_decoder :: proc(d: Decoder, hdr: Header = Header(0)) -> (v: Value, err: Decode_Error) {
|
||||
_decode_from_decoder :: proc(d: Decoder, hdr: Header = Header(0), allocator := context.allocator, loc := #caller_location) -> (v: Value, err: Decode_Error) {
|
||||
hdr := hdr
|
||||
r := d.reader
|
||||
if hdr == Header(0) { hdr = _decode_header(r) or_return }
|
||||
@@ -161,11 +162,11 @@ _decode_from_decoder :: proc(d: Decoder, hdr: Header = Header(0)) -> (v: Value,
|
||||
switch maj {
|
||||
case .Unsigned: return _decode_tiny_u8(add)
|
||||
case .Negative: return Negative_U8(_decode_tiny_u8(add) or_return), nil
|
||||
case .Bytes: return _decode_bytes_ptr(d, add)
|
||||
case .Text: return _decode_text_ptr(d, add)
|
||||
case .Array: return _decode_array_ptr(d, add)
|
||||
case .Map: return _decode_map_ptr(d, add)
|
||||
case .Tag: return _decode_tag_ptr(d, add)
|
||||
case .Bytes: return _decode_bytes_ptr(d, add, .Bytes, allocator, loc)
|
||||
case .Text: return _decode_text_ptr(d, add, allocator, loc)
|
||||
case .Array: return _decode_array_ptr(d, add, allocator, loc)
|
||||
case .Map: return _decode_map_ptr(d, add, allocator, loc)
|
||||
case .Tag: return _decode_tag_ptr(d, add, allocator, loc)
|
||||
case .Other: return _decode_tiny_simple(add)
|
||||
case: return nil, .Bad_Major
|
||||
}
|
||||
@@ -203,27 +204,27 @@ encode :: encode_into
|
||||
|
||||
// Encodes the CBOR value into binary CBOR allocated on the given allocator.
|
||||
// See the docs on the proc group `encode_into` for more info.
|
||||
encode_into_bytes :: proc(v: Value, flags := ENCODE_SMALL, allocator := context.allocator, temp_allocator := context.temp_allocator) -> (data: []byte, err: Encode_Error) {
|
||||
b := strings.builder_make(allocator) or_return
|
||||
encode_into_bytes :: proc(v: Value, flags := ENCODE_SMALL, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> (data: []byte, err: Encode_Error) {
|
||||
b := strings.builder_make(allocator, loc) or_return
|
||||
encode_into_builder(&b, v, flags, temp_allocator) or_return
|
||||
return b.buf[:], nil
|
||||
}
|
||||
|
||||
// Encodes the CBOR value into binary CBOR written to the given builder.
|
||||
// See the docs on the proc group `encode_into` for more info.
|
||||
encode_into_builder :: proc(b: ^strings.Builder, v: Value, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator) -> Encode_Error {
|
||||
return encode_into_writer(strings.to_stream(b), v, flags, temp_allocator)
|
||||
encode_into_builder :: proc(b: ^strings.Builder, v: Value, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator, loc := #caller_location) -> Encode_Error {
|
||||
return encode_into_writer(strings.to_stream(b), v, flags, temp_allocator, loc=loc)
|
||||
}
|
||||
|
||||
// Encodes the CBOR value into binary CBOR written to the given writer.
|
||||
// See the docs on the proc group `encode_into` for more info.
|
||||
encode_into_writer :: proc(w: io.Writer, v: Value, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator) -> Encode_Error {
|
||||
return encode_into_encoder(Encoder{flags, w, temp_allocator}, v)
|
||||
encode_into_writer :: proc(w: io.Writer, v: Value, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator, loc := #caller_location) -> Encode_Error {
|
||||
return encode_into_encoder(Encoder{flags, w, temp_allocator}, v, loc=loc)
|
||||
}
|
||||
|
||||
// Encodes the CBOR value into binary CBOR written to the given encoder.
|
||||
// See the docs on the proc group `encode_into` for more info.
|
||||
encode_into_encoder :: proc(e: Encoder, v: Value) -> Encode_Error {
|
||||
encode_into_encoder :: proc(e: Encoder, v: Value, loc := #caller_location) -> Encode_Error {
|
||||
e := e
|
||||
|
||||
if e.temp_allocator.procedure == nil {
|
||||
@@ -366,21 +367,21 @@ _encode_u64_exact :: proc(w: io.Writer, v: u64, major: Major = .Unsigned) -> (er
|
||||
return
|
||||
}
|
||||
|
||||
_decode_bytes_ptr :: proc(d: Decoder, add: Add, type: Major = .Bytes) -> (v: ^Bytes, err: Decode_Error) {
|
||||
v = new(Bytes) or_return
|
||||
defer if err != nil { free(v) }
|
||||
_decode_bytes_ptr :: proc(d: Decoder, add: Add, type: Major = .Bytes, allocator := context.allocator, loc := #caller_location) -> (v: ^Bytes, err: Decode_Error) {
|
||||
v = new(Bytes, allocator, loc) or_return
|
||||
defer if err != nil { free(v, allocator, loc) }
|
||||
|
||||
v^ = _decode_bytes(d, add, type) or_return
|
||||
v^ = _decode_bytes(d, add, type, allocator, loc) or_return
|
||||
return
|
||||
}
|
||||
|
||||
_decode_bytes :: proc(d: Decoder, add: Add, type: Major = .Bytes, allocator := context.allocator) -> (v: Bytes, err: Decode_Error) {
|
||||
_decode_bytes :: proc(d: Decoder, add: Add, type: Major = .Bytes, allocator := context.allocator, loc := #caller_location) -> (v: Bytes, err: Decode_Error) {
|
||||
context.allocator = allocator
|
||||
|
||||
add := add
|
||||
n, scap := _decode_len_str(d, add) or_return
|
||||
|
||||
buf := strings.builder_make(0, scap) or_return
|
||||
buf := strings.builder_make(0, scap, allocator, loc) or_return
|
||||
defer if err != nil { strings.builder_destroy(&buf) }
|
||||
buf_stream := strings.to_stream(&buf)
|
||||
|
||||
@@ -426,40 +427,40 @@ _encode_bytes :: proc(e: Encoder, val: Bytes, major: Major = .Bytes) -> (err: En
|
||||
return
|
||||
}
|
||||
|
||||
_decode_text_ptr :: proc(d: Decoder, add: Add) -> (v: ^Text, err: Decode_Error) {
|
||||
v = new(Text) or_return
|
||||
_decode_text_ptr :: proc(d: Decoder, add: Add, allocator := context.allocator, loc := #caller_location) -> (v: ^Text, err: Decode_Error) {
|
||||
v = new(Text, allocator, loc) or_return
|
||||
defer if err != nil { free(v) }
|
||||
|
||||
v^ = _decode_text(d, add) or_return
|
||||
v^ = _decode_text(d, add, allocator, loc) or_return
|
||||
return
|
||||
}
|
||||
|
||||
_decode_text :: proc(d: Decoder, add: Add, allocator := context.allocator) -> (v: Text, err: Decode_Error) {
|
||||
return (Text)(_decode_bytes(d, add, .Text, allocator) or_return), nil
|
||||
_decode_text :: proc(d: Decoder, add: Add, allocator := context.allocator, loc := #caller_location) -> (v: Text, err: Decode_Error) {
|
||||
return (Text)(_decode_bytes(d, add, .Text, allocator, loc) or_return), nil
|
||||
}
|
||||
|
||||
_encode_text :: proc(e: Encoder, val: Text) -> Encode_Error {
|
||||
return _encode_bytes(e, transmute([]byte)val, .Text)
|
||||
}
|
||||
|
||||
_decode_array_ptr :: proc(d: Decoder, add: Add) -> (v: ^Array, err: Decode_Error) {
|
||||
v = new(Array) or_return
|
||||
_decode_array_ptr :: proc(d: Decoder, add: Add, allocator := context.allocator, loc := #caller_location) -> (v: ^Array, err: Decode_Error) {
|
||||
v = new(Array, allocator, loc) or_return
|
||||
defer if err != nil { free(v) }
|
||||
|
||||
v^ = _decode_array(d, add) or_return
|
||||
v^ = _decode_array(d, add, allocator, loc) or_return
|
||||
return
|
||||
}
|
||||
|
||||
_decode_array :: proc(d: Decoder, add: Add) -> (v: Array, err: Decode_Error) {
|
||||
_decode_array :: proc(d: Decoder, add: Add, allocator := context.allocator, loc := #caller_location) -> (v: Array, err: Decode_Error) {
|
||||
n, scap := _decode_len_container(d, add) or_return
|
||||
array := make([dynamic]Value, 0, scap) or_return
|
||||
array := make([dynamic]Value, 0, scap, allocator, loc) or_return
|
||||
defer if err != nil {
|
||||
for entry in array { destroy(entry) }
|
||||
delete(array)
|
||||
for entry in array { destroy(entry, allocator) }
|
||||
delete(array, loc)
|
||||
}
|
||||
|
||||
for i := 0; n == -1 || i < n; i += 1 {
|
||||
val, verr := _decode_from_decoder(d)
|
||||
val, verr := _decode_from_decoder(d, {}, allocator, loc)
|
||||
if n == -1 && verr == .Break {
|
||||
break
|
||||
} else if verr != nil {
|
||||
@@ -485,39 +486,39 @@ _encode_array :: proc(e: Encoder, arr: Array) -> Encode_Error {
|
||||
return nil
|
||||
}
|
||||
|
||||
_decode_map_ptr :: proc(d: Decoder, add: Add) -> (v: ^Map, err: Decode_Error) {
|
||||
v = new(Map) or_return
|
||||
_decode_map_ptr :: proc(d: Decoder, add: Add, allocator := context.allocator, loc := #caller_location) -> (v: ^Map, err: Decode_Error) {
|
||||
v = new(Map, allocator, loc) or_return
|
||||
defer if err != nil { free(v) }
|
||||
|
||||
v^ = _decode_map(d, add) or_return
|
||||
v^ = _decode_map(d, add, allocator, loc) or_return
|
||||
return
|
||||
}
|
||||
|
||||
_decode_map :: proc(d: Decoder, add: Add) -> (v: Map, err: Decode_Error) {
|
||||
_decode_map :: proc(d: Decoder, add: Add, allocator := context.allocator, loc := #caller_location) -> (v: Map, err: Decode_Error) {
|
||||
n, scap := _decode_len_container(d, add) or_return
|
||||
items := make([dynamic]Map_Entry, 0, scap) or_return
|
||||
items := make([dynamic]Map_Entry, 0, scap, allocator, loc) or_return
|
||||
defer if err != nil {
|
||||
for entry in items {
|
||||
destroy(entry.key)
|
||||
destroy(entry.value)
|
||||
}
|
||||
delete(items)
|
||||
delete(items, loc)
|
||||
}
|
||||
|
||||
for i := 0; n == -1 || i < n; i += 1 {
|
||||
key, kerr := _decode_from_decoder(d)
|
||||
key, kerr := _decode_from_decoder(d, {}, allocator, loc)
|
||||
if n == -1 && kerr == .Break {
|
||||
break
|
||||
} else if kerr != nil {
|
||||
return nil, kerr
|
||||
}
|
||||
|
||||
value := _decode_from_decoder(d) or_return
|
||||
value := _decode_from_decoder(d, {}, allocator, loc) or_return
|
||||
|
||||
append(&items, Map_Entry{
|
||||
key = key,
|
||||
value = value,
|
||||
}) or_return
|
||||
}, loc) or_return
|
||||
}
|
||||
|
||||
if .Shrink_Excess in d.flags { shrink(&items) }
|
||||
@@ -578,20 +579,20 @@ _encode_map :: proc(e: Encoder, m: Map) -> (err: Encode_Error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
_decode_tag_ptr :: proc(d: Decoder, add: Add) -> (v: Value, err: Decode_Error) {
|
||||
tag := _decode_tag(d, add) or_return
|
||||
_decode_tag_ptr :: proc(d: Decoder, add: Add, allocator := context.allocator, loc := #caller_location) -> (v: Value, err: Decode_Error) {
|
||||
tag := _decode_tag(d, add, allocator, loc) or_return
|
||||
if t, ok := tag.?; ok {
|
||||
defer if err != nil { destroy(t.value) }
|
||||
tp := new(Tag) or_return
|
||||
tp := new(Tag, allocator, loc) or_return
|
||||
tp^ = t
|
||||
return tp, nil
|
||||
}
|
||||
|
||||
// no error, no tag, this was the self described CBOR tag, skip it.
|
||||
return _decode_from_decoder(d)
|
||||
return _decode_from_decoder(d, {}, allocator, loc)
|
||||
}
|
||||
|
||||
_decode_tag :: proc(d: Decoder, add: Add) -> (v: Maybe(Tag), err: Decode_Error) {
|
||||
_decode_tag :: proc(d: Decoder, add: Add, allocator := context.allocator, loc := #caller_location) -> (v: Maybe(Tag), err: Decode_Error) {
|
||||
num := _decode_uint_as_u64(d.reader, add) or_return
|
||||
|
||||
// CBOR can be wrapped in a tag that decoders can use to see/check if the binary data is CBOR.
|
||||
@@ -602,7 +603,7 @@ _decode_tag :: proc(d: Decoder, add: Add) -> (v: Maybe(Tag), err: Decode_Error)
|
||||
|
||||
t := Tag{
|
||||
number = num,
|
||||
value = _decode_from_decoder(d) or_return,
|
||||
value = _decode_from_decoder(d, {}, allocator, loc) or_return,
|
||||
}
|
||||
|
||||
if nested, ok := t.value.(^Tag); ok {
|
||||
@@ -883,4 +884,4 @@ _encode_deterministic_f64 :: proc(w: io.Writer, v: f64) -> io.Error {
|
||||
}
|
||||
|
||||
return _encode_f64_exact(w, v)
|
||||
}
|
||||
}
|
||||
@@ -45,8 +45,8 @@ marshal :: marshal_into
|
||||
|
||||
// Marshals the given value into a CBOR byte stream (allocated using the given allocator).
|
||||
// See docs on the `marshal_into` proc group for more info.
|
||||
marshal_into_bytes :: proc(v: any, flags := ENCODE_SMALL, allocator := context.allocator, temp_allocator := context.temp_allocator) -> (bytes: []byte, err: Marshal_Error) {
|
||||
b, alloc_err := strings.builder_make(allocator)
|
||||
marshal_into_bytes :: proc(v: any, flags := ENCODE_SMALL, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> (bytes: []byte, err: Marshal_Error) {
|
||||
b, alloc_err := strings.builder_make(allocator, loc=loc)
|
||||
// The builder as a stream also returns .EOF if it ran out of memory so this is consistent.
|
||||
if alloc_err != nil {
|
||||
return nil, .EOF
|
||||
@@ -54,7 +54,7 @@ marshal_into_bytes :: proc(v: any, flags := ENCODE_SMALL, allocator := context.a
|
||||
|
||||
defer if err != nil { strings.builder_destroy(&b) }
|
||||
|
||||
if err = marshal_into_builder(&b, v, flags, temp_allocator); err != nil {
|
||||
if err = marshal_into_builder(&b, v, flags, temp_allocator, loc=loc); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -63,20 +63,20 @@ marshal_into_bytes :: proc(v: any, flags := ENCODE_SMALL, allocator := context.a
|
||||
|
||||
// Marshals the given value into a CBOR byte stream written to the given builder.
|
||||
// See docs on the `marshal_into` proc group for more info.
|
||||
marshal_into_builder :: proc(b: ^strings.Builder, v: any, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator) -> Marshal_Error {
|
||||
return marshal_into_writer(strings.to_writer(b), v, flags, temp_allocator)
|
||||
marshal_into_builder :: proc(b: ^strings.Builder, v: any, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator, loc := #caller_location) -> Marshal_Error {
|
||||
return marshal_into_writer(strings.to_writer(b), v, flags, temp_allocator, loc=loc)
|
||||
}
|
||||
|
||||
// Marshals the given value into a CBOR byte stream written to the given writer.
|
||||
// See docs on the `marshal_into` proc group for more info.
|
||||
marshal_into_writer :: proc(w: io.Writer, v: any, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator) -> Marshal_Error {
|
||||
marshal_into_writer :: proc(w: io.Writer, v: any, flags := ENCODE_SMALL, temp_allocator := context.temp_allocator, loc := #caller_location) -> Marshal_Error {
|
||||
encoder := Encoder{flags, w, temp_allocator}
|
||||
return marshal_into_encoder(encoder, v)
|
||||
return marshal_into_encoder(encoder, v, loc=loc)
|
||||
}
|
||||
|
||||
// Marshals the given value into a CBOR byte stream written to the given encoder.
|
||||
// See docs on the `marshal_into` proc group for more info.
|
||||
marshal_into_encoder :: proc(e: Encoder, v: any) -> (err: Marshal_Error) {
|
||||
marshal_into_encoder :: proc(e: Encoder, v: any, loc := #caller_location) -> (err: Marshal_Error) {
|
||||
e := e
|
||||
|
||||
if e.temp_allocator.procedure == nil {
|
||||
|
||||
@@ -31,8 +31,8 @@ unmarshal :: proc {
|
||||
unmarshal_from_string,
|
||||
}
|
||||
|
||||
unmarshal_from_reader :: proc(r: io.Reader, ptr: ^$T, flags := Decoder_Flags{}, allocator := context.allocator, temp_allocator := context.temp_allocator) -> (err: Unmarshal_Error) {
|
||||
err = unmarshal_from_decoder(Decoder{ DEFAULT_MAX_PRE_ALLOC, flags, r }, ptr, allocator, temp_allocator)
|
||||
unmarshal_from_reader :: proc(r: io.Reader, ptr: ^$T, flags := Decoder_Flags{}, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
err = unmarshal_from_decoder(Decoder{ DEFAULT_MAX_PRE_ALLOC, flags, r }, ptr, allocator, temp_allocator, loc)
|
||||
|
||||
// Normal EOF does not exist here, we try to read the exact amount that is said to be provided.
|
||||
if err == .EOF { err = .Unexpected_EOF }
|
||||
@@ -40,21 +40,21 @@ unmarshal_from_reader :: proc(r: io.Reader, ptr: ^$T, flags := Decoder_Flags{},
|
||||
}
|
||||
|
||||
// Unmarshals from a string, see docs on the proc group `Unmarshal` for more info.
|
||||
unmarshal_from_string :: proc(s: string, ptr: ^$T, flags := Decoder_Flags{}, allocator := context.allocator, temp_allocator := context.temp_allocator) -> (err: Unmarshal_Error) {
|
||||
unmarshal_from_string :: proc(s: string, ptr: ^$T, flags := Decoder_Flags{}, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
sr: strings.Reader
|
||||
r := strings.to_reader(&sr, s)
|
||||
|
||||
err = unmarshal_from_reader(r, ptr, flags, allocator, temp_allocator)
|
||||
err = unmarshal_from_reader(r, ptr, flags, allocator, temp_allocator, loc)
|
||||
|
||||
// Normal EOF does not exist here, we try to read the exact amount that is said to be provided.
|
||||
if err == .EOF { err = .Unexpected_EOF }
|
||||
return
|
||||
}
|
||||
|
||||
unmarshal_from_decoder :: proc(d: Decoder, ptr: ^$T, allocator := context.allocator, temp_allocator := context.temp_allocator) -> (err: Unmarshal_Error) {
|
||||
unmarshal_from_decoder :: proc(d: Decoder, ptr: ^$T, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
d := d
|
||||
|
||||
err = _unmarshal_any_ptr(d, ptr, nil, allocator, temp_allocator)
|
||||
err = _unmarshal_any_ptr(d, ptr, nil, allocator, temp_allocator, loc)
|
||||
|
||||
// Normal EOF does not exist here, we try to read the exact amount that is said to be provided.
|
||||
if err == .EOF { err = .Unexpected_EOF }
|
||||
@@ -62,7 +62,7 @@ unmarshal_from_decoder :: proc(d: Decoder, ptr: ^$T, allocator := context.alloca
|
||||
|
||||
}
|
||||
|
||||
_unmarshal_any_ptr :: proc(d: Decoder, v: any, hdr: Maybe(Header) = nil, allocator := context.allocator, temp_allocator := context.temp_allocator) -> Unmarshal_Error {
|
||||
_unmarshal_any_ptr :: proc(d: Decoder, v: any, hdr: Maybe(Header) = nil, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> Unmarshal_Error {
|
||||
context.allocator = allocator
|
||||
context.temp_allocator = temp_allocator
|
||||
v := v
|
||||
@@ -78,10 +78,10 @@ _unmarshal_any_ptr :: proc(d: Decoder, v: any, hdr: Maybe(Header) = nil, allocat
|
||||
}
|
||||
|
||||
data := any{(^rawptr)(v.data)^, ti.variant.(reflect.Type_Info_Pointer).elem.id}
|
||||
return _unmarshal_value(d, data, hdr.? or_else (_decode_header(d.reader) or_return))
|
||||
return _unmarshal_value(d, data, hdr.? or_else (_decode_header(d.reader) or_return), allocator, temp_allocator, loc)
|
||||
}
|
||||
|
||||
_unmarshal_value :: proc(d: Decoder, v: any, hdr: Header) -> (err: Unmarshal_Error) {
|
||||
_unmarshal_value :: proc(d: Decoder, v: any, hdr: Header, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
v := v
|
||||
ti := reflect.type_info_base(type_info_of(v.id))
|
||||
r := d.reader
|
||||
@@ -104,7 +104,7 @@ _unmarshal_value :: proc(d: Decoder, v: any, hdr: Header) -> (err: Unmarshal_Err
|
||||
// Allow generic unmarshal by doing it into a `Value`.
|
||||
switch &dst in v {
|
||||
case Value:
|
||||
dst = err_conv(_decode_from_decoder(d, hdr)) or_return
|
||||
dst = err_conv(_decode_from_decoder(d, hdr, allocator, loc)) or_return
|
||||
return
|
||||
}
|
||||
|
||||
@@ -308,7 +308,7 @@ _unmarshal_value :: proc(d: Decoder, v: any, hdr: Header) -> (err: Unmarshal_Err
|
||||
if impl, ok := _tag_implementations_nr[nr]; ok {
|
||||
return impl->unmarshal(d, nr, v)
|
||||
} else if nr == TAG_OBJECT_TYPE {
|
||||
return _unmarshal_union(d, v, ti, hdr)
|
||||
return _unmarshal_union(d, v, ti, hdr, loc=loc)
|
||||
} else {
|
||||
// Discard the tag info and unmarshal as its value.
|
||||
return _unmarshal_value(d, v, _decode_header(r) or_return)
|
||||
@@ -316,19 +316,19 @@ _unmarshal_value :: proc(d: Decoder, v: any, hdr: Header) -> (err: Unmarshal_Err
|
||||
|
||||
return _unsupported(v, hdr, add)
|
||||
|
||||
case .Bytes: return _unmarshal_bytes(d, v, ti, hdr, add)
|
||||
case .Text: return _unmarshal_string(d, v, ti, hdr, add)
|
||||
case .Array: return _unmarshal_array(d, v, ti, hdr, add)
|
||||
case .Map: return _unmarshal_map(d, v, ti, hdr, add)
|
||||
case .Bytes: return _unmarshal_bytes(d, v, ti, hdr, add, allocator=allocator, loc=loc)
|
||||
case .Text: return _unmarshal_string(d, v, ti, hdr, add, allocator=allocator, loc=loc)
|
||||
case .Array: return _unmarshal_array(d, v, ti, hdr, add, allocator=allocator, loc=loc)
|
||||
case .Map: return _unmarshal_map(d, v, ti, hdr, add, allocator=allocator, loc=loc)
|
||||
|
||||
case: return .Bad_Major
|
||||
}
|
||||
}
|
||||
|
||||
_unmarshal_bytes :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add) -> (err: Unmarshal_Error) {
|
||||
_unmarshal_bytes :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add, allocator := context.allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
#partial switch t in ti.variant {
|
||||
case reflect.Type_Info_String:
|
||||
bytes := err_conv(_decode_bytes(d, add)) or_return
|
||||
bytes := err_conv(_decode_bytes(d, add, allocator=allocator, loc=loc)) or_return
|
||||
|
||||
if t.is_cstring {
|
||||
raw := (^cstring)(v.data)
|
||||
@@ -347,7 +347,7 @@ _unmarshal_bytes :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
|
||||
if elem_base.id != byte { return _unsupported(v, hdr) }
|
||||
|
||||
bytes := err_conv(_decode_bytes(d, add)) or_return
|
||||
bytes := err_conv(_decode_bytes(d, add, allocator=allocator, loc=loc)) or_return
|
||||
raw := (^mem.Raw_Slice)(v.data)
|
||||
raw^ = transmute(mem.Raw_Slice)bytes
|
||||
return
|
||||
@@ -357,12 +357,12 @@ _unmarshal_bytes :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
|
||||
if elem_base.id != byte { return _unsupported(v, hdr) }
|
||||
|
||||
bytes := err_conv(_decode_bytes(d, add)) or_return
|
||||
bytes := err_conv(_decode_bytes(d, add, allocator=allocator, loc=loc)) or_return
|
||||
raw := (^mem.Raw_Dynamic_Array)(v.data)
|
||||
raw.data = raw_data(bytes)
|
||||
raw.len = len(bytes)
|
||||
raw.cap = len(bytes)
|
||||
raw.allocator = context.allocator
|
||||
raw.allocator = allocator
|
||||
return
|
||||
|
||||
case reflect.Type_Info_Array:
|
||||
@@ -385,10 +385,10 @@ _unmarshal_bytes :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
return _unsupported(v, hdr)
|
||||
}
|
||||
|
||||
_unmarshal_string :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add) -> (err: Unmarshal_Error) {
|
||||
_unmarshal_string :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add, allocator := context.allocator, temp_allocator := context.temp_allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
#partial switch t in ti.variant {
|
||||
case reflect.Type_Info_String:
|
||||
text := err_conv(_decode_text(d, add)) or_return
|
||||
text := err_conv(_decode_text(d, add, allocator, loc)) or_return
|
||||
|
||||
if t.is_cstring {
|
||||
raw := (^cstring)(v.data)
|
||||
@@ -403,8 +403,8 @@ _unmarshal_string :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Heade
|
||||
|
||||
// Enum by its variant name.
|
||||
case reflect.Type_Info_Enum:
|
||||
text := err_conv(_decode_text(d, add, allocator=context.temp_allocator)) or_return
|
||||
defer delete(text, context.temp_allocator)
|
||||
text := err_conv(_decode_text(d, add, allocator=temp_allocator, loc=loc)) or_return
|
||||
defer delete(text, temp_allocator, loc)
|
||||
|
||||
for name, i in t.names {
|
||||
if name == text {
|
||||
@@ -414,8 +414,8 @@ _unmarshal_string :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Heade
|
||||
}
|
||||
|
||||
case reflect.Type_Info_Rune:
|
||||
text := err_conv(_decode_text(d, add, allocator=context.temp_allocator)) or_return
|
||||
defer delete(text, context.temp_allocator)
|
||||
text := err_conv(_decode_text(d, add, allocator=temp_allocator, loc=loc)) or_return
|
||||
defer delete(text, temp_allocator, loc)
|
||||
|
||||
r := (^rune)(v.data)
|
||||
dr, n := utf8.decode_rune(text)
|
||||
@@ -430,13 +430,15 @@ _unmarshal_string :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Heade
|
||||
return _unsupported(v, hdr)
|
||||
}
|
||||
|
||||
_unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add) -> (err: Unmarshal_Error) {
|
||||
_unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add, allocator := context.allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
assign_array :: proc(
|
||||
d: Decoder,
|
||||
da: ^mem.Raw_Dynamic_Array,
|
||||
elemt: ^reflect.Type_Info,
|
||||
length: int,
|
||||
growable := true,
|
||||
allocator := context.allocator,
|
||||
loc := #caller_location,
|
||||
) -> (out_of_space: bool, err: Unmarshal_Error) {
|
||||
for idx: uintptr = 0; length == -1 || idx < uintptr(length); idx += 1 {
|
||||
elem_ptr := rawptr(uintptr(da.data) + idx*uintptr(elemt.size))
|
||||
@@ -450,13 +452,13 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
if !growable { return true, .Out_Of_Memory }
|
||||
|
||||
cap := 2 * da.cap
|
||||
ok := runtime.__dynamic_array_reserve(da, elemt.size, elemt.align, cap)
|
||||
ok := runtime.__dynamic_array_reserve(da, elemt.size, elemt.align, cap, loc)
|
||||
|
||||
// NOTE: Might be lying here, but it is at least an allocator error.
|
||||
if !ok { return false, .Out_Of_Memory }
|
||||
}
|
||||
|
||||
err = _unmarshal_value(d, elem, hdr)
|
||||
err = _unmarshal_value(d, elem, hdr, allocator=allocator, loc=loc)
|
||||
if length == -1 && err == .Break { break }
|
||||
if err != nil { return }
|
||||
|
||||
@@ -469,10 +471,10 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
// Allow generically storing the values array.
|
||||
switch &dst in v {
|
||||
case ^Array:
|
||||
dst = err_conv(_decode_array_ptr(d, add)) or_return
|
||||
dst = err_conv(_decode_array_ptr(d, add, allocator=allocator, loc=loc)) or_return
|
||||
return
|
||||
case Array:
|
||||
dst = err_conv(_decode_array(d, add)) or_return
|
||||
dst = err_conv(_decode_array(d, add, allocator=allocator, loc=loc)) or_return
|
||||
return
|
||||
}
|
||||
|
||||
@@ -480,8 +482,8 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
case reflect.Type_Info_Slice:
|
||||
length, scap := err_conv(_decode_len_container(d, add)) or_return
|
||||
|
||||
data := mem.alloc_bytes_non_zeroed(t.elem.size * scap, t.elem.align) or_return
|
||||
defer if err != nil { mem.free_bytes(data) }
|
||||
data := mem.alloc_bytes_non_zeroed(t.elem.size * scap, t.elem.align, allocator=allocator, loc=loc) or_return
|
||||
defer if err != nil { mem.free_bytes(data, allocator=allocator, loc=loc) }
|
||||
|
||||
da := mem.Raw_Dynamic_Array{raw_data(data), 0, length, context.allocator }
|
||||
|
||||
@@ -489,7 +491,7 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
|
||||
if .Shrink_Excess in d.flags {
|
||||
// Ignoring an error here, but this is not critical to succeed.
|
||||
_ = runtime.__dynamic_array_shrink(&da, t.elem.size, t.elem.align, da.len)
|
||||
_ = runtime.__dynamic_array_shrink(&da, t.elem.size, t.elem.align, da.len, loc=loc)
|
||||
}
|
||||
|
||||
raw := (^mem.Raw_Slice)(v.data)
|
||||
@@ -500,8 +502,8 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
case reflect.Type_Info_Dynamic_Array:
|
||||
length, scap := err_conv(_decode_len_container(d, add)) or_return
|
||||
|
||||
data := mem.alloc_bytes_non_zeroed(t.elem.size * scap, t.elem.align) or_return
|
||||
defer if err != nil { mem.free_bytes(data) }
|
||||
data := mem.alloc_bytes_non_zeroed(t.elem.size * scap, t.elem.align, loc=loc) or_return
|
||||
defer if err != nil { mem.free_bytes(data, allocator=allocator, loc=loc) }
|
||||
|
||||
raw := (^mem.Raw_Dynamic_Array)(v.data)
|
||||
raw.data = raw_data(data)
|
||||
@@ -513,7 +515,7 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
|
||||
if .Shrink_Excess in d.flags {
|
||||
// Ignoring an error here, but this is not critical to succeed.
|
||||
_ = runtime.__dynamic_array_shrink(raw, t.elem.size, t.elem.align, raw.len)
|
||||
_ = runtime.__dynamic_array_shrink(raw, t.elem.size, t.elem.align, raw.len, loc=loc)
|
||||
}
|
||||
return
|
||||
|
||||
@@ -525,7 +527,7 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
return _unsupported(v, hdr)
|
||||
}
|
||||
|
||||
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, length, context.allocator }
|
||||
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, length, allocator }
|
||||
|
||||
out_of_space := assign_array(d, &da, t.elem, length, growable=false) or_return
|
||||
if out_of_space { return _unsupported(v, hdr) }
|
||||
@@ -539,7 +541,7 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
return _unsupported(v, hdr)
|
||||
}
|
||||
|
||||
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, length, context.allocator }
|
||||
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, length, allocator }
|
||||
|
||||
out_of_space := assign_array(d, &da, t.elem, length, growable=false) or_return
|
||||
if out_of_space { return _unsupported(v, hdr) }
|
||||
@@ -553,7 +555,7 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
return _unsupported(v, hdr)
|
||||
}
|
||||
|
||||
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, 2, context.allocator }
|
||||
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, 2, allocator }
|
||||
|
||||
info: ^runtime.Type_Info
|
||||
switch ti.id {
|
||||
@@ -575,7 +577,7 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
return _unsupported(v, hdr)
|
||||
}
|
||||
|
||||
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, 4, context.allocator }
|
||||
da := mem.Raw_Dynamic_Array{rawptr(v.data), 0, 4, allocator }
|
||||
|
||||
info: ^runtime.Type_Info
|
||||
switch ti.id {
|
||||
@@ -593,17 +595,17 @@ _unmarshal_array :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
}
|
||||
}
|
||||
|
||||
_unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add) -> (err: Unmarshal_Error) {
|
||||
_unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add, allocator := context.allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
r := d.reader
|
||||
decode_key :: proc(d: Decoder, v: any, allocator := context.allocator) -> (k: string, err: Unmarshal_Error) {
|
||||
decode_key :: proc(d: Decoder, v: any, allocator := context.allocator, loc := #caller_location) -> (k: string, err: Unmarshal_Error) {
|
||||
entry_hdr := _decode_header(d.reader) or_return
|
||||
entry_maj, entry_add := _header_split(entry_hdr)
|
||||
#partial switch entry_maj {
|
||||
case .Text:
|
||||
k = err_conv(_decode_text(d, entry_add, allocator)) or_return
|
||||
k = err_conv(_decode_text(d, entry_add, allocator=allocator, loc=loc)) or_return
|
||||
return
|
||||
case .Bytes:
|
||||
bytes := err_conv(_decode_bytes(d, entry_add, allocator=allocator)) or_return
|
||||
bytes := err_conv(_decode_bytes(d, entry_add, allocator=allocator, loc=loc)) or_return
|
||||
k = string(bytes)
|
||||
return
|
||||
case:
|
||||
@@ -615,10 +617,10 @@ _unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header,
|
||||
// Allow generically storing the map array.
|
||||
switch &dst in v {
|
||||
case ^Map:
|
||||
dst = err_conv(_decode_map_ptr(d, add)) or_return
|
||||
dst = err_conv(_decode_map_ptr(d, add, allocator=allocator, loc=loc)) or_return
|
||||
return
|
||||
case Map:
|
||||
dst = err_conv(_decode_map(d, add)) or_return
|
||||
dst = err_conv(_decode_map(d, add, allocator=allocator, loc=loc)) or_return
|
||||
return
|
||||
}
|
||||
|
||||
@@ -754,7 +756,7 @@ _unmarshal_map :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header,
|
||||
// Unmarshal into a union, based on the `TAG_OBJECT_TYPE` tag of the spec, it denotes a tag which
|
||||
// contains an array of exactly two elements, the first is a textual representation of the following
|
||||
// CBOR value's type.
|
||||
_unmarshal_union :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header) -> (err: Unmarshal_Error) {
|
||||
_unmarshal_union :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, loc := #caller_location) -> (err: Unmarshal_Error) {
|
||||
r := d.reader
|
||||
#partial switch t in ti.variant {
|
||||
case reflect.Type_Info_Union:
|
||||
@@ -792,7 +794,7 @@ _unmarshal_union :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
case reflect.Type_Info_Named:
|
||||
if vti.name == target_name {
|
||||
reflect.set_union_variant_raw_tag(v, tag)
|
||||
return _unmarshal_value(d, any{v.data, variant.id}, _decode_header(r) or_return)
|
||||
return _unmarshal_value(d, any{v.data, variant.id}, _decode_header(r) or_return, loc=loc)
|
||||
}
|
||||
|
||||
case:
|
||||
@@ -804,7 +806,7 @@ _unmarshal_union :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header
|
||||
|
||||
if variant_name == target_name {
|
||||
reflect.set_union_variant_raw_tag(v, tag)
|
||||
return _unmarshal_value(d, any{v.data, variant.id}, _decode_header(r) or_return)
|
||||
return _unmarshal_value(d, any{v.data, variant.id}, _decode_header(r) or_return, loc=loc)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ package encoding_hex
|
||||
|
||||
import "core:strings"
|
||||
|
||||
encode :: proc(src: []byte, allocator := context.allocator) -> []byte #no_bounds_check {
|
||||
dst := make([]byte, len(src) * 2, allocator)
|
||||
encode :: proc(src: []byte, allocator := context.allocator, loc := #caller_location) -> []byte #no_bounds_check {
|
||||
dst := make([]byte, len(src) * 2, allocator, loc)
|
||||
for i, j := 0, 0; i < len(src); i += 1 {
|
||||
v := src[i]
|
||||
dst[j] = HEXTABLE[v>>4]
|
||||
@@ -15,12 +15,12 @@ encode :: proc(src: []byte, allocator := context.allocator) -> []byte #no_bounds
|
||||
}
|
||||
|
||||
|
||||
decode :: proc(src: []byte, allocator := context.allocator) -> (dst: []byte, ok: bool) #no_bounds_check {
|
||||
decode :: proc(src: []byte, allocator := context.allocator, loc := #caller_location) -> (dst: []byte, ok: bool) #no_bounds_check {
|
||||
if len(src) % 2 == 1 {
|
||||
return
|
||||
}
|
||||
|
||||
dst = make([]byte, len(src) / 2, allocator)
|
||||
dst = make([]byte, len(src) / 2, allocator, loc)
|
||||
for i, j := 0, 1; j < len(src); j += 2 {
|
||||
p := src[j-1]
|
||||
q := src[j]
|
||||
@@ -69,5 +69,4 @@ hex_digit :: proc(char: byte) -> (u8, bool) {
|
||||
case 'A' ..= 'F': return char - 'A' + 10, true
|
||||
case: return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+16
-15
@@ -160,34 +160,35 @@ CONVENTION_SOFT_TRANSFORM :: "transform"
|
||||
|
||||
/* destroy procedures */
|
||||
|
||||
meta_destroy :: proc(meta: Meta, allocator := context.allocator) {
|
||||
meta_destroy :: proc(meta: Meta, allocator := context.allocator, loc := #caller_location) {
|
||||
if nested, ok := meta.value.([]Meta); ok {
|
||||
for m in nested {
|
||||
meta_destroy(m)
|
||||
meta_destroy(m, loc=loc)
|
||||
}
|
||||
delete(nested, allocator)
|
||||
delete(nested, allocator, loc=loc)
|
||||
}
|
||||
}
|
||||
nodes_destroy :: proc(nodes: []Node, allocator := context.allocator) {
|
||||
nodes_destroy :: proc(nodes: []Node, allocator := context.allocator, loc := #caller_location) {
|
||||
for node in nodes {
|
||||
for meta in node.meta_data {
|
||||
meta_destroy(meta)
|
||||
meta_destroy(meta, loc=loc)
|
||||
}
|
||||
delete(node.meta_data, allocator)
|
||||
delete(node.meta_data, allocator, loc=loc)
|
||||
|
||||
switch n in node.content {
|
||||
case Node_Geometry:
|
||||
delete(n.corner_stack, allocator)
|
||||
delete(n.edge_stack, allocator)
|
||||
delete(n.face_stack, allocator)
|
||||
delete(n.corner_stack, allocator, loc=loc)
|
||||
delete(n.vertex_stack, allocator, loc=loc)
|
||||
delete(n.edge_stack, allocator, loc=loc)
|
||||
delete(n.face_stack, allocator, loc=loc)
|
||||
case Node_Image:
|
||||
delete(n.image_stack, allocator)
|
||||
delete(n.image_stack, allocator, loc=loc)
|
||||
}
|
||||
}
|
||||
delete(nodes, allocator)
|
||||
delete(nodes, allocator, loc=loc)
|
||||
}
|
||||
|
||||
file_destroy :: proc(file: File) {
|
||||
nodes_destroy(file.nodes, file.allocator)
|
||||
delete(file.backing, file.allocator)
|
||||
}
|
||||
file_destroy :: proc(file: File, loc := #caller_location) {
|
||||
nodes_destroy(file.nodes, file.allocator, loc=loc)
|
||||
delete(file.backing, file.allocator, loc=loc)
|
||||
}
|
||||
+20
-22
@@ -11,24 +11,21 @@ Read_Error :: enum {
|
||||
Unable_To_Read_File,
|
||||
}
|
||||
|
||||
read_from_file :: proc(filename: string, print_error := false, allocator := context.allocator) -> (file: File, err: Read_Error) {
|
||||
read_from_file :: proc(filename: string, print_error := false, allocator := context.allocator, loc := #caller_location) -> (file: File, err: Read_Error) {
|
||||
context.allocator = allocator
|
||||
|
||||
data, ok := os.read_entire_file(filename)
|
||||
data, ok := os.read_entire_file(filename, allocator, loc)
|
||||
if !ok {
|
||||
err = .Unable_To_Read_File
|
||||
delete(data, allocator, loc)
|
||||
return
|
||||
}
|
||||
defer if !ok {
|
||||
delete(data)
|
||||
} else {
|
||||
file.backing = data
|
||||
}
|
||||
file, err = read(data, filename, print_error, allocator)
|
||||
file, err = read(data, filename, print_error, allocator, loc)
|
||||
file.backing = data
|
||||
return
|
||||
}
|
||||
|
||||
read :: proc(data: []byte, filename := "<input>", print_error := false, allocator := context.allocator) -> (file: File, err: Read_Error) {
|
||||
read :: proc(data: []byte, filename := "<input>", print_error := false, allocator := context.allocator, loc := #caller_location) -> (file: File, err: Read_Error) {
|
||||
Reader :: struct {
|
||||
filename: string,
|
||||
data: []byte,
|
||||
@@ -79,8 +76,8 @@ read :: proc(data: []byte, filename := "<input>", print_error := false, allocato
|
||||
return string(data[:len]), nil
|
||||
}
|
||||
|
||||
read_meta :: proc(r: ^Reader, capacity: u32le) -> (meta_data: []Meta, err: Read_Error) {
|
||||
meta_data = make([]Meta, int(capacity))
|
||||
read_meta :: proc(r: ^Reader, capacity: u32le, allocator := context.allocator, loc := #caller_location) -> (meta_data: []Meta, err: Read_Error) {
|
||||
meta_data = make([]Meta, int(capacity), allocator=allocator)
|
||||
count := 0
|
||||
defer meta_data = meta_data[:count]
|
||||
for &m in meta_data {
|
||||
@@ -111,10 +108,10 @@ read :: proc(data: []byte, filename := "<input>", print_error := false, allocato
|
||||
return
|
||||
}
|
||||
|
||||
read_layer_stack :: proc(r: ^Reader, capacity: u32le) -> (layers: Layer_Stack, err: Read_Error) {
|
||||
read_layer_stack :: proc(r: ^Reader, capacity: u32le, allocator := context.allocator, loc := #caller_location) -> (layers: Layer_Stack, err: Read_Error) {
|
||||
stack_count := read_value(r, u32le) or_return
|
||||
layer_count := 0
|
||||
layers = make(Layer_Stack, stack_count)
|
||||
layers = make(Layer_Stack, stack_count, allocator=allocator, loc=loc)
|
||||
defer layers = layers[:layer_count]
|
||||
for &layer in layers {
|
||||
layer.name = read_name(r) or_return
|
||||
@@ -170,7 +167,8 @@ read :: proc(data: []byte, filename := "<input>", print_error := false, allocato
|
||||
|
||||
node_count := 0
|
||||
file.header = header^
|
||||
file.nodes = make([]Node, header.internal_node_count)
|
||||
file.nodes = make([]Node, header.internal_node_count, allocator=allocator, loc=loc)
|
||||
file.allocator = allocator
|
||||
defer if err != nil {
|
||||
nodes_destroy(file.nodes)
|
||||
file.nodes = nil
|
||||
@@ -198,15 +196,15 @@ read :: proc(data: []byte, filename := "<input>", print_error := false, allocato
|
||||
case .Geometry:
|
||||
g: Node_Geometry
|
||||
|
||||
g.vertex_count = read_value(r, u32le) or_return
|
||||
g.vertex_stack = read_layer_stack(r, g.vertex_count) or_return
|
||||
g.edge_corner_count = read_value(r, u32le) or_return
|
||||
g.corner_stack = read_layer_stack(r, g.edge_corner_count) or_return
|
||||
g.vertex_count = read_value(r, u32le) or_return
|
||||
g.vertex_stack = read_layer_stack(r, g.vertex_count, loc=loc) or_return
|
||||
g.edge_corner_count = read_value(r, u32le) or_return
|
||||
g.corner_stack = read_layer_stack(r, g.edge_corner_count, loc=loc) or_return
|
||||
if header.version > 2 {
|
||||
g.edge_stack = read_layer_stack(r, g.edge_corner_count) or_return
|
||||
g.edge_stack = read_layer_stack(r, g.edge_corner_count, loc=loc) or_return
|
||||
}
|
||||
g.face_count = read_value(r, u32le) or_return
|
||||
g.face_stack = read_layer_stack(r, g.face_count) or_return
|
||||
g.face_count = read_value(r, u32le) or_return
|
||||
g.face_stack = read_layer_stack(r, g.face_count, loc=loc) or_return
|
||||
|
||||
node.content = g
|
||||
|
||||
@@ -233,4 +231,4 @@ read :: proc(data: []byte, filename := "<input>", print_error := false, allocato
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -62,8 +62,8 @@ Marshal_Options :: struct {
|
||||
mjson_skipped_first_braces_end: bool,
|
||||
}
|
||||
|
||||
marshal :: proc(v: any, opt: Marshal_Options = {}, allocator := context.allocator) -> (data: []byte, err: Marshal_Error) {
|
||||
b := strings.builder_make(allocator)
|
||||
marshal :: proc(v: any, opt: Marshal_Options = {}, allocator := context.allocator, loc := #caller_location) -> (data: []byte, err: Marshal_Error) {
|
||||
b := strings.builder_make(allocator, loc)
|
||||
defer if err != nil {
|
||||
strings.builder_destroy(&b)
|
||||
}
|
||||
|
||||
@@ -28,27 +28,27 @@ make_parser_from_string :: proc(data: string, spec := DEFAULT_SPECIFICATION, par
|
||||
}
|
||||
|
||||
|
||||
parse :: proc(data: []byte, spec := DEFAULT_SPECIFICATION, parse_integers := false, allocator := context.allocator) -> (Value, Error) {
|
||||
return parse_string(string(data), spec, parse_integers, allocator)
|
||||
parse :: proc(data: []byte, spec := DEFAULT_SPECIFICATION, parse_integers := false, allocator := context.allocator, loc := #caller_location) -> (Value, Error) {
|
||||
return parse_string(string(data), spec, parse_integers, allocator, loc)
|
||||
}
|
||||
|
||||
parse_string :: proc(data: string, spec := DEFAULT_SPECIFICATION, parse_integers := false, allocator := context.allocator) -> (Value, Error) {
|
||||
parse_string :: proc(data: string, spec := DEFAULT_SPECIFICATION, parse_integers := false, allocator := context.allocator, loc := #caller_location) -> (Value, Error) {
|
||||
context.allocator = allocator
|
||||
p := make_parser_from_string(data, spec, parse_integers, allocator)
|
||||
|
||||
switch p.spec {
|
||||
case .JSON:
|
||||
return parse_object(&p)
|
||||
return parse_object(&p, loc)
|
||||
case .JSON5:
|
||||
return parse_value(&p)
|
||||
return parse_value(&p, loc)
|
||||
case .SJSON:
|
||||
#partial switch p.curr_token.kind {
|
||||
case .Ident, .String:
|
||||
return parse_object_body(&p, .EOF)
|
||||
return parse_object_body(&p, .EOF, loc)
|
||||
}
|
||||
return parse_value(&p)
|
||||
return parse_value(&p, loc)
|
||||
}
|
||||
return parse_object(&p)
|
||||
return parse_object(&p, loc)
|
||||
}
|
||||
|
||||
token_end_pos :: proc(tok: Token) -> Pos {
|
||||
@@ -106,7 +106,7 @@ parse_comma :: proc(p: ^Parser) -> (do_break: bool) {
|
||||
return false
|
||||
}
|
||||
|
||||
parse_value :: proc(p: ^Parser) -> (value: Value, err: Error) {
|
||||
parse_value :: proc(p: ^Parser, loc := #caller_location) -> (value: Value, err: Error) {
|
||||
err = .None
|
||||
token := p.curr_token
|
||||
#partial switch token.kind {
|
||||
@@ -142,13 +142,13 @@ parse_value :: proc(p: ^Parser) -> (value: Value, err: Error) {
|
||||
|
||||
case .String:
|
||||
advance_token(p)
|
||||
return unquote_string(token, p.spec, p.allocator)
|
||||
return unquote_string(token, p.spec, p.allocator, loc)
|
||||
|
||||
case .Open_Brace:
|
||||
return parse_object(p)
|
||||
return parse_object(p, loc)
|
||||
|
||||
case .Open_Bracket:
|
||||
return parse_array(p)
|
||||
return parse_array(p, loc)
|
||||
|
||||
case:
|
||||
if p.spec != .JSON {
|
||||
@@ -176,7 +176,7 @@ parse_value :: proc(p: ^Parser) -> (value: Value, err: Error) {
|
||||
return
|
||||
}
|
||||
|
||||
parse_array :: proc(p: ^Parser) -> (value: Value, err: Error) {
|
||||
parse_array :: proc(p: ^Parser, loc := #caller_location) -> (value: Value, err: Error) {
|
||||
err = .None
|
||||
expect_token(p, .Open_Bracket) or_return
|
||||
|
||||
@@ -184,14 +184,14 @@ parse_array :: proc(p: ^Parser) -> (value: Value, err: Error) {
|
||||
array.allocator = p.allocator
|
||||
defer if err != nil {
|
||||
for elem in array {
|
||||
destroy_value(elem)
|
||||
destroy_value(elem, loc=loc)
|
||||
}
|
||||
delete(array)
|
||||
delete(array, loc)
|
||||
}
|
||||
|
||||
for p.curr_token.kind != .Close_Bracket {
|
||||
elem := parse_value(p) or_return
|
||||
append(&array, elem)
|
||||
elem := parse_value(p, loc) or_return
|
||||
append(&array, elem, loc)
|
||||
|
||||
if parse_comma(p) {
|
||||
break
|
||||
@@ -228,38 +228,39 @@ clone_string :: proc(s: string, allocator: mem.Allocator, loc := #caller_locatio
|
||||
return
|
||||
}
|
||||
|
||||
parse_object_key :: proc(p: ^Parser, key_allocator: mem.Allocator) -> (key: string, err: Error) {
|
||||
parse_object_key :: proc(p: ^Parser, key_allocator: mem.Allocator, loc := #caller_location) -> (key: string, err: Error) {
|
||||
tok := p.curr_token
|
||||
if p.spec != .JSON {
|
||||
if allow_token(p, .Ident) {
|
||||
return clone_string(tok.text, key_allocator)
|
||||
return clone_string(tok.text, key_allocator, loc)
|
||||
}
|
||||
}
|
||||
if tok_err := expect_token(p, .String); tok_err != nil {
|
||||
err = .Expected_String_For_Object_Key
|
||||
return
|
||||
}
|
||||
return unquote_string(tok, p.spec, key_allocator)
|
||||
return unquote_string(tok, p.spec, key_allocator, loc)
|
||||
}
|
||||
|
||||
parse_object_body :: proc(p: ^Parser, end_token: Token_Kind) -> (obj: Object, err: Error) {
|
||||
obj.allocator = p.allocator
|
||||
parse_object_body :: proc(p: ^Parser, end_token: Token_Kind, loc := #caller_location) -> (obj: Object, err: Error) {
|
||||
obj = make(Object, allocator=p.allocator, loc=loc)
|
||||
|
||||
defer if err != nil {
|
||||
for key, elem in obj {
|
||||
delete(key, p.allocator)
|
||||
destroy_value(elem)
|
||||
delete(key, p.allocator, loc)
|
||||
destroy_value(elem, loc=loc)
|
||||
}
|
||||
delete(obj)
|
||||
delete(obj, loc)
|
||||
}
|
||||
|
||||
for p.curr_token.kind != end_token {
|
||||
key := parse_object_key(p, p.allocator) or_return
|
||||
key := parse_object_key(p, p.allocator, loc) or_return
|
||||
parse_colon(p) or_return
|
||||
elem := parse_value(p) or_return
|
||||
elem := parse_value(p, loc) or_return
|
||||
|
||||
if key in obj {
|
||||
err = .Duplicate_Object_Key
|
||||
delete(key, p.allocator)
|
||||
delete(key, p.allocator, loc)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -267,7 +268,7 @@ parse_object_body :: proc(p: ^Parser, end_token: Token_Kind) -> (obj: Object, er
|
||||
// inserting empty key/values into the object and for those we do not
|
||||
// want to allocate anything
|
||||
if key != "" {
|
||||
reserve_error := reserve(&obj, len(obj) + 1)
|
||||
reserve_error := reserve(&obj, len(obj) + 1, loc)
|
||||
if reserve_error == mem.Allocator_Error.Out_Of_Memory {
|
||||
return nil, .Out_Of_Memory
|
||||
}
|
||||
@@ -281,9 +282,9 @@ parse_object_body :: proc(p: ^Parser, end_token: Token_Kind) -> (obj: Object, er
|
||||
return obj, .None
|
||||
}
|
||||
|
||||
parse_object :: proc(p: ^Parser) -> (value: Value, err: Error) {
|
||||
parse_object :: proc(p: ^Parser, loc := #caller_location) -> (value: Value, err: Error) {
|
||||
expect_token(p, .Open_Brace) or_return
|
||||
obj := parse_object_body(p, .Close_Brace) or_return
|
||||
obj := parse_object_body(p, .Close_Brace, loc) or_return
|
||||
expect_token(p, .Close_Brace) or_return
|
||||
return obj, .None
|
||||
}
|
||||
@@ -480,4 +481,4 @@ unquote_string :: proc(token: Token, spec: Specification, allocator := context.a
|
||||
}
|
||||
|
||||
return string(b[:w]), nil
|
||||
}
|
||||
}
|
||||
@@ -89,22 +89,22 @@ Error :: enum {
|
||||
|
||||
|
||||
|
||||
destroy_value :: proc(value: Value, allocator := context.allocator) {
|
||||
destroy_value :: proc(value: Value, allocator := context.allocator, loc := #caller_location) {
|
||||
context.allocator = allocator
|
||||
#partial switch v in value {
|
||||
case Object:
|
||||
for key, elem in v {
|
||||
delete(key)
|
||||
destroy_value(elem)
|
||||
delete(key, loc=loc)
|
||||
destroy_value(elem, loc=loc)
|
||||
}
|
||||
delete(v)
|
||||
delete(v, loc=loc)
|
||||
case Array:
|
||||
for elem in v {
|
||||
destroy_value(elem)
|
||||
destroy_value(elem, loc=loc)
|
||||
}
|
||||
delete(v)
|
||||
delete(v, loc=loc)
|
||||
case String:
|
||||
delete(v)
|
||||
delete(v, loc=loc)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+20
-4
@@ -2,6 +2,7 @@ package fmt
|
||||
|
||||
import "base:intrinsics"
|
||||
import "base:runtime"
|
||||
import "core:math"
|
||||
import "core:math/bits"
|
||||
import "core:mem"
|
||||
import "core:io"
|
||||
@@ -2968,6 +2969,21 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) {
|
||||
fmt_bit_field(fi, v, verb, info, "")
|
||||
}
|
||||
}
|
||||
// This proc helps keep some of the code around whether or not to print an
|
||||
// intermediate plus sign in complexes and quaternions more readable.
|
||||
@(private)
|
||||
_cq_should_print_intermediate_plus :: proc "contextless" (fi: ^Info, f: f64) -> bool {
|
||||
if !fi.plus && f >= 0 {
|
||||
#partial switch math.classify(f) {
|
||||
case .Neg_Zero, .Inf:
|
||||
// These two classes print their own signs.
|
||||
return false
|
||||
case:
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
// Formats a complex number based on the given formatting verb
|
||||
//
|
||||
// Inputs:
|
||||
@@ -2981,7 +2997,7 @@ fmt_complex :: proc(fi: ^Info, c: complex128, bits: int, verb: rune) {
|
||||
case 'f', 'F', 'v', 'h', 'H', 'w':
|
||||
r, i := real(c), imag(c)
|
||||
fmt_float(fi, r, bits/2, verb)
|
||||
if !fi.plus && i >= 0 {
|
||||
if _cq_should_print_intermediate_plus(fi, i) {
|
||||
io.write_rune(fi.writer, '+', &fi.n)
|
||||
}
|
||||
fmt_float(fi, i, bits/2, verb)
|
||||
@@ -3007,19 +3023,19 @@ fmt_quaternion :: proc(fi: ^Info, q: quaternion256, bits: int, verb: rune) {
|
||||
|
||||
fmt_float(fi, r, bits/4, verb)
|
||||
|
||||
if !fi.plus && i >= 0 {
|
||||
if _cq_should_print_intermediate_plus(fi, i) {
|
||||
io.write_rune(fi.writer, '+', &fi.n)
|
||||
}
|
||||
fmt_float(fi, i, bits/4, verb)
|
||||
io.write_rune(fi.writer, 'i', &fi.n)
|
||||
|
||||
if !fi.plus && j >= 0 {
|
||||
if _cq_should_print_intermediate_plus(fi, j) {
|
||||
io.write_rune(fi.writer, '+', &fi.n)
|
||||
}
|
||||
fmt_float(fi, j, bits/4, verb)
|
||||
io.write_rune(fi.writer, 'j', &fi.n)
|
||||
|
||||
if !fi.plus && k >= 0 {
|
||||
if _cq_should_print_intermediate_plus(fi, k) {
|
||||
io.write_rune(fi.writer, '+', &fi.n)
|
||||
}
|
||||
fmt_float(fi, k, bits/4, verb)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//+build !freestanding
|
||||
package log
|
||||
|
||||
import "core:encoding/ansi"
|
||||
import "core:fmt"
|
||||
import "core:strings"
|
||||
import "core:os"
|
||||
@@ -70,18 +71,10 @@ file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string
|
||||
backing: [1024]byte //NOTE(Hoej): 1024 might be too much for a header backing, unless somebody has really long paths.
|
||||
buf := strings.builder_from_bytes(backing[:])
|
||||
|
||||
do_level_header(options, level, &buf)
|
||||
do_level_header(options, &buf, level)
|
||||
|
||||
when time.IS_SUPPORTED {
|
||||
if Full_Timestamp_Opts & options != nil {
|
||||
fmt.sbprint(&buf, "[")
|
||||
t := time.now()
|
||||
y, m, d := time.date(t)
|
||||
h, min, s := time.clock(t)
|
||||
if .Date in options { fmt.sbprintf(&buf, "%d-%02d-%02d ", y, m, d) }
|
||||
if .Time in options { fmt.sbprintf(&buf, "%02d:%02d:%02d", h, min, s) }
|
||||
fmt.sbprint(&buf, "] ")
|
||||
}
|
||||
do_time_header(options, &buf, time.now())
|
||||
}
|
||||
|
||||
do_location_header(options, &buf, location)
|
||||
@@ -99,12 +92,12 @@ file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string
|
||||
fmt.fprintf(h, "%s%s\n", strings.to_string(buf), text)
|
||||
}
|
||||
|
||||
do_level_header :: proc(opts: Options, level: Level, str: ^strings.Builder) {
|
||||
do_level_header :: proc(opts: Options, str: ^strings.Builder, level: Level) {
|
||||
|
||||
RESET :: "\x1b[0m"
|
||||
RED :: "\x1b[31m"
|
||||
YELLOW :: "\x1b[33m"
|
||||
DARK_GREY :: "\x1b[90m"
|
||||
RESET :: ansi.CSI + ansi.RESET + ansi.SGR
|
||||
RED :: ansi.CSI + ansi.FG_RED + ansi.SGR
|
||||
YELLOW :: ansi.CSI + ansi.FG_YELLOW + ansi.SGR
|
||||
DARK_GREY :: ansi.CSI + ansi.FG_BRIGHT_BLACK + ansi.SGR
|
||||
|
||||
col := RESET
|
||||
switch level {
|
||||
@@ -125,6 +118,24 @@ do_level_header :: proc(opts: Options, level: Level, str: ^strings.Builder) {
|
||||
}
|
||||
}
|
||||
|
||||
do_time_header :: proc(opts: Options, buf: ^strings.Builder, t: time.Time) {
|
||||
when time.IS_SUPPORTED {
|
||||
if Full_Timestamp_Opts & opts != nil {
|
||||
fmt.sbprint(buf, "[")
|
||||
y, m, d := time.date(t)
|
||||
h, min, s := time.clock(t)
|
||||
if .Date in opts {
|
||||
fmt.sbprintf(buf, "%d-%02d-%02d", y, m, d)
|
||||
if .Time in opts {
|
||||
fmt.sbprint(buf, " ")
|
||||
}
|
||||
}
|
||||
if .Time in opts { fmt.sbprintf(buf, "%02d:%02d:%02d", h, min, s) }
|
||||
fmt.sbprint(buf, "] ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
do_location_header :: proc(opts: Options, buf: ^strings.Builder, location := #caller_location) {
|
||||
if Location_Header_Opts & opts == nil {
|
||||
return
|
||||
|
||||
@@ -3,6 +3,7 @@ package linalg
|
||||
import "core:math"
|
||||
import "base:builtin"
|
||||
import "base:intrinsics"
|
||||
import "base:runtime"
|
||||
|
||||
// Generic
|
||||
|
||||
@@ -223,33 +224,27 @@ quaternion_mul_quaternion :: proc "contextless" (q1, q2: $Q) -> Q where IS_QUATE
|
||||
|
||||
@(require_results)
|
||||
quaternion64_mul_vector3 :: proc "contextless" (q: $Q/quaternion64, v: $V/[3]$F/f16) -> V {
|
||||
Raw_Quaternion :: struct {xyz: [3]f16, r: f16}
|
||||
|
||||
q := transmute(Raw_Quaternion)q
|
||||
q := transmute(runtime.Raw_Quaternion64_Vector_Scalar)q
|
||||
v := v
|
||||
|
||||
t := cross(2*q.xyz, v)
|
||||
return V(v + q.r*t + cross(q.xyz, t))
|
||||
t := cross(2*q.vector, v)
|
||||
return V(v + q.scalar*t + cross(q.vector, t))
|
||||
}
|
||||
@(require_results)
|
||||
quaternion128_mul_vector3 :: proc "contextless" (q: $Q/quaternion128, v: $V/[3]$F/f32) -> V {
|
||||
Raw_Quaternion :: struct {xyz: [3]f32, r: f32}
|
||||
|
||||
q := transmute(Raw_Quaternion)q
|
||||
q := transmute(runtime.Raw_Quaternion128_Vector_Scalar)q
|
||||
v := v
|
||||
|
||||
t := cross(2*q.xyz, v)
|
||||
return V(v + q.r*t + cross(q.xyz, t))
|
||||
t := cross(2*q.vector, v)
|
||||
return V(v + q.scalar*t + cross(q.vector, t))
|
||||
}
|
||||
@(require_results)
|
||||
quaternion256_mul_vector3 :: proc "contextless" (q: $Q/quaternion256, v: $V/[3]$F/f64) -> V {
|
||||
Raw_Quaternion :: struct {xyz: [3]f64, r: f64}
|
||||
|
||||
q := transmute(Raw_Quaternion)q
|
||||
q := transmute(runtime.Raw_Quaternion256_Vector_Scalar)q
|
||||
v := v
|
||||
|
||||
t := cross(2*q.xyz, v)
|
||||
return V(v + q.r*t + cross(q.xyz, t))
|
||||
t := cross(2*q.vector, v)
|
||||
return V(v + q.scalar*t + cross(q.vector, t))
|
||||
}
|
||||
quaternion_mul_vector3 :: proc{quaternion64_mul_vector3, quaternion128_mul_vector3, quaternion256_mul_vector3}
|
||||
|
||||
|
||||
+9
-6
@@ -11,12 +11,15 @@ Raw_Dynamic_Array :: runtime.Raw_Dynamic_Array
|
||||
Raw_Map :: runtime.Raw_Map
|
||||
Raw_Soa_Pointer :: runtime.Raw_Soa_Pointer
|
||||
|
||||
Raw_Complex64 :: struct {real, imag: f32}
|
||||
Raw_Complex128 :: struct {real, imag: f64}
|
||||
Raw_Quaternion128 :: struct {imag, jmag, kmag: f32, real: f32}
|
||||
Raw_Quaternion256 :: struct {imag, jmag, kmag: f64, real: f64}
|
||||
Raw_Quaternion128_Vector_Scalar :: struct {vector: [3]f32, scalar: f32}
|
||||
Raw_Quaternion256_Vector_Scalar :: struct {vector: [3]f64, scalar: f64}
|
||||
Raw_Complex32 :: runtime.Raw_Complex32
|
||||
Raw_Complex64 :: runtime.Raw_Complex64
|
||||
Raw_Complex128 :: runtime.Raw_Complex128
|
||||
Raw_Quaternion64 :: runtime.Raw_Quaternion64
|
||||
Raw_Quaternion128 :: runtime.Raw_Quaternion128
|
||||
Raw_Quaternion256 :: runtime.Raw_Quaternion256
|
||||
Raw_Quaternion64_Vector_Scalar :: runtime.Raw_Quaternion64_Vector_Scalar
|
||||
Raw_Quaternion128_Vector_Scalar :: runtime.Raw_Quaternion128_Vector_Scalar
|
||||
Raw_Quaternion256_Vector_Scalar :: runtime.Raw_Quaternion256_Vector_Scalar
|
||||
|
||||
make_any :: proc "contextless" (data: rawptr, id: typeid) -> any {
|
||||
return transmute(any)Raw_Any{data, id}
|
||||
|
||||
@@ -0,0 +1,341 @@
|
||||
package mem
|
||||
|
||||
// The Rollback Stack Allocator was designed for the test runner to be fast,
|
||||
// able to grow, and respect the Tracking Allocator's requirement for
|
||||
// individual frees. It is not overly concerned with fragmentation, however.
|
||||
//
|
||||
// It has support for expansion when configured with a block allocator and
|
||||
// limited support for out-of-order frees.
|
||||
//
|
||||
// Allocation has constant-time best and usual case performance.
|
||||
// At worst, it is linear according to the number of memory blocks.
|
||||
//
|
||||
// Allocation follows a first-fit strategy when there are multiple memory
|
||||
// blocks.
|
||||
//
|
||||
// Freeing has constant-time best and usual case performance.
|
||||
// At worst, it is linear according to the number of memory blocks and number
|
||||
// of freed items preceding the last item in a block.
|
||||
//
|
||||
// Resizing has constant-time performance, if it's the last item in a block, or
|
||||
// the new size is smaller. Naturally, this becomes linear-time if there are
|
||||
// multiple blocks to search for the pointer's owning block. Otherwise, the
|
||||
// allocator defaults to a combined alloc & free operation internally.
|
||||
//
|
||||
// Out-of-order freeing is accomplished by collapsing a run of freed items
|
||||
// from the last allocation backwards.
|
||||
//
|
||||
// Each allocation has an overhead of 8 bytes and any extra bytes to satisfy
|
||||
// the requested alignment.
|
||||
|
||||
import "base:runtime"
|
||||
|
||||
ROLLBACK_STACK_DEFAULT_BLOCK_SIZE :: 4 * Megabyte
|
||||
|
||||
// This limitation is due to the size of `prev_ptr`, but it is only for the
|
||||
// head block; any allocation in excess of the allocator's `block_size` is
|
||||
// valid, so long as the block allocator can handle it.
|
||||
//
|
||||
// This is because allocations over the block size are not split up if the item
|
||||
// within is freed; they are immediately returned to the block allocator.
|
||||
ROLLBACK_STACK_MAX_HEAD_BLOCK_SIZE :: 2 * Gigabyte
|
||||
|
||||
|
||||
Rollback_Stack_Header :: bit_field u64 {
|
||||
prev_offset: uintptr | 32,
|
||||
is_free: bool | 1,
|
||||
prev_ptr: uintptr | 31,
|
||||
}
|
||||
|
||||
Rollback_Stack_Block :: struct {
|
||||
next_block: ^Rollback_Stack_Block,
|
||||
last_alloc: rawptr,
|
||||
offset: uintptr,
|
||||
buffer: []byte,
|
||||
}
|
||||
|
||||
Rollback_Stack :: struct {
|
||||
head: ^Rollback_Stack_Block,
|
||||
block_size: int,
|
||||
block_allocator: Allocator,
|
||||
}
|
||||
|
||||
|
||||
@(private="file", require_results)
|
||||
rb_ptr_in_bounds :: proc(block: ^Rollback_Stack_Block, ptr: rawptr) -> bool {
|
||||
start := raw_data(block.buffer)
|
||||
end := start[block.offset:]
|
||||
return start < ptr && ptr <= end
|
||||
}
|
||||
|
||||
@(private="file", require_results)
|
||||
rb_find_ptr :: proc(stack: ^Rollback_Stack, ptr: rawptr) -> (
|
||||
parent: ^Rollback_Stack_Block,
|
||||
block: ^Rollback_Stack_Block,
|
||||
header: ^Rollback_Stack_Header,
|
||||
err: Allocator_Error,
|
||||
) {
|
||||
for block = stack.head; block != nil; block = block.next_block {
|
||||
if rb_ptr_in_bounds(block, ptr) {
|
||||
header = cast(^Rollback_Stack_Header)(cast(uintptr)ptr - size_of(Rollback_Stack_Header))
|
||||
return
|
||||
}
|
||||
parent = block
|
||||
}
|
||||
return nil, nil, nil, .Invalid_Pointer
|
||||
}
|
||||
|
||||
@(private="file", require_results)
|
||||
rb_find_last_alloc :: proc(stack: ^Rollback_Stack, ptr: rawptr) -> (
|
||||
block: ^Rollback_Stack_Block,
|
||||
header: ^Rollback_Stack_Header,
|
||||
ok: bool,
|
||||
) {
|
||||
for block = stack.head; block != nil; block = block.next_block {
|
||||
if block.last_alloc == ptr {
|
||||
header = cast(^Rollback_Stack_Header)(cast(uintptr)ptr - size_of(Rollback_Stack_Header))
|
||||
return block, header, true
|
||||
}
|
||||
}
|
||||
return nil, nil, false
|
||||
}
|
||||
|
||||
@(private="file")
|
||||
rb_rollback_block :: proc(block: ^Rollback_Stack_Block, header: ^Rollback_Stack_Header) {
|
||||
header := header
|
||||
for block.offset > 0 && header.is_free {
|
||||
block.offset = header.prev_offset
|
||||
block.last_alloc = raw_data(block.buffer)[header.prev_ptr:]
|
||||
header = cast(^Rollback_Stack_Header)(raw_data(block.buffer)[header.prev_ptr - size_of(Rollback_Stack_Header):])
|
||||
}
|
||||
}
|
||||
|
||||
@(private="file", require_results)
|
||||
rb_free :: proc(stack: ^Rollback_Stack, ptr: rawptr) -> Allocator_Error {
|
||||
parent, block, header := rb_find_ptr(stack, ptr) or_return
|
||||
if header.is_free {
|
||||
return .Invalid_Pointer
|
||||
}
|
||||
header.is_free = true
|
||||
if block.last_alloc == ptr {
|
||||
block.offset = header.prev_offset
|
||||
rb_rollback_block(block, header)
|
||||
}
|
||||
if parent != nil && block.offset == 0 {
|
||||
parent.next_block = block.next_block
|
||||
runtime.mem_free_with_size(block, size_of(Rollback_Stack_Block) + len(block.buffer), stack.block_allocator)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@(private="file")
|
||||
rb_free_all :: proc(stack: ^Rollback_Stack) {
|
||||
for block := stack.head.next_block; block != nil; /**/ {
|
||||
next_block := block.next_block
|
||||
runtime.mem_free_with_size(block, size_of(Rollback_Stack_Block) + len(block.buffer), stack.block_allocator)
|
||||
block = next_block
|
||||
}
|
||||
|
||||
stack.head.next_block = nil
|
||||
stack.head.last_alloc = nil
|
||||
stack.head.offset = 0
|
||||
}
|
||||
|
||||
@(private="file", require_results)
|
||||
rb_resize :: proc(stack: ^Rollback_Stack, ptr: rawptr, old_size, size, alignment: int) -> (result: []byte, err: Allocator_Error) {
|
||||
if ptr != nil {
|
||||
if block, _, ok := rb_find_last_alloc(stack, ptr); ok {
|
||||
// `block.offset` should never underflow because it is contingent
|
||||
// on `old_size` in the first place, assuming sane arguments.
|
||||
assert(block.offset >= cast(uintptr)old_size, "Rollback Stack Allocator received invalid `old_size`.")
|
||||
|
||||
if block.offset + cast(uintptr)size - cast(uintptr)old_size < cast(uintptr)len(block.buffer) {
|
||||
// Prevent singleton allocations from fragmenting by forbidding
|
||||
// them to shrink, removing the possibility of overflow bugs.
|
||||
if len(block.buffer) <= stack.block_size {
|
||||
block.offset += cast(uintptr)size - cast(uintptr)old_size
|
||||
}
|
||||
#no_bounds_check return (cast([^]byte)ptr)[:size], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result = rb_alloc(stack, size, alignment) or_return
|
||||
runtime.mem_copy_non_overlapping(raw_data(result), ptr, old_size)
|
||||
err = rb_free(stack, ptr)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@(private="file", require_results)
|
||||
rb_alloc :: proc(stack: ^Rollback_Stack, size, alignment: int) -> (result: []byte, err: Allocator_Error) {
|
||||
parent: ^Rollback_Stack_Block
|
||||
for block := stack.head; /**/; block = block.next_block {
|
||||
when !ODIN_DISABLE_ASSERT {
|
||||
allocated_new_block: bool
|
||||
}
|
||||
|
||||
if block == nil {
|
||||
if stack.block_allocator.procedure == nil {
|
||||
return nil, .Out_Of_Memory
|
||||
}
|
||||
|
||||
minimum_size_required := size_of(Rollback_Stack_Header) + size + alignment - 1
|
||||
new_block_size := max(minimum_size_required, stack.block_size)
|
||||
block = rb_make_block(new_block_size, stack.block_allocator) or_return
|
||||
parent.next_block = block
|
||||
when !ODIN_DISABLE_ASSERT {
|
||||
allocated_new_block = true
|
||||
}
|
||||
}
|
||||
|
||||
start := raw_data(block.buffer)[block.offset:]
|
||||
padding := cast(uintptr)calc_padding_with_header(cast(uintptr)start, cast(uintptr)alignment, size_of(Rollback_Stack_Header))
|
||||
|
||||
if block.offset + padding + cast(uintptr)size > cast(uintptr)len(block.buffer) {
|
||||
when !ODIN_DISABLE_ASSERT {
|
||||
if allocated_new_block {
|
||||
panic("Rollback Stack Allocator allocated a new block but did not use it.")
|
||||
}
|
||||
}
|
||||
parent = block
|
||||
continue
|
||||
}
|
||||
|
||||
header := cast(^Rollback_Stack_Header)(start[padding - size_of(Rollback_Stack_Header):])
|
||||
ptr := start[padding:]
|
||||
|
||||
header^ = {
|
||||
prev_offset = block.offset,
|
||||
prev_ptr = uintptr(0) if block.last_alloc == nil else cast(uintptr)block.last_alloc - cast(uintptr)raw_data(block.buffer),
|
||||
is_free = false,
|
||||
}
|
||||
|
||||
block.last_alloc = ptr
|
||||
block.offset += padding + cast(uintptr)size
|
||||
|
||||
if len(block.buffer) > stack.block_size {
|
||||
// This block exceeds the allocator's standard block size and is considered a singleton.
|
||||
// Prevent any further allocations on it.
|
||||
block.offset = cast(uintptr)len(block.buffer)
|
||||
}
|
||||
|
||||
#no_bounds_check return ptr[:size], nil
|
||||
}
|
||||
|
||||
return nil, .Out_Of_Memory
|
||||
}
|
||||
|
||||
@(private="file", require_results)
|
||||
rb_make_block :: proc(size: int, allocator: Allocator) -> (block: ^Rollback_Stack_Block, err: Allocator_Error) {
|
||||
buffer := runtime.mem_alloc(size_of(Rollback_Stack_Block) + size, align_of(Rollback_Stack_Block), allocator) or_return
|
||||
|
||||
block = cast(^Rollback_Stack_Block)raw_data(buffer)
|
||||
#no_bounds_check block.buffer = buffer[size_of(Rollback_Stack_Block):]
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
rollback_stack_init_buffered :: proc(stack: ^Rollback_Stack, buffer: []byte, location := #caller_location) {
|
||||
MIN_SIZE :: size_of(Rollback_Stack_Block) + size_of(Rollback_Stack_Header) + size_of(rawptr)
|
||||
assert(len(buffer) >= MIN_SIZE, "User-provided buffer to Rollback Stack Allocator is too small.", location)
|
||||
|
||||
block := cast(^Rollback_Stack_Block)raw_data(buffer)
|
||||
block^ = {}
|
||||
#no_bounds_check block.buffer = buffer[size_of(Rollback_Stack_Block):]
|
||||
|
||||
stack^ = {}
|
||||
stack.head = block
|
||||
stack.block_size = len(block.buffer)
|
||||
}
|
||||
|
||||
rollback_stack_init_dynamic :: proc(
|
||||
stack: ^Rollback_Stack,
|
||||
block_size : int = ROLLBACK_STACK_DEFAULT_BLOCK_SIZE,
|
||||
block_allocator := context.allocator,
|
||||
location := #caller_location,
|
||||
) -> Allocator_Error {
|
||||
assert(block_size >= size_of(Rollback_Stack_Header) + size_of(rawptr), "Rollback Stack Allocator block size is too small.", location)
|
||||
when size_of(int) > 4 {
|
||||
// It's impossible to specify an argument in excess when your integer
|
||||
// size is insufficient; check only on platforms with big enough ints.
|
||||
assert(block_size <= ROLLBACK_STACK_MAX_HEAD_BLOCK_SIZE, "Rollback Stack Allocators cannot support head blocks larger than 2 gigabytes.", location)
|
||||
}
|
||||
|
||||
block := rb_make_block(block_size, block_allocator) or_return
|
||||
|
||||
stack^ = {}
|
||||
stack.head = block
|
||||
stack.block_size = block_size
|
||||
stack.block_allocator = block_allocator
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
rollback_stack_init :: proc {
|
||||
rollback_stack_init_buffered,
|
||||
rollback_stack_init_dynamic,
|
||||
}
|
||||
|
||||
rollback_stack_destroy :: proc(stack: ^Rollback_Stack) {
|
||||
if stack.block_allocator.procedure != nil {
|
||||
rb_free_all(stack)
|
||||
free(stack.head, stack.block_allocator)
|
||||
}
|
||||
stack^ = {}
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
rollback_stack_allocator :: proc(stack: ^Rollback_Stack) -> Allocator {
|
||||
return Allocator {
|
||||
data = stack,
|
||||
procedure = rollback_stack_allocator_proc,
|
||||
}
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
rollback_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, location := #caller_location,
|
||||
) -> (result: []byte, err: Allocator_Error) {
|
||||
stack := cast(^Rollback_Stack)allocator_data
|
||||
|
||||
switch mode {
|
||||
case .Alloc, .Alloc_Non_Zeroed:
|
||||
assert(size >= 0, "Size must be positive or zero.", location)
|
||||
assert(is_power_of_two(cast(uintptr)alignment), "Alignment must be a power of two.", location)
|
||||
result = rb_alloc(stack, size, alignment) or_return
|
||||
|
||||
if mode == .Alloc {
|
||||
zero_slice(result)
|
||||
}
|
||||
|
||||
case .Free:
|
||||
err = rb_free(stack, old_memory)
|
||||
|
||||
case .Free_All:
|
||||
rb_free_all(stack)
|
||||
|
||||
case .Resize, .Resize_Non_Zeroed:
|
||||
assert(size >= 0, "Size must be positive or zero.", location)
|
||||
assert(old_size >= 0, "Old size must be positive or zero.", location)
|
||||
assert(is_power_of_two(cast(uintptr)alignment), "Alignment must be a power of two.", location)
|
||||
result = rb_resize(stack, old_memory, old_size, size, alignment) or_return
|
||||
|
||||
#no_bounds_check if mode == .Resize && size > old_size {
|
||||
zero_slice(result[old_size:])
|
||||
}
|
||||
|
||||
case .Query_Features:
|
||||
set := (^Allocator_Mode_Set)(old_memory)
|
||||
if set != nil {
|
||||
set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Free_All, .Resize, .Resize_Non_Zeroed}
|
||||
}
|
||||
return nil, nil
|
||||
|
||||
case .Query_Info:
|
||||
return nil, .Mode_Not_Implemented
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -47,6 +47,7 @@ tracking_allocator_destroy :: proc(t: ^Tracking_Allocator) {
|
||||
}
|
||||
|
||||
|
||||
// Clear only the current allocation data while keeping the totals intact.
|
||||
tracking_allocator_clear :: proc(t: ^Tracking_Allocator) {
|
||||
sync.mutex_lock(&t.mutex)
|
||||
clear(&t.allocation_map)
|
||||
@@ -55,6 +56,19 @@ tracking_allocator_clear :: proc(t: ^Tracking_Allocator) {
|
||||
sync.mutex_unlock(&t.mutex)
|
||||
}
|
||||
|
||||
// Reset all of a Tracking Allocator's allocation data back to zero.
|
||||
tracking_allocator_reset :: proc(t: ^Tracking_Allocator) {
|
||||
sync.mutex_lock(&t.mutex)
|
||||
clear(&t.allocation_map)
|
||||
clear(&t.bad_free_array)
|
||||
t.total_memory_allocated = 0
|
||||
t.total_allocation_count = 0
|
||||
t.total_memory_freed = 0
|
||||
t.total_free_count = 0
|
||||
t.peak_memory_allocated = 0
|
||||
t.current_memory_allocated = 0
|
||||
sync.mutex_unlock(&t.mutex)
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator {
|
||||
|
||||
@@ -753,7 +753,7 @@ Array_Type :: struct {
|
||||
using node: Expr,
|
||||
open: tokenizer.Pos,
|
||||
tag: ^Expr,
|
||||
len: ^Expr, // Ellipsis node for [?]T arrray types, nil for slice types
|
||||
len: ^Expr, // Ellipsis node for [?]T array types, nil for slice types
|
||||
close: tokenizer.Pos,
|
||||
elem: ^Expr,
|
||||
}
|
||||
|
||||
@@ -278,7 +278,9 @@ clone_node :: proc(node: ^Node) -> ^Node {
|
||||
r.foreign_library = clone(r.foreign_library)
|
||||
r.body = clone(r.body)
|
||||
case ^Foreign_Import_Decl:
|
||||
r.attributes = clone_dynamic_array(r.attributes)
|
||||
r.name = auto_cast clone(r.name)
|
||||
r.fullpaths = clone_array(r.fullpaths)
|
||||
case ^Proc_Group:
|
||||
r.args = clone(r.args)
|
||||
case ^Attribute:
|
||||
|
||||
@@ -320,6 +320,7 @@ walk :: proc(v: ^Visitor, node: ^Node) {
|
||||
if n.comment != nil {
|
||||
walk(v, n.comment)
|
||||
}
|
||||
walk_expr_list(v, n.fullpaths)
|
||||
|
||||
case ^Proc_Group:
|
||||
walk_expr_list(v, n.args)
|
||||
|
||||
@@ -1204,7 +1204,7 @@ parse_foreign_decl :: proc(p: ^Parser) -> ^ast.Decl {
|
||||
path := expect_token(p, .String)
|
||||
reserve(&fullpaths, 1)
|
||||
bl := ast.new(ast.Basic_Lit, path.pos, end_pos(path))
|
||||
bl.tok = tok
|
||||
bl.tok = path
|
||||
append(&fullpaths, bl)
|
||||
}
|
||||
|
||||
|
||||
@@ -442,7 +442,7 @@ F_GETPATH :: 50 // return the full path of the fd
|
||||
foreign libc {
|
||||
@(link_name="__error") __error :: proc() -> ^c.int ---
|
||||
|
||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, mode: u16) -> Handle ---
|
||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, #c_vararg args: ..any) -> Handle ---
|
||||
@(link_name="close") _unix_close :: proc(handle: Handle) -> c.int ---
|
||||
@(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: c.size_t) -> int ---
|
||||
@(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: c.size_t) -> int ---
|
||||
|
||||
@@ -5,7 +5,6 @@ foreign import libc "system:c"
|
||||
|
||||
import "base:runtime"
|
||||
import "core:strings"
|
||||
import "core:sys/unix"
|
||||
import "core:c"
|
||||
|
||||
Handle :: distinct i32
|
||||
@@ -328,6 +327,11 @@ foreign dl {
|
||||
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
|
||||
}
|
||||
|
||||
@(private)
|
||||
foreign libc {
|
||||
_lwp_self :: proc() -> i32 ---
|
||||
}
|
||||
|
||||
// NOTE(phix): Perhaps share the following functions with FreeBSD if they turn out to be the same in the end.
|
||||
|
||||
is_path_separator :: proc(r: rune) -> bool {
|
||||
@@ -721,7 +725,7 @@ exit :: proc "contextless" (code: int) -> ! {
|
||||
}
|
||||
|
||||
current_thread_id :: proc "contextless" () -> int {
|
||||
return cast(int) unix.pthread_self()
|
||||
return int(_lwp_self())
|
||||
}
|
||||
|
||||
dlopen :: proc(filename: string, flags: int) -> rawptr {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
//+build i386, amd64
|
||||
package simd_x86
|
||||
|
||||
@(require_results, enable_target_feature = "aes")
|
||||
_mm_aesdec :: #force_inline proc "c" (a, b: __m128i) -> __m128i {
|
||||
return aesdec(a, b)
|
||||
}
|
||||
|
||||
@(require_results, enable_target_feature = "aes")
|
||||
_mm_aesdeclast :: #force_inline proc "c" (a, b: __m128i) -> __m128i {
|
||||
return aesdeclast(a, b)
|
||||
}
|
||||
|
||||
@(require_results, enable_target_feature = "aes")
|
||||
_mm_aesenc :: #force_inline proc "c" (a, b: __m128i) -> __m128i {
|
||||
return aesenc(a, b)
|
||||
}
|
||||
|
||||
@(require_results, enable_target_feature = "aes")
|
||||
_mm_aesenclast :: #force_inline proc "c" (a, b: __m128i) -> __m128i {
|
||||
return aesenclast(a, b)
|
||||
}
|
||||
|
||||
@(require_results, enable_target_feature = "aes")
|
||||
_mm_aesimc :: #force_inline proc "c" (a: __m128i) -> __m128i {
|
||||
return aesimc(a)
|
||||
}
|
||||
|
||||
@(require_results, enable_target_feature = "aes")
|
||||
_mm_aeskeygenassist :: #force_inline proc "c" (a: __m128i, $IMM8: u8) -> __m128i {
|
||||
return aeskeygenassist(a, u8(IMM8))
|
||||
}
|
||||
|
||||
|
||||
@(private, default_calling_convention = "none")
|
||||
foreign _ {
|
||||
@(link_name = "llvm.x86.aesni.aesdec")
|
||||
aesdec :: proc(a, b: __m128i) -> __m128i ---
|
||||
@(link_name = "llvm.x86.aesni.aesdeclast")
|
||||
aesdeclast :: proc(a, b: __m128i) -> __m128i ---
|
||||
@(link_name = "llvm.x86.aesni.aesenc")
|
||||
aesenc :: proc(a, b: __m128i) -> __m128i ---
|
||||
@(link_name = "llvm.x86.aesni.aesenclast")
|
||||
aesenclast :: proc(a, b: __m128i) -> __m128i ---
|
||||
@(link_name = "llvm.x86.aesni.aesimc")
|
||||
aesimc :: proc(a: __m128i) -> __m128i ---
|
||||
@(link_name = "llvm.x86.aesni.aeskeygenassist")
|
||||
aeskeygenassist :: proc(a: __m128i, imm8: u8) -> __m128i ---
|
||||
}
|
||||
+283
-7
@@ -835,17 +835,21 @@ Example:
|
||||
|
||||
n, _, ok = strconv.parse_f64_prefix("12.34e2")
|
||||
fmt.printfln("%.3f %v", n, ok)
|
||||
|
||||
n, _, ok = strconv.parse_f64_prefix("13.37 hellope")
|
||||
fmt.printfln("%.3f %v", n, ok)
|
||||
}
|
||||
|
||||
Output:
|
||||
|
||||
0.000 false
|
||||
1234.000 true
|
||||
13.370 true
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 64-bit floating point number.
|
||||
- nr: The length of the parsed substring.
|
||||
- ok: `false` if a base 10 float could not be found, or if the input string contained more than just the number.
|
||||
- ok: `false` if a base 10 float could not be found
|
||||
*/
|
||||
parse_f64_prefix :: proc(str: string) -> (value: f64, nr: int, ok: bool) {
|
||||
common_prefix_len_ignore_case :: proc "contextless" (s, prefix: string) -> int {
|
||||
@@ -878,13 +882,16 @@ parse_f64_prefix :: proc(str: string) -> (value: f64, nr: int, ok: bool) {
|
||||
s = s[1:]
|
||||
fallthrough
|
||||
case 'i', 'I':
|
||||
n = common_prefix_len_ignore_case(s, "infinity")
|
||||
if 3 < n && n < 8 { // "inf" or "infinity"
|
||||
n = 3
|
||||
}
|
||||
if n == 3 || n == 8 {
|
||||
m := common_prefix_len_ignore_case(s, "infinity")
|
||||
if 3 <= m && m < 9 { // "inf" to "infinity"
|
||||
f = 0h7ff00000_00000000 if sign == 1 else 0hfff00000_00000000
|
||||
n = nsign + 3
|
||||
if m == 8 {
|
||||
// We only count the entire prefix if it is precisely "infinity".
|
||||
n = nsign + m
|
||||
} else {
|
||||
// The string was either only "inf" or incomplete.
|
||||
n = nsign + 3
|
||||
}
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
@@ -1124,6 +1131,275 @@ parse_f64_prefix :: proc(str: string) -> (value: f64, nr: int, ok: bool) {
|
||||
ok = !overflow
|
||||
return
|
||||
}
|
||||
/*
|
||||
Parses a 128-bit complex number from a string
|
||||
|
||||
**Inputs**
|
||||
- str: The input string containing a 128-bit complex number.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
parse_complex128_example :: proc() {
|
||||
n: int
|
||||
c, ok := strconv.parse_complex128("3+1i", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
|
||||
c, ok = strconv.parse_complex128("5+7i hellope", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
}
|
||||
|
||||
Output:
|
||||
|
||||
3+1i 4 true
|
||||
5+7i 4 false
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 128-bit complex number.
|
||||
- ok: `false` if a complex number could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
parse_complex128 :: proc(str: string, n: ^int = nil) -> (value: complex128, ok: bool) {
|
||||
real_value, imag_value: f64
|
||||
nr_r, nr_i: int
|
||||
|
||||
real_value, nr_r, _ = parse_f64_prefix(str)
|
||||
imag_value, nr_i, _ = parse_f64_prefix(str[nr_r:])
|
||||
|
||||
i_parsed := len(str) >= nr_r + nr_i + 1 && str[nr_r + nr_i] == 'i'
|
||||
if !i_parsed {
|
||||
// No `i` means we refuse to treat the second float we parsed as an
|
||||
// imaginary value.
|
||||
imag_value = 0
|
||||
nr_i = 0
|
||||
}
|
||||
|
||||
ok = i_parsed && len(str) == nr_r + nr_i + 1
|
||||
|
||||
if n != nil {
|
||||
n^ = nr_r + nr_i + (1 if i_parsed else 0)
|
||||
}
|
||||
|
||||
value = complex(real_value, imag_value)
|
||||
return
|
||||
}
|
||||
/*
|
||||
Parses a 64-bit complex number from a string
|
||||
|
||||
**Inputs**
|
||||
- str: The input string containing a 64-bit complex number.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
parse_complex64_example :: proc() {
|
||||
n: int
|
||||
c, ok := strconv.parse_complex64("3+1i", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
|
||||
c, ok = strconv.parse_complex64("5+7i hellope", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
}
|
||||
|
||||
Output:
|
||||
|
||||
3+1i 4 true
|
||||
5+7i 4 false
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 64-bit complex number.
|
||||
- ok: `false` if a complex number could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
parse_complex64 :: proc(str: string, n: ^int = nil) -> (value: complex64, ok: bool) {
|
||||
v: complex128 = ---
|
||||
v, ok = parse_complex128(str, n)
|
||||
return cast(complex64)v, ok
|
||||
}
|
||||
/*
|
||||
Parses a 32-bit complex number from a string
|
||||
|
||||
**Inputs**
|
||||
- str: The input string containing a 32-bit complex number.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
parse_complex32_example :: proc() {
|
||||
n: int
|
||||
c, ok := strconv.parse_complex32("3+1i", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
|
||||
c, ok = strconv.parse_complex32("5+7i hellope", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
}
|
||||
|
||||
Output:
|
||||
|
||||
3+1i 4 true
|
||||
5+7i 4 false
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 32-bit complex number.
|
||||
- ok: `false` if a complex number could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
parse_complex32 :: proc(str: string, n: ^int = nil) -> (value: complex32, ok: bool) {
|
||||
v: complex128 = ---
|
||||
v, ok = parse_complex128(str, n)
|
||||
return cast(complex32)v, ok
|
||||
}
|
||||
/*
|
||||
Parses a 256-bit quaternion from a string
|
||||
|
||||
**Inputs**
|
||||
- str: The input string containing a 256-bit quaternion.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
parse_quaternion256_example :: proc() {
|
||||
n: int
|
||||
q, ok := strconv.parse_quaternion256("1+2i+3j+4k", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
|
||||
q, ok = strconv.parse_quaternion256("1+2i+3j+4k hellope", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
}
|
||||
|
||||
Output:
|
||||
|
||||
1+2i+3j+4k 10 true
|
||||
1+2i+3j+4k 10 false
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 256-bit quaternion.
|
||||
- ok: `false` if a quaternion could not be found, or if the input string contained more than just the quaternion.
|
||||
*/
|
||||
parse_quaternion256 :: proc(str: string, n: ^int = nil) -> (value: quaternion256, ok: bool) {
|
||||
iterate_and_assign :: proc (iter: ^string, terminator: byte, nr_total: ^int, state: bool) -> (value: f64, ok: bool) {
|
||||
if !state {
|
||||
return
|
||||
}
|
||||
|
||||
nr: int
|
||||
value, nr, _ = parse_f64_prefix(iter^)
|
||||
iter^ = iter[nr:]
|
||||
|
||||
if len(iter) > 0 && iter[0] == terminator {
|
||||
iter^ = iter[1:]
|
||||
nr_total^ += nr + 1
|
||||
ok = true
|
||||
} else {
|
||||
value = 0
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
real_value, imag_value, jmag_value, kmag_value: f64
|
||||
nr: int
|
||||
|
||||
real_value, nr, _ = parse_f64_prefix(str)
|
||||
iter := str[nr:]
|
||||
|
||||
// Need to have parsed at least something in order to get started.
|
||||
ok = nr > 0
|
||||
|
||||
// Quaternion parsing is done this way to honour the rest of the API with
|
||||
// regards to partial parsing. Otherwise, we could error out early.
|
||||
imag_value, ok = iterate_and_assign(&iter, 'i', &nr, ok)
|
||||
jmag_value, ok = iterate_and_assign(&iter, 'j', &nr, ok)
|
||||
kmag_value, ok = iterate_and_assign(&iter, 'k', &nr, ok)
|
||||
|
||||
if len(iter) != 0 {
|
||||
ok = false
|
||||
}
|
||||
|
||||
if n != nil {
|
||||
n^ = nr
|
||||
}
|
||||
|
||||
value = quaternion(
|
||||
real = real_value,
|
||||
imag = imag_value,
|
||||
jmag = jmag_value,
|
||||
kmag = kmag_value)
|
||||
return
|
||||
}
|
||||
/*
|
||||
Parses a 128-bit quaternion from a string
|
||||
|
||||
**Inputs**
|
||||
- str: The input string containing a 128-bit quaternion.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
parse_quaternion128_example :: proc() {
|
||||
n: int
|
||||
q, ok := strconv.parse_quaternion128("1+2i+3j+4k", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
|
||||
q, ok = strconv.parse_quaternion128("1+2i+3j+4k hellope", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
}
|
||||
|
||||
Output:
|
||||
|
||||
1+2i+3j+4k 10 true
|
||||
1+2i+3j+4k 10 false
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 128-bit quaternion.
|
||||
- ok: `false` if a quaternion could not be found, or if the input string contained more than just the quaternion.
|
||||
*/
|
||||
parse_quaternion128 :: proc(str: string, n: ^int = nil) -> (value: quaternion128, ok: bool) {
|
||||
v: quaternion256 = ---
|
||||
v, ok = parse_quaternion256(str, n)
|
||||
return cast(quaternion128)v, ok
|
||||
}
|
||||
/*
|
||||
Parses a 64-bit quaternion from a string
|
||||
|
||||
**Inputs**
|
||||
- str: The input string containing a 64-bit quaternion.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
parse_quaternion64_example :: proc() {
|
||||
n: int
|
||||
q, ok := strconv.parse_quaternion64("1+2i+3j+4k", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
|
||||
q, ok = strconv.parse_quaternion64("1+2i+3j+4k hellope", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
}
|
||||
|
||||
Output:
|
||||
|
||||
1+2i+3j+4k 10 true
|
||||
1+2i+3j+4k 10 false
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 64-bit quaternion.
|
||||
- ok: `false` if a quaternion could not be found, or if the input string contained more than just the quaternion.
|
||||
*/
|
||||
parse_quaternion64 :: proc(str: string, n: ^int = nil) -> (value: quaternion64, ok: bool) {
|
||||
v: quaternion256 = ---
|
||||
v, ok = parse_quaternion256(str, n)
|
||||
return cast(quaternion64)v, ok
|
||||
}
|
||||
/*
|
||||
Appends a boolean value as a string to the given buffer
|
||||
|
||||
|
||||
@@ -350,9 +350,9 @@ Output:
|
||||
ab
|
||||
|
||||
*/
|
||||
write_byte :: proc(b: ^Builder, x: byte) -> (n: int) {
|
||||
write_byte :: proc(b: ^Builder, x: byte, loc := #caller_location) -> (n: int) {
|
||||
n0 := len(b.buf)
|
||||
append(&b.buf, x)
|
||||
append(&b.buf, x, loc)
|
||||
n1 := len(b.buf)
|
||||
return n1-n0
|
||||
}
|
||||
@@ -380,9 +380,9 @@ NOTE: The backing dynamic array may be fixed in capacity or fail to resize, `n`
|
||||
Returns:
|
||||
- n: The number of bytes appended
|
||||
*/
|
||||
write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) {
|
||||
write_bytes :: proc(b: ^Builder, x: []byte, loc := #caller_location) -> (n: int) {
|
||||
n0 := len(b.buf)
|
||||
append(&b.buf, ..x)
|
||||
append(&b.buf, ..x, loc=loc)
|
||||
n1 := len(b.buf)
|
||||
return n1-n0
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
//+private
|
||||
package sync
|
||||
|
||||
import "core:sys/unix"
|
||||
foreign import libc "system:c"
|
||||
|
||||
foreign libc {
|
||||
_lwp_self :: proc "c" () -> i32 ---
|
||||
}
|
||||
|
||||
_current_thread_id :: proc "contextless" () -> int {
|
||||
return cast(int) unix.pthread_self()
|
||||
return int(_lwp_self())
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ sem_t :: struct {
|
||||
PTHREAD_CANCEL_ENABLE :: 0
|
||||
PTHREAD_CANCEL_DISABLE :: 1
|
||||
PTHREAD_CANCEL_DEFERRED :: 0
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS :: 2
|
||||
|
||||
foreign import "system:pthread"
|
||||
|
||||
@@ -119,4 +119,4 @@ foreign pthread {
|
||||
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
|
||||
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
|
||||
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ sem_t :: distinct rawptr
|
||||
PTHREAD_CANCEL_ENABLE :: 0
|
||||
PTHREAD_CANCEL_DISABLE :: 1
|
||||
PTHREAD_CANCEL_DEFERRED :: 0
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS :: 1
|
||||
PTHREAD_CANCEL_ASYNCHRONOUS :: 2
|
||||
|
||||
foreign import libc "system:c"
|
||||
|
||||
@@ -71,4 +71,4 @@ foreign libc {
|
||||
pthread_setcancelstate :: proc (state: c.int, old_state: ^c.int) -> c.int ---
|
||||
pthread_setcanceltype :: proc (type: c.int, old_type: ^c.int) -> c.int ---
|
||||
pthread_cancel :: proc (thread: pthread_t) -> c.int ---
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,4 +116,5 @@ foreign pthread {
|
||||
pthread_mutexattr_setpshared :: proc(attrs: ^pthread_mutexattr_t, value: c.int) -> c.int ---
|
||||
pthread_mutexattr_getpshared :: proc(attrs: ^pthread_mutexattr_t, result: ^c.int) -> c.int ---
|
||||
|
||||
pthread_testcancel :: proc () ---
|
||||
}
|
||||
|
||||
@@ -453,9 +453,9 @@ foreign kernel32 {
|
||||
// [MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setfilecompletionnotificationmodes)
|
||||
SetFileCompletionNotificationModes :: proc(FileHandle: HANDLE, Flags: u8) -> BOOL ---
|
||||
// [MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-createiocompletionport)
|
||||
CreateIoCompletionPort :: proc(FileHandle: HANDLE, ExistingCompletionPort: HANDLE, CompletionKey: ^uintptr, NumberOfConcurrentThreads: DWORD) -> HANDLE ---
|
||||
CreateIoCompletionPort :: proc(FileHandle: HANDLE, ExistingCompletionPort: HANDLE, CompletionKey: ULONG_PTR, NumberOfConcurrentThreads: DWORD) -> HANDLE ---
|
||||
//[MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-getqueuedcompletionstatus)
|
||||
GetQueuedCompletionStatus :: proc(CompletionPort: HANDLE, lpNumberOfBytesTransferred: ^DWORD, lpCompletionKey: uintptr, lpOverlapped: ^^OVERLAPPED, dwMilliseconds: DWORD) -> BOOL ---
|
||||
GetQueuedCompletionStatus :: proc(CompletionPort: HANDLE, lpNumberOfBytesTransferred: ^DWORD, lpCompletionKey: PULONG_PTR, lpOverlapped: ^^OVERLAPPED, dwMilliseconds: DWORD) -> BOOL ---
|
||||
// [MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-getqueuedcompletionstatusex)
|
||||
GetQueuedCompletionStatusEx :: proc(CompletionPort: HANDLE, lpCompletionPortEntries: ^OVERLAPPED_ENTRY, ulCount: c_ulong, ulNumEntriesRemoved: ^c_ulong, dwMilliseconds: DWORD, fAlertable: BOOL) -> BOOL ---
|
||||
// [MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-postqueuedcompletionstatus)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
//+private
|
||||
package testing
|
||||
|
||||
import "base:runtime"
|
||||
import "core:sync/chan"
|
||||
import "core:time"
|
||||
|
||||
Test_State :: enum {
|
||||
Ready,
|
||||
Running,
|
||||
Successful,
|
||||
Failed,
|
||||
}
|
||||
|
||||
Update_Channel :: chan.Chan(Channel_Event)
|
||||
Update_Channel_Sender :: chan.Chan(Channel_Event, .Send)
|
||||
|
||||
Task_Channel :: struct {
|
||||
channel: Update_Channel,
|
||||
test_index: int,
|
||||
}
|
||||
|
||||
Event_New_Test :: struct {
|
||||
test_index: int,
|
||||
}
|
||||
|
||||
Event_State_Change :: struct {
|
||||
new_state: Test_State,
|
||||
}
|
||||
|
||||
Event_Set_Fail_Timeout :: struct {
|
||||
at_time: time.Time,
|
||||
location: runtime.Source_Code_Location,
|
||||
}
|
||||
|
||||
Event_Log_Message :: struct {
|
||||
level: runtime.Logger_Level,
|
||||
text: string,
|
||||
time: time.Time,
|
||||
formatted_text: string,
|
||||
}
|
||||
|
||||
Channel_Event :: union {
|
||||
Event_New_Test,
|
||||
Event_State_Change,
|
||||
Event_Set_Fail_Timeout,
|
||||
Event_Log_Message,
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
//+private
|
||||
package testing
|
||||
|
||||
import "base:runtime"
|
||||
import "core:fmt"
|
||||
import pkg_log "core:log"
|
||||
import "core:strings"
|
||||
import "core:sync/chan"
|
||||
import "core:time"
|
||||
|
||||
Default_Test_Logger_Opts :: runtime.Logger_Options {
|
||||
.Level,
|
||||
.Terminal_Color,
|
||||
.Short_File_Path,
|
||||
.Line,
|
||||
.Procedure,
|
||||
.Date, .Time,
|
||||
}
|
||||
|
||||
Log_Message :: struct {
|
||||
level: runtime.Logger_Level,
|
||||
text: string,
|
||||
time: time.Time,
|
||||
// `text` may be allocated differently, depending on where a log message
|
||||
// originates from.
|
||||
allocator: runtime.Allocator,
|
||||
}
|
||||
|
||||
test_logger_proc :: proc(logger_data: rawptr, level: runtime.Logger_Level, text: string, options: runtime.Logger_Options, location := #caller_location) {
|
||||
t := cast(^T)logger_data
|
||||
|
||||
if level >= .Error {
|
||||
t.error_count += 1
|
||||
}
|
||||
|
||||
cloned_text, clone_error := strings.clone(text, t._log_allocator)
|
||||
assert(clone_error == nil, "Error while cloning string in test thread logger proc.")
|
||||
|
||||
now := time.now()
|
||||
|
||||
chan.send(t.channel, Event_Log_Message {
|
||||
level = level,
|
||||
text = cloned_text,
|
||||
time = now,
|
||||
formatted_text = format_log_text(level, text, options, location, now, t._log_allocator),
|
||||
})
|
||||
}
|
||||
|
||||
runner_logger_proc :: proc(logger_data: rawptr, level: runtime.Logger_Level, text: string, options: runtime.Logger_Options, location := #caller_location) {
|
||||
log_messages := cast(^[dynamic]Log_Message)logger_data
|
||||
|
||||
now := time.now()
|
||||
|
||||
append(log_messages, Log_Message {
|
||||
level = level,
|
||||
text = format_log_text(level, text, options, location, now),
|
||||
time = now,
|
||||
allocator = context.allocator,
|
||||
})
|
||||
}
|
||||
|
||||
format_log_text :: proc(level: runtime.Logger_Level, text: string, options: runtime.Logger_Options, location: runtime.Source_Code_Location, at_time: time.Time, allocator := context.allocator) -> string{
|
||||
backing: [1024]byte
|
||||
buf := strings.builder_from_bytes(backing[:])
|
||||
|
||||
pkg_log.do_level_header(options, &buf, level)
|
||||
pkg_log.do_time_header(options, &buf, at_time)
|
||||
pkg_log.do_location_header(options, &buf, location)
|
||||
|
||||
return fmt.aprintf("%s%s", strings.to_string(buf), text, allocator = allocator)
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
//+private
|
||||
package testing
|
||||
|
||||
import "base:runtime"
|
||||
import "core:encoding/ansi"
|
||||
import "core:fmt"
|
||||
import "core:io"
|
||||
import "core:mem"
|
||||
import "core:path/filepath"
|
||||
import "core:strings"
|
||||
|
||||
// Definitions of colors for use in the test runner.
|
||||
SGR_RESET :: ansi.CSI + ansi.RESET + ansi.SGR
|
||||
SGR_READY :: ansi.CSI + ansi.FG_BRIGHT_BLACK + ansi.SGR
|
||||
SGR_RUNNING :: ansi.CSI + ansi.FG_YELLOW + ansi.SGR
|
||||
SGR_SUCCESS :: ansi.CSI + ansi.FG_GREEN + ansi.SGR
|
||||
SGR_FAILED :: ansi.CSI + ansi.FG_RED + ansi.SGR
|
||||
|
||||
MAX_PROGRESS_WIDTH :: 100
|
||||
|
||||
// More than enough bytes to cover long package names, long test names, dozens
|
||||
// of ANSI codes, et cetera.
|
||||
LINE_BUFFER_SIZE :: (MAX_PROGRESS_WIDTH * 8 + 224) * runtime.Byte
|
||||
|
||||
PROGRESS_COLUMN_SPACING :: 2
|
||||
|
||||
Package_Run :: struct {
|
||||
name: string,
|
||||
header: string,
|
||||
|
||||
frame_ready: bool,
|
||||
|
||||
redraw_buffer: [LINE_BUFFER_SIZE]byte,
|
||||
redraw_string: string,
|
||||
|
||||
last_change_state: Test_State,
|
||||
last_change_name: string,
|
||||
|
||||
tests: []Internal_Test,
|
||||
test_states: []Test_State,
|
||||
}
|
||||
|
||||
Report :: struct {
|
||||
packages: []Package_Run,
|
||||
packages_by_name: map[string]^Package_Run,
|
||||
|
||||
pkg_column_len: int,
|
||||
test_column_len: int,
|
||||
progress_width: int,
|
||||
|
||||
all_tests: []Internal_Test,
|
||||
all_test_states: []Test_State,
|
||||
}
|
||||
|
||||
// Organize all tests by package and sort out test state data.
|
||||
make_report :: proc(internal_tests: []Internal_Test) -> (report: Report, error: runtime.Allocator_Error) {
|
||||
assert(len(internal_tests) > 0, "make_report called with no tests")
|
||||
|
||||
packages: [dynamic]Package_Run
|
||||
|
||||
report.all_tests = internal_tests
|
||||
report.all_test_states = make([]Test_State, len(internal_tests)) or_return
|
||||
|
||||
// First, figure out what belongs where.
|
||||
#no_bounds_check cur_pkg := internal_tests[0].pkg
|
||||
pkg_start: int
|
||||
|
||||
// This loop assumes the tests are sorted by package already.
|
||||
for it, index in internal_tests {
|
||||
if cur_pkg != it.pkg {
|
||||
#no_bounds_check {
|
||||
append(&packages, Package_Run {
|
||||
name = cur_pkg,
|
||||
tests = report.all_tests[pkg_start:index],
|
||||
test_states = report.all_test_states[pkg_start:index],
|
||||
}) or_return
|
||||
}
|
||||
|
||||
when PROGRESS_WIDTH == 0 {
|
||||
report.progress_width = max(report.progress_width, index - pkg_start)
|
||||
}
|
||||
|
||||
pkg_start = index
|
||||
report.pkg_column_len = max(report.pkg_column_len, len(cur_pkg))
|
||||
cur_pkg = it.pkg
|
||||
}
|
||||
report.test_column_len = max(report.test_column_len, len(it.name))
|
||||
}
|
||||
|
||||
// Handle the last (or only) package.
|
||||
#no_bounds_check {
|
||||
append(&packages, Package_Run {
|
||||
name = cur_pkg,
|
||||
header = cur_pkg,
|
||||
tests = report.all_tests[pkg_start:],
|
||||
test_states = report.all_test_states[pkg_start:],
|
||||
}) or_return
|
||||
}
|
||||
when PROGRESS_WIDTH == 0 {
|
||||
report.progress_width = max(report.progress_width, len(internal_tests) - pkg_start)
|
||||
} else {
|
||||
report.progress_width = PROGRESS_WIDTH
|
||||
}
|
||||
report.progress_width = min(report.progress_width, MAX_PROGRESS_WIDTH)
|
||||
|
||||
report.pkg_column_len = PROGRESS_COLUMN_SPACING + max(report.pkg_column_len, len(cur_pkg))
|
||||
|
||||
shrink(&packages) or_return
|
||||
|
||||
for &pkg in packages {
|
||||
pkg.header = fmt.aprintf("%- *[1]s[", pkg.name, report.pkg_column_len)
|
||||
assert(len(pkg.header) > 0, "Error allocating package header string.")
|
||||
|
||||
// This is safe because the array is done resizing, and it has the same
|
||||
// lifetime as the map.
|
||||
report.packages_by_name[pkg.name] = &pkg
|
||||
}
|
||||
|
||||
// It's okay to discard the dynamic array's allocator information here,
|
||||
// because its capacity has been shrunk to its length, it was allocated by
|
||||
// the caller's context allocator, and it will be deallocated by the same.
|
||||
//
|
||||
// `delete_slice` is equivalent to `delete_dynamic_array` in this case.
|
||||
report.packages = packages[:]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
destroy_report :: proc(report: ^Report) {
|
||||
for pkg in report.packages {
|
||||
delete(pkg.header)
|
||||
}
|
||||
|
||||
delete(report.packages)
|
||||
delete(report.packages_by_name)
|
||||
delete(report.all_test_states)
|
||||
}
|
||||
|
||||
redraw_package :: proc(w: io.Writer, report: Report, pkg: ^Package_Run) {
|
||||
if pkg.frame_ready {
|
||||
io.write_string(w, pkg.redraw_string)
|
||||
return
|
||||
}
|
||||
|
||||
// Write the output line here so we can cache it.
|
||||
line_builder := strings.builder_from_bytes(pkg.redraw_buffer[:])
|
||||
line_writer := strings.to_writer(&line_builder)
|
||||
|
||||
highest_run_index: int
|
||||
failed_count: int
|
||||
done_count: int
|
||||
#no_bounds_check for i := 0; i < len(pkg.test_states); i += 1 {
|
||||
switch pkg.test_states[i] {
|
||||
case .Ready:
|
||||
continue
|
||||
case .Running:
|
||||
highest_run_index = max(highest_run_index, i)
|
||||
case .Successful:
|
||||
done_count += 1
|
||||
case .Failed:
|
||||
failed_count += 1
|
||||
done_count += 1
|
||||
}
|
||||
}
|
||||
|
||||
start := max(0, highest_run_index - (report.progress_width - 1))
|
||||
end := min(start + report.progress_width, len(pkg.test_states))
|
||||
|
||||
// This variable is to keep track of the last ANSI code emitted, in
|
||||
// order to avoid repeating the same code over in a sequence.
|
||||
//
|
||||
// This should help reduce screen flicker.
|
||||
last_state := Test_State(-1)
|
||||
|
||||
io.write_string(line_writer, pkg.header)
|
||||
|
||||
#no_bounds_check for state in pkg.test_states[start:end] {
|
||||
switch state {
|
||||
case .Ready:
|
||||
if last_state != state {
|
||||
io.write_string(line_writer, SGR_READY)
|
||||
last_state = state
|
||||
}
|
||||
case .Running:
|
||||
if last_state != state {
|
||||
io.write_string(line_writer, SGR_RUNNING)
|
||||
last_state = state
|
||||
}
|
||||
case .Successful:
|
||||
if last_state != state {
|
||||
io.write_string(line_writer, SGR_SUCCESS)
|
||||
last_state = state
|
||||
}
|
||||
case .Failed:
|
||||
if last_state != state {
|
||||
io.write_string(line_writer, SGR_FAILED)
|
||||
last_state = state
|
||||
}
|
||||
}
|
||||
io.write_byte(line_writer, '|')
|
||||
}
|
||||
|
||||
for _ in 0 ..< report.progress_width - (end - start) {
|
||||
io.write_byte(line_writer, ' ')
|
||||
}
|
||||
|
||||
io.write_string(line_writer, SGR_RESET + "] ")
|
||||
|
||||
ticker: string
|
||||
if done_count == len(pkg.test_states) {
|
||||
ticker = "[package done]"
|
||||
if failed_count > 0 {
|
||||
ticker = fmt.tprintf("%s (" + SGR_FAILED + "%i" + SGR_RESET + " failed)", ticker, failed_count)
|
||||
}
|
||||
} else {
|
||||
if len(pkg.last_change_name) == 0 {
|
||||
#no_bounds_check pkg.last_change_name = pkg.tests[0].name
|
||||
}
|
||||
|
||||
switch pkg.last_change_state {
|
||||
case .Ready:
|
||||
ticker = fmt.tprintf(SGR_READY + "%s" + SGR_RESET, pkg.last_change_name)
|
||||
case .Running:
|
||||
ticker = fmt.tprintf(SGR_RUNNING + "%s" + SGR_RESET, pkg.last_change_name)
|
||||
case .Failed:
|
||||
ticker = fmt.tprintf(SGR_FAILED + "%s" + SGR_RESET, pkg.last_change_name)
|
||||
case .Successful:
|
||||
ticker = fmt.tprintf(SGR_SUCCESS + "%s" + SGR_RESET, pkg.last_change_name)
|
||||
}
|
||||
}
|
||||
|
||||
if done_count == len(pkg.test_states) {
|
||||
fmt.wprintfln(line_writer, " % 4i :: %s",
|
||||
len(pkg.test_states),
|
||||
ticker,
|
||||
)
|
||||
} else {
|
||||
fmt.wprintfln(line_writer, "% 4i/% 4i :: %s",
|
||||
done_count,
|
||||
len(pkg.test_states),
|
||||
ticker,
|
||||
)
|
||||
}
|
||||
|
||||
pkg.redraw_string = strings.to_string(line_builder)
|
||||
pkg.frame_ready = true
|
||||
io.write_string(w, pkg.redraw_string)
|
||||
}
|
||||
|
||||
redraw_report :: proc(w: io.Writer, report: Report) {
|
||||
// If we print a line longer than the user's terminal can handle, it may
|
||||
// wrap around, shifting the progress report out of alignment.
|
||||
//
|
||||
// There are ways to get the current terminal width, and that would be the
|
||||
// ideal way to handle this, but it would require system-specific code such
|
||||
// as setting STDIN to be non-blocking in order to read the response from
|
||||
// the ANSI DSR escape code, or reading environment variables.
|
||||
//
|
||||
// The DECAWM escape codes control whether or not the terminal will wrap
|
||||
// long lines or overwrite the last visible character.
|
||||
// This should be fine for now.
|
||||
//
|
||||
// Note that we only do this for the animated summary; log messages are
|
||||
// still perfectly fine to wrap, as they're printed in their own batch,
|
||||
// whereas the animation depends on each package being only on one line.
|
||||
//
|
||||
// Of course, if you resize your terminal while it's printing, things can
|
||||
// still break...
|
||||
fmt.wprint(w, ansi.CSI + ansi.DECAWM_OFF)
|
||||
for &pkg in report.packages {
|
||||
redraw_package(w, report, &pkg)
|
||||
}
|
||||
fmt.wprint(w, ansi.CSI + ansi.DECAWM_ON)
|
||||
}
|
||||
|
||||
needs_to_redraw :: proc(report: Report) -> bool {
|
||||
for pkg in report.packages {
|
||||
if !pkg.frame_ready {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
draw_status_bar :: proc(w: io.Writer, threads_string: string, total_done_count, total_test_count: int) {
|
||||
if total_done_count == total_test_count {
|
||||
// All tests are done; print a blank line to maintain the same height
|
||||
// of the progress report.
|
||||
fmt.wprintln(w)
|
||||
} else {
|
||||
fmt.wprintfln(w,
|
||||
"%s % 4i/% 4i :: total",
|
||||
threads_string,
|
||||
total_done_count,
|
||||
total_test_count)
|
||||
}
|
||||
}
|
||||
|
||||
write_memory_report :: proc(w: io.Writer, tracker: ^mem.Tracking_Allocator, pkg, name: string) {
|
||||
fmt.wprintf(w,
|
||||
"<% 10M/% 10M> <% 10M> (% 5i/% 5i) :: %s.%s",
|
||||
tracker.current_memory_allocated,
|
||||
tracker.total_memory_allocated,
|
||||
tracker.peak_memory_allocated,
|
||||
tracker.total_free_count,
|
||||
tracker.total_allocation_count,
|
||||
pkg,
|
||||
name)
|
||||
|
||||
for ptr, entry in tracker.allocation_map {
|
||||
fmt.wprintf(w,
|
||||
"\n +++ leak % 10M @ %p [%s:%i:%s()]",
|
||||
entry.size,
|
||||
ptr,
|
||||
filepath.base(entry.location.file_path),
|
||||
entry.location.line,
|
||||
entry.location.procedure)
|
||||
}
|
||||
|
||||
for entry in tracker.bad_free_array {
|
||||
fmt.wprintf(w,
|
||||
"\n +++ bad free @ %p [%s:%i:%s()]",
|
||||
entry.memory,
|
||||
filepath.base(entry.location.file_path),
|
||||
entry.location.line,
|
||||
entry.location.procedure)
|
||||
}
|
||||
}
|
||||
+791
-44
@@ -1,73 +1,820 @@
|
||||
//+private
|
||||
package testing
|
||||
|
||||
import "base:intrinsics"
|
||||
import "base:runtime"
|
||||
import "core:bytes"
|
||||
import "core:encoding/ansi"
|
||||
@require import "core:encoding/base64"
|
||||
import "core:fmt"
|
||||
import "core:io"
|
||||
@require import pkg_log "core:log"
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
import "core:slice"
|
||||
@require import "core:strings"
|
||||
import "core:sync/chan"
|
||||
import "core:thread"
|
||||
import "core:time"
|
||||
|
||||
reset_t :: proc(t: ^T) {
|
||||
clear(&t.cleanups)
|
||||
t.error_count = 0
|
||||
// Specify how many threads to use when running tests.
|
||||
TEST_THREADS : int : #config(ODIN_TEST_THREADS, 0)
|
||||
// Track the memory used by each test.
|
||||
TRACKING_MEMORY : bool : #config(ODIN_TEST_TRACK_MEMORY, true)
|
||||
// Always report how much memory is used, even when there are no leaks or bad frees.
|
||||
ALWAYS_REPORT_MEMORY : bool : #config(ODIN_TEST_ALWAYS_REPORT_MEMORY, false)
|
||||
// Specify how much memory each thread allocator starts with.
|
||||
PER_THREAD_MEMORY : int : #config(ODIN_TEST_THREAD_MEMORY, mem.ROLLBACK_STACK_DEFAULT_BLOCK_SIZE)
|
||||
// Select a specific set of tests to run by name.
|
||||
// Each test is separated by a comma and may optionally include the package name.
|
||||
// This may be useful when running tests on multiple packages with `-all-packages`.
|
||||
// The format is: `package.test_name,test_name_only,...`
|
||||
TEST_NAMES : string : #config(ODIN_TEST_NAMES, "")
|
||||
// Show the fancy animated progress report.
|
||||
FANCY_OUTPUT : bool : #config(ODIN_TEST_FANCY, true)
|
||||
// Copy failed tests to the clipboard when done.
|
||||
USE_CLIPBOARD : bool : #config(ODIN_TEST_CLIPBOARD, false)
|
||||
// How many test results to show at a time per package.
|
||||
PROGRESS_WIDTH : int : #config(ODIN_TEST_PROGRESS_WIDTH, 24)
|
||||
// This is the random seed that will be sent to each test.
|
||||
// If it is unspecified, it will be set to the system cycle counter at startup.
|
||||
SHARED_RANDOM_SEED : u64 : #config(ODIN_TEST_RANDOM_SEED, 0)
|
||||
// Set the lowest log level for this test run.
|
||||
LOG_LEVEL : string : #config(ODIN_TEST_LOG_LEVEL, "info")
|
||||
|
||||
|
||||
get_log_level :: #force_inline proc() -> runtime.Logger_Level {
|
||||
when ODIN_DEBUG {
|
||||
// Always use .Debug in `-debug` mode.
|
||||
return .Debug
|
||||
} else {
|
||||
when LOG_LEVEL == "debug" { return .Debug }
|
||||
else when LOG_LEVEL == "info" { return .Info }
|
||||
else when LOG_LEVEL == "warning" { return .Warning }
|
||||
else when LOG_LEVEL == "error" { return .Error }
|
||||
else when LOG_LEVEL == "fatal" { return .Fatal }
|
||||
}
|
||||
}
|
||||
|
||||
end_t :: proc(t: ^T) {
|
||||
for i := len(t.cleanups)-1; i >= 0; i -= 1 {
|
||||
c := t.cleanups[i]
|
||||
#no_bounds_check c := t.cleanups[i]
|
||||
context = c.ctx
|
||||
c.procedure(c.user_data)
|
||||
}
|
||||
|
||||
delete(t.cleanups)
|
||||
t.cleanups = {}
|
||||
}
|
||||
|
||||
Task_Data :: struct {
|
||||
it: Internal_Test,
|
||||
t: T,
|
||||
allocator_index: int,
|
||||
}
|
||||
|
||||
Task_Timeout :: struct {
|
||||
test_index: int,
|
||||
at_time: time.Time,
|
||||
location: runtime.Source_Code_Location,
|
||||
}
|
||||
|
||||
run_test_task :: proc(task: thread.Task) {
|
||||
data := cast(^Task_Data)(task.data)
|
||||
|
||||
setup_task_signal_handler(task.user_index)
|
||||
|
||||
chan.send(data.t.channel, Event_New_Test {
|
||||
test_index = task.user_index,
|
||||
})
|
||||
|
||||
chan.send(data.t.channel, Event_State_Change {
|
||||
new_state = .Running,
|
||||
})
|
||||
|
||||
context.assertion_failure_proc = test_assertion_failure_proc
|
||||
|
||||
context.logger = {
|
||||
procedure = test_logger_proc,
|
||||
data = &data.t,
|
||||
lowest_level = get_log_level(),
|
||||
options = Default_Test_Logger_Opts,
|
||||
}
|
||||
|
||||
free_all(context.temp_allocator)
|
||||
|
||||
data.it.p(&data.t)
|
||||
|
||||
end_t(&data.t)
|
||||
|
||||
new_state : Test_State = .Failed if failed(&data.t) else .Successful
|
||||
|
||||
chan.send(data.t.channel, Event_State_Change {
|
||||
new_state = new_state,
|
||||
})
|
||||
}
|
||||
|
||||
runner :: proc(internal_tests: []Internal_Test) -> bool {
|
||||
stream := os.stream_from_handle(os.stdout)
|
||||
w := io.to_writer(stream)
|
||||
BATCH_BUFFER_SIZE :: 32 * mem.Kilobyte
|
||||
POOL_BLOCK_SIZE :: 16 * mem.Kilobyte
|
||||
CLIPBOARD_BUFFER_SIZE :: 16 * mem.Kilobyte
|
||||
|
||||
t := &T{}
|
||||
t.w = w
|
||||
reserve(&t.cleanups, 1024)
|
||||
defer delete(t.cleanups)
|
||||
BUFFERED_EVENTS_PER_CHANNEL :: 16
|
||||
RESERVED_LOG_MESSAGES :: 64
|
||||
RESERVED_TEST_FAILURES :: 64
|
||||
|
||||
total_success_count := 0
|
||||
total_test_count := len(internal_tests)
|
||||
ERROR_STRING_TIMEOUT : string : "Test timed out."
|
||||
ERROR_STRING_UNKNOWN : string : "Test failed for unknown reasons."
|
||||
OSC_WINDOW_TITLE : string : ansi.OSC + ansi.WINDOW_TITLE + ";Odin test runner (%i/%i)" + ansi.ST
|
||||
|
||||
slice.sort_by(internal_tests, proc(a, b: Internal_Test) -> bool {
|
||||
if a.pkg < b.pkg {
|
||||
return true
|
||||
safe_delete_string :: proc(s: string, allocator := context.allocator) {
|
||||
// Guard against bad frees on static strings.
|
||||
switch raw_data(s) {
|
||||
case raw_data(ERROR_STRING_TIMEOUT), raw_data(ERROR_STRING_UNKNOWN):
|
||||
return
|
||||
case:
|
||||
delete(s, allocator)
|
||||
}
|
||||
return a.name < b.name
|
||||
})
|
||||
}
|
||||
|
||||
prev_pkg := ""
|
||||
stdout := io.to_writer(os.stream_from_handle(os.stdout))
|
||||
stderr := io.to_writer(os.stream_from_handle(os.stderr))
|
||||
|
||||
// -- Prepare test data.
|
||||
|
||||
alloc_error: mem.Allocator_Error
|
||||
|
||||
when TEST_NAMES != "" {
|
||||
select_internal_tests: [dynamic]Internal_Test
|
||||
defer delete(select_internal_tests)
|
||||
|
||||
{
|
||||
index_list := TEST_NAMES
|
||||
for selector in strings.split_iterator(&index_list, ",") {
|
||||
// Temp allocator is fine since we just need to identify which test it's referring to.
|
||||
split_selector := strings.split(selector, ".", context.temp_allocator)
|
||||
|
||||
found := false
|
||||
switch len(split_selector) {
|
||||
case 1:
|
||||
// Only the test name?
|
||||
#no_bounds_check name := split_selector[0]
|
||||
find_test_by_name: for it in internal_tests {
|
||||
if it.name == name {
|
||||
found = true
|
||||
_, alloc_error = append(&select_internal_tests, it)
|
||||
fmt.assertf(alloc_error == nil, "Error appending to select internal tests: %v", alloc_error)
|
||||
break find_test_by_name
|
||||
}
|
||||
}
|
||||
case 2:
|
||||
#no_bounds_check pkg := split_selector[0]
|
||||
#no_bounds_check name := split_selector[1]
|
||||
find_test_by_pkg_and_name: for it in internal_tests {
|
||||
if it.pkg == pkg && it.name == name {
|
||||
found = true
|
||||
_, alloc_error = append(&select_internal_tests, it)
|
||||
fmt.assertf(alloc_error == nil, "Error appending to select internal tests: %v", alloc_error)
|
||||
break find_test_by_pkg_and_name
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
fmt.wprintfln(stderr, "No test found for the name: %q", selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Intentional shadow with user-specified tests.
|
||||
internal_tests := select_internal_tests[:]
|
||||
}
|
||||
|
||||
total_failure_count := 0
|
||||
total_success_count := 0
|
||||
total_done_count := 0
|
||||
total_test_count := len(internal_tests)
|
||||
|
||||
when !FANCY_OUTPUT {
|
||||
// This is strictly for updating the window title when the progress
|
||||
// report is disabled. We're otherwise able to depend on the call to
|
||||
// `needs_to_redraw`.
|
||||
last_done_count := -1
|
||||
}
|
||||
|
||||
if total_test_count == 0 {
|
||||
// Exit early.
|
||||
fmt.wprintln(stdout, "No tests to run.")
|
||||
return true
|
||||
}
|
||||
|
||||
for it in internal_tests {
|
||||
if it.p == nil {
|
||||
total_test_count -= 1
|
||||
continue
|
||||
}
|
||||
// NOTE(Feoramund): The old test runner skipped over tests with nil
|
||||
// procedures, but I couldn't find any case where they occurred.
|
||||
// This assert stands to prevent any oversight on my part.
|
||||
fmt.assertf(it.p != nil, "Test %s.%s has <nil> procedure.", it.pkg, it.name)
|
||||
}
|
||||
|
||||
free_all(context.temp_allocator)
|
||||
reset_t(t)
|
||||
defer end_t(t)
|
||||
|
||||
if prev_pkg != it.pkg {
|
||||
prev_pkg = it.pkg
|
||||
logf(t, "[Package: %s]", it.pkg)
|
||||
}
|
||||
|
||||
logf(t, "[Test: %s]", it.name)
|
||||
|
||||
run_internal_test(t, it)
|
||||
|
||||
if failed(t) {
|
||||
logf(t, "[%s : FAILURE]", it.name)
|
||||
slice.sort_by(internal_tests, proc(a, b: Internal_Test) -> bool {
|
||||
if a.pkg == b.pkg {
|
||||
return a.name < b.name
|
||||
} else {
|
||||
logf(t, "[%s : SUCCESS]", it.name)
|
||||
total_success_count += 1
|
||||
return a.pkg < b.pkg
|
||||
}
|
||||
})
|
||||
|
||||
// -- Set thread count.
|
||||
|
||||
when TEST_THREADS == 0 {
|
||||
thread_count := os.processor_core_count()
|
||||
} else {
|
||||
thread_count := max(1, TEST_THREADS)
|
||||
}
|
||||
|
||||
thread_count = min(thread_count, total_test_count)
|
||||
|
||||
// -- Allocate.
|
||||
|
||||
pool_stack: mem.Rollback_Stack
|
||||
alloc_error = mem.rollback_stack_init(&pool_stack, POOL_BLOCK_SIZE)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for thread pool: %v", alloc_error)
|
||||
defer mem.rollback_stack_destroy(&pool_stack)
|
||||
|
||||
pool: thread.Pool
|
||||
thread.pool_init(&pool, mem.rollback_stack_allocator(&pool_stack), thread_count)
|
||||
defer thread.pool_destroy(&pool)
|
||||
|
||||
task_channels: []Task_Channel = ---
|
||||
task_channels, alloc_error = make([]Task_Channel, thread_count)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for update channels: %v", alloc_error)
|
||||
defer delete(task_channels)
|
||||
|
||||
for &task_channel, index in task_channels {
|
||||
task_channel.channel, alloc_error = chan.create_buffered(Update_Channel, BUFFERED_EVENTS_PER_CHANNEL, context.allocator)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for update channel #%i: %v", index, alloc_error)
|
||||
}
|
||||
defer for &task_channel in task_channels {
|
||||
chan.destroy(&task_channel.channel)
|
||||
}
|
||||
|
||||
// This buffer is used to batch writes to STDOUT or STDERR, to help reduce
|
||||
// screen flickering.
|
||||
batch_buffer: bytes.Buffer
|
||||
bytes.buffer_init_allocator(&batch_buffer, 0, BATCH_BUFFER_SIZE)
|
||||
batch_writer := io.to_writer(bytes.buffer_to_stream(&batch_buffer))
|
||||
defer bytes.buffer_destroy(&batch_buffer)
|
||||
|
||||
report: Report = ---
|
||||
report, alloc_error = make_report(internal_tests)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for test report: %v", alloc_error)
|
||||
defer destroy_report(&report)
|
||||
|
||||
when FANCY_OUTPUT {
|
||||
// We cannot make use of the ANSI save/restore cursor codes, because they
|
||||
// work by absolute screen coordinates. This will cause unnecessary
|
||||
// scrollback if we print at the bottom of someone's terminal.
|
||||
ansi_redraw_string := fmt.aprintf(
|
||||
// ANSI for "go up N lines then erase the screen from the cursor forward."
|
||||
ansi.CSI + "%i" + ansi.CPL + ansi.CSI + ansi.ED +
|
||||
// We'll combine this with the window title format string, since it
|
||||
// can be printed at the same time.
|
||||
"%s",
|
||||
// 1 extra line for the status bar.
|
||||
1 + len(report.packages), OSC_WINDOW_TITLE)
|
||||
assert(len(ansi_redraw_string) > 0, "Error allocating ANSI redraw string.")
|
||||
defer delete(ansi_redraw_string)
|
||||
|
||||
thread_count_status_string: string = ---
|
||||
{
|
||||
PADDING :: PROGRESS_COLUMN_SPACING + PROGRESS_WIDTH
|
||||
|
||||
unpadded := fmt.tprintf("%i thread%s", thread_count, "" if thread_count == 1 else "s")
|
||||
thread_count_status_string = fmt.aprintf("%- *[1]s", unpadded, report.pkg_column_len + PADDING)
|
||||
assert(len(thread_count_status_string) > 0, "Error allocating thread count status string.")
|
||||
}
|
||||
defer delete(thread_count_status_string)
|
||||
}
|
||||
|
||||
task_data_slots: []Task_Data = ---
|
||||
task_data_slots, alloc_error = make([]Task_Data, thread_count)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for task data slots: %v", alloc_error)
|
||||
defer delete(task_data_slots)
|
||||
|
||||
// Tests rotate through these allocators as they finish.
|
||||
task_allocators: []mem.Rollback_Stack = ---
|
||||
task_allocators, alloc_error = make([]mem.Rollback_Stack, thread_count)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for task allocators: %v", alloc_error)
|
||||
defer delete(task_allocators)
|
||||
|
||||
when TRACKING_MEMORY {
|
||||
task_memory_trackers: []mem.Tracking_Allocator = ---
|
||||
task_memory_trackers, alloc_error = make([]mem.Tracking_Allocator, thread_count)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for memory trackers: %v", alloc_error)
|
||||
defer delete(task_memory_trackers)
|
||||
}
|
||||
|
||||
#no_bounds_check for i in 0 ..< thread_count {
|
||||
alloc_error = mem.rollback_stack_init(&task_allocators[i], PER_THREAD_MEMORY)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for task allocator #%i: %v", i, alloc_error)
|
||||
when TRACKING_MEMORY {
|
||||
mem.tracking_allocator_init(&task_memory_trackers[i], mem.rollback_stack_allocator(&task_allocators[i]))
|
||||
}
|
||||
}
|
||||
logf(t, "----------------------------------------")
|
||||
if total_test_count == 0 {
|
||||
log(t, "NO TESTS RAN")
|
||||
} else {
|
||||
logf(t, "%d/%d SUCCESSFUL", total_success_count, total_test_count)
|
||||
|
||||
defer #no_bounds_check for i in 0 ..< thread_count {
|
||||
when TRACKING_MEMORY {
|
||||
mem.tracking_allocator_destroy(&task_memory_trackers[i])
|
||||
}
|
||||
mem.rollback_stack_destroy(&task_allocators[i])
|
||||
}
|
||||
|
||||
task_timeouts: [dynamic]Task_Timeout = ---
|
||||
task_timeouts, alloc_error = make([dynamic]Task_Timeout, 0, thread_count)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for task timeouts: %v", alloc_error)
|
||||
defer delete(task_timeouts)
|
||||
|
||||
failed_test_reason_map: map[int]string = ---
|
||||
failed_test_reason_map, alloc_error = make(map[int]string, RESERVED_TEST_FAILURES)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for failed test reasons: %v", alloc_error)
|
||||
defer delete(failed_test_reason_map)
|
||||
|
||||
log_messages: [dynamic]Log_Message = ---
|
||||
log_messages, alloc_error = make([dynamic]Log_Message, 0, RESERVED_LOG_MESSAGES)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for log message queue: %v", alloc_error)
|
||||
defer delete(log_messages)
|
||||
|
||||
sorted_failed_test_reasons: [dynamic]int = ---
|
||||
sorted_failed_test_reasons, alloc_error = make([dynamic]int, 0, RESERVED_TEST_FAILURES)
|
||||
fmt.assertf(alloc_error == nil, "Error allocating memory for sorted failed test reasons: %v", alloc_error)
|
||||
defer delete(sorted_failed_test_reasons)
|
||||
|
||||
when USE_CLIPBOARD {
|
||||
clipboard_buffer: bytes.Buffer
|
||||
bytes.buffer_init_allocator(&clipboard_buffer, 0, CLIPBOARD_BUFFER_SIZE)
|
||||
defer bytes.buffer_destroy(&clipboard_buffer)
|
||||
}
|
||||
|
||||
when SHARED_RANDOM_SEED == 0 {
|
||||
shared_random_seed := cast(u64)intrinsics.read_cycle_counter()
|
||||
} else {
|
||||
shared_random_seed := SHARED_RANDOM_SEED
|
||||
}
|
||||
|
||||
// -- Setup initial tasks.
|
||||
|
||||
// NOTE(Feoramund): This is the allocator that will be used by threads to
|
||||
// persist log messages past their lifetimes. It has its own variable name
|
||||
// in the event it needs to be changed from `context.allocator` without
|
||||
// digging through the source to divine everywhere it is used for that.
|
||||
shared_log_allocator := context.allocator
|
||||
|
||||
context.logger = {
|
||||
procedure = runner_logger_proc,
|
||||
data = &log_messages,
|
||||
lowest_level = get_log_level(),
|
||||
options = Default_Test_Logger_Opts - {.Short_File_Path, .Line, .Procedure},
|
||||
}
|
||||
|
||||
run_index: int
|
||||
|
||||
setup_tasks: for &data, task_index in task_data_slots {
|
||||
setup_next_test: for run_index < total_test_count {
|
||||
#no_bounds_check it := internal_tests[run_index]
|
||||
defer run_index += 1
|
||||
|
||||
data.it = it
|
||||
data.t.seed = shared_random_seed
|
||||
#no_bounds_check data.t.channel = chan.as_send(task_channels[task_index].channel)
|
||||
data.t._log_allocator = shared_log_allocator
|
||||
data.allocator_index = task_index
|
||||
|
||||
#no_bounds_check when TRACKING_MEMORY {
|
||||
task_allocator := mem.tracking_allocator(&task_memory_trackers[task_index])
|
||||
} else {
|
||||
task_allocator := mem.rollback_stack_allocator(&task_allocators[task_index])
|
||||
}
|
||||
|
||||
thread.pool_add_task(&pool, task_allocator, run_test_task, &data, run_index)
|
||||
|
||||
continue setup_tasks
|
||||
}
|
||||
}
|
||||
|
||||
// -- Run tests.
|
||||
|
||||
setup_signal_handler()
|
||||
|
||||
fmt.wprint(stdout, ansi.CSI + ansi.DECTCEM_HIDE)
|
||||
|
||||
when FANCY_OUTPUT {
|
||||
signals_were_raised := false
|
||||
|
||||
redraw_report(stdout, report)
|
||||
draw_status_bar(stdout, thread_count_status_string, total_done_count, total_test_count)
|
||||
}
|
||||
|
||||
when TEST_THREADS == 0 {
|
||||
pkg_log.infof("Starting test runner with %i thread%s. Set with -define:ODIN_TEST_THREADS=n.",
|
||||
thread_count,
|
||||
"" if thread_count == 1 else "s")
|
||||
} else {
|
||||
pkg_log.infof("Starting test runner with %i thread%s.",
|
||||
thread_count,
|
||||
"" if thread_count == 1 else "s")
|
||||
}
|
||||
|
||||
when SHARED_RANDOM_SEED == 0 {
|
||||
pkg_log.infof("The random seed sent to every test is: %v. Set with -define:ODIN_TEST_RANDOM_SEED=n.", shared_random_seed)
|
||||
} else {
|
||||
pkg_log.infof("The random seed sent to every test is: %v.", shared_random_seed)
|
||||
}
|
||||
|
||||
when TRACKING_MEMORY {
|
||||
when ALWAYS_REPORT_MEMORY {
|
||||
pkg_log.info("Memory tracking is enabled. Tests will log their memory usage when complete.")
|
||||
} else {
|
||||
pkg_log.info("Memory tracking is enabled. Tests will log their memory usage if there's an issue.")
|
||||
}
|
||||
pkg_log.info("< Final Mem/ Total Mem> < Peak Mem> (#Free/Alloc) :: [package.test_name]")
|
||||
} else when ALWAYS_REPORT_MEMORY {
|
||||
pkg_log.warn("ODIN_TEST_ALWAYS_REPORT_MEMORY is true, but ODIN_TRACK_MEMORY is false.")
|
||||
}
|
||||
|
||||
start_time := time.now()
|
||||
|
||||
thread.pool_start(&pool)
|
||||
main_loop: for !thread.pool_is_empty(&pool) {
|
||||
{
|
||||
events_pending := thread.pool_num_done(&pool) > 0
|
||||
|
||||
if !events_pending {
|
||||
poll_tasks: for &task_channel in task_channels {
|
||||
if chan.len(task_channel.channel) > 0 {
|
||||
events_pending = true
|
||||
break poll_tasks
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !events_pending {
|
||||
// Keep the main thread from pegging a core at 100% usage.
|
||||
time.sleep(1 * time.Microsecond)
|
||||
}
|
||||
}
|
||||
|
||||
cycle_pool: for task in thread.pool_pop_done(&pool) {
|
||||
data := cast(^Task_Data)(task.data)
|
||||
|
||||
when TRACKING_MEMORY {
|
||||
#no_bounds_check tracker := &task_memory_trackers[data.allocator_index]
|
||||
|
||||
memory_is_in_bad_state := len(tracker.allocation_map) + len(tracker.bad_free_array) > 0
|
||||
|
||||
when ALWAYS_REPORT_MEMORY {
|
||||
should_report := true
|
||||
} else {
|
||||
should_report := memory_is_in_bad_state
|
||||
}
|
||||
|
||||
if should_report {
|
||||
write_memory_report(batch_writer, tracker, data.it.pkg, data.it.name)
|
||||
|
||||
pkg_log.log(.Warning if memory_is_in_bad_state else .Info, bytes.buffer_to_string(&batch_buffer))
|
||||
bytes.buffer_reset(&batch_buffer)
|
||||
}
|
||||
|
||||
mem.tracking_allocator_reset(tracker)
|
||||
}
|
||||
|
||||
free_all(task.allocator)
|
||||
|
||||
if run_index < total_test_count {
|
||||
#no_bounds_check it := internal_tests[run_index]
|
||||
defer run_index += 1
|
||||
|
||||
data.it = it
|
||||
data.t.seed = shared_random_seed
|
||||
data.t.error_count = 0
|
||||
|
||||
thread.pool_add_task(&pool, task.allocator, run_test_task, data, run_index)
|
||||
}
|
||||
}
|
||||
|
||||
handle_events: for &task_channel in task_channels {
|
||||
for ev in chan.try_recv(task_channel.channel) {
|
||||
switch event in ev {
|
||||
case Event_New_Test:
|
||||
task_channel.test_index = event.test_index
|
||||
|
||||
case Event_State_Change:
|
||||
#no_bounds_check report.all_test_states[task_channel.test_index] = event.new_state
|
||||
|
||||
#no_bounds_check it := internal_tests[task_channel.test_index]
|
||||
#no_bounds_check pkg := report.packages_by_name[it.pkg]
|
||||
|
||||
#partial switch event.new_state {
|
||||
case .Failed:
|
||||
if task_channel.test_index not_in failed_test_reason_map {
|
||||
failed_test_reason_map[task_channel.test_index] = ERROR_STRING_UNKNOWN
|
||||
}
|
||||
total_failure_count += 1
|
||||
total_done_count += 1
|
||||
case .Successful:
|
||||
total_success_count += 1
|
||||
total_done_count += 1
|
||||
}
|
||||
|
||||
when ODIN_DEBUG {
|
||||
pkg_log.debugf("Test #%i %s.%s changed state to %v.", task_channel.test_index, it.pkg, it.name, event.new_state)
|
||||
}
|
||||
|
||||
pkg.last_change_state = event.new_state
|
||||
pkg.last_change_name = it.name
|
||||
pkg.frame_ready = false
|
||||
|
||||
case Event_Set_Fail_Timeout:
|
||||
_, alloc_error = append(&task_timeouts, Task_Timeout {
|
||||
test_index = task_channel.test_index,
|
||||
at_time = event.at_time,
|
||||
location = event.location,
|
||||
})
|
||||
fmt.assertf(alloc_error == nil, "Error appending to task timeouts: %v", alloc_error)
|
||||
|
||||
case Event_Log_Message:
|
||||
_, alloc_error = append(&log_messages, Log_Message {
|
||||
level = event.level,
|
||||
text = event.formatted_text,
|
||||
time = event.time,
|
||||
allocator = shared_log_allocator,
|
||||
})
|
||||
fmt.assertf(alloc_error == nil, "Error appending to log messages: %v", alloc_error)
|
||||
|
||||
if event.level >= .Error {
|
||||
// Save the message for the final summary.
|
||||
if old_error, ok := failed_test_reason_map[task_channel.test_index]; ok {
|
||||
safe_delete_string(old_error, shared_log_allocator)
|
||||
}
|
||||
failed_test_reason_map[task_channel.test_index] = event.text
|
||||
} else {
|
||||
delete(event.text, shared_log_allocator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
check_timeouts: for i := len(task_timeouts) - 1; i >= 0; i -= 1 {
|
||||
#no_bounds_check timeout := &task_timeouts[i]
|
||||
|
||||
if time.since(timeout.at_time) < 0 {
|
||||
continue check_timeouts
|
||||
}
|
||||
|
||||
defer unordered_remove(&task_timeouts, i)
|
||||
|
||||
#no_bounds_check if report.all_test_states[timeout.test_index] > .Running {
|
||||
continue check_timeouts
|
||||
}
|
||||
|
||||
if !thread.pool_stop_task(&pool, timeout.test_index) {
|
||||
// The task may have stopped a split second after we started
|
||||
// checking, but we haven't handled the new state yet.
|
||||
continue check_timeouts
|
||||
}
|
||||
|
||||
#no_bounds_check report.all_test_states[timeout.test_index] = .Failed
|
||||
#no_bounds_check it := internal_tests[timeout.test_index]
|
||||
#no_bounds_check pkg := report.packages_by_name[it.pkg]
|
||||
pkg.frame_ready = false
|
||||
|
||||
if old_error, ok := failed_test_reason_map[timeout.test_index]; ok {
|
||||
safe_delete_string(old_error, shared_log_allocator)
|
||||
}
|
||||
failed_test_reason_map[timeout.test_index] = ERROR_STRING_TIMEOUT
|
||||
total_failure_count += 1
|
||||
total_done_count += 1
|
||||
|
||||
now := time.now()
|
||||
_, alloc_error = append(&log_messages, Log_Message {
|
||||
level = .Error,
|
||||
text = format_log_text(.Error, ERROR_STRING_TIMEOUT, Default_Test_Logger_Opts, timeout.location, now),
|
||||
time = now,
|
||||
allocator = context.allocator,
|
||||
})
|
||||
fmt.assertf(alloc_error == nil, "Error appending to log messages: %v", alloc_error)
|
||||
|
||||
find_task_data: for &data in task_data_slots {
|
||||
if data.it.pkg == it.pkg && data.it.name == it.name {
|
||||
end_t(&data.t)
|
||||
break find_task_data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if should_stop_runner() {
|
||||
fmt.wprintln(stderr, "\nCaught interrupt signal. Stopping all tests.")
|
||||
thread.pool_shutdown(&pool)
|
||||
break main_loop
|
||||
}
|
||||
|
||||
when FANCY_OUTPUT {
|
||||
// Because the bounds checking procs send directly to STDERR with
|
||||
// no way to redirect or handle them, we need to at least try to
|
||||
// let the user see those messages when using the animated progress
|
||||
// report. This flag may be set by the block of code below if a
|
||||
// signal is raised.
|
||||
//
|
||||
// It'll be purely by luck if the output is interleaved properly,
|
||||
// given the nature of non-thread-safe printing.
|
||||
//
|
||||
// At worst, if Odin did not print any error for this signal, we'll
|
||||
// just re-display the progress report. The fatal log error message
|
||||
// should be enough to clue the user in that something dire has
|
||||
// occurred.
|
||||
bypass_progress_overwrite := false
|
||||
}
|
||||
|
||||
if test_index, reason, ok := should_stop_test(); ok {
|
||||
#no_bounds_check report.all_test_states[test_index] = .Failed
|
||||
#no_bounds_check it := internal_tests[test_index]
|
||||
#no_bounds_check pkg := report.packages_by_name[it.pkg]
|
||||
pkg.frame_ready = false
|
||||
|
||||
fmt.assertf(thread.pool_stop_task(&pool, test_index),
|
||||
"A signal (%v) was raised to stop test #%i %s.%s, but it was unable to be found.",
|
||||
reason, test_index, it.pkg, it.name)
|
||||
|
||||
if test_index not_in failed_test_reason_map {
|
||||
// We only write a new error message here if there wasn't one
|
||||
// already, because the message we can provide based only on
|
||||
// the signal won't be very useful, whereas asserts and panics
|
||||
// will provide a user-written error message.
|
||||
failed_test_reason_map[test_index] = fmt.aprintf("Signal caught: %v", reason, allocator = shared_log_allocator)
|
||||
pkg_log.fatalf("Caught signal to stop test #%i %s.%s for: %v.", test_index, it.pkg, it.name, reason)
|
||||
|
||||
}
|
||||
|
||||
when FANCY_OUTPUT {
|
||||
bypass_progress_overwrite = true
|
||||
signals_were_raised = true
|
||||
}
|
||||
|
||||
total_failure_count += 1
|
||||
total_done_count += 1
|
||||
}
|
||||
|
||||
// -- Redraw.
|
||||
|
||||
when FANCY_OUTPUT {
|
||||
if len(log_messages) == 0 && !needs_to_redraw(report) {
|
||||
continue main_loop
|
||||
}
|
||||
|
||||
if !bypass_progress_overwrite {
|
||||
fmt.wprintf(stdout, ansi_redraw_string, total_done_count, total_test_count)
|
||||
}
|
||||
} else {
|
||||
if total_done_count != last_done_count {
|
||||
fmt.wprintf(stdout, OSC_WINDOW_TITLE, total_done_count, total_test_count)
|
||||
last_done_count = total_done_count
|
||||
}
|
||||
|
||||
if len(log_messages) == 0 {
|
||||
continue main_loop
|
||||
}
|
||||
}
|
||||
|
||||
// Because each thread has its own messenger channel, log messages
|
||||
// arrive in chunks that are in-order, but when they're merged with the
|
||||
// logs from other threads, they become out-of-order.
|
||||
slice.stable_sort_by(log_messages[:], proc(a, b: Log_Message) -> bool {
|
||||
return time.diff(a.time, b.time) > 0
|
||||
})
|
||||
|
||||
for message in log_messages {
|
||||
fmt.wprintln(batch_writer, message.text)
|
||||
delete(message.text, message.allocator)
|
||||
}
|
||||
|
||||
fmt.wprint(stderr, bytes.buffer_to_string(&batch_buffer))
|
||||
clear(&log_messages)
|
||||
bytes.buffer_reset(&batch_buffer)
|
||||
|
||||
when FANCY_OUTPUT {
|
||||
redraw_report(batch_writer, report)
|
||||
draw_status_bar(batch_writer, thread_count_status_string, total_done_count, total_test_count)
|
||||
fmt.wprint(stdout, bytes.buffer_to_string(&batch_buffer))
|
||||
bytes.buffer_reset(&batch_buffer)
|
||||
}
|
||||
}
|
||||
|
||||
// -- All tests are complete, or the runner has been interrupted.
|
||||
|
||||
// NOTE(Feoramund): If you've arrived here after receiving signal 11 or
|
||||
// SIGSEGV on the main runner thread, while using a UNIX-like platform,
|
||||
// there is the possibility that you may have encountered a rare edge case
|
||||
// involving the joining of threads.
|
||||
//
|
||||
// At the time of writing, the thread library is undergoing a rewrite that
|
||||
// should solve this problem; it is not an issue with the test runner itself.
|
||||
thread.pool_join(&pool)
|
||||
|
||||
finished_in := time.since(start_time)
|
||||
|
||||
when !FANCY_OUTPUT {
|
||||
// One line to space out the results, since we don't have the status
|
||||
// bar in plain mode.
|
||||
fmt.wprintln(batch_writer)
|
||||
}
|
||||
|
||||
fmt.wprintf(batch_writer,
|
||||
"Finished %i test%s in %v.",
|
||||
total_done_count,
|
||||
"" if total_done_count == 1 else "s",
|
||||
finished_in)
|
||||
|
||||
if total_done_count != total_test_count {
|
||||
not_run_count := total_test_count - total_done_count
|
||||
fmt.wprintf(batch_writer,
|
||||
" " + SGR_READY + "%i" + SGR_RESET + " %s left undone.",
|
||||
not_run_count,
|
||||
"test was" if not_run_count == 1 else "tests were")
|
||||
}
|
||||
|
||||
if total_success_count == total_test_count {
|
||||
fmt.wprintfln(batch_writer,
|
||||
" %s " + SGR_SUCCESS + "successful." + SGR_RESET,
|
||||
"The test was" if total_test_count == 1 else "All tests were")
|
||||
} else if total_failure_count > 0 {
|
||||
if total_failure_count == total_test_count {
|
||||
fmt.wprintfln(batch_writer,
|
||||
" %s " + SGR_FAILED + "failed." + SGR_RESET,
|
||||
"The test" if total_test_count == 1 else "All tests")
|
||||
} else {
|
||||
fmt.wprintfln(batch_writer,
|
||||
" " + SGR_FAILED + "%i" + SGR_RESET + " test%s failed.",
|
||||
total_failure_count,
|
||||
"" if total_failure_count == 1 else "s")
|
||||
}
|
||||
|
||||
for test_index in failed_test_reason_map {
|
||||
_, alloc_error = append(&sorted_failed_test_reasons, test_index)
|
||||
fmt.assertf(alloc_error == nil, "Error appending to sorted failed test reasons: %v", alloc_error)
|
||||
}
|
||||
|
||||
slice.sort(sorted_failed_test_reasons[:])
|
||||
|
||||
for test_index in sorted_failed_test_reasons {
|
||||
#no_bounds_check last_error := failed_test_reason_map[test_index]
|
||||
#no_bounds_check it := internal_tests[test_index]
|
||||
pkg_and_name := fmt.tprintf("%s.%s", it.pkg, it.name)
|
||||
fmt.wprintfln(batch_writer, " - %- *[1]s\t%s",
|
||||
pkg_and_name,
|
||||
report.pkg_column_len + report.test_column_len,
|
||||
last_error)
|
||||
safe_delete_string(last_error, shared_log_allocator)
|
||||
}
|
||||
|
||||
if total_success_count > 0 {
|
||||
when USE_CLIPBOARD {
|
||||
clipboard_writer := io.to_writer(bytes.buffer_to_stream(&clipboard_buffer))
|
||||
fmt.wprint(clipboard_writer, "-define:ODIN_TEST_NAMES=")
|
||||
for test_index in sorted_failed_test_reasons {
|
||||
#no_bounds_check it := internal_tests[test_index]
|
||||
fmt.wprintf(clipboard_writer, "%s.%s,", it.pkg, it.name)
|
||||
}
|
||||
|
||||
encoded_names := base64.encode(bytes.buffer_to_bytes(&clipboard_buffer), allocator = context.temp_allocator)
|
||||
|
||||
fmt.wprintf(batch_writer,
|
||||
ansi.OSC + ansi.CLIPBOARD + ";c;%s" + ansi.ST +
|
||||
"\nThe name%s of the failed test%s been copied to your clipboard.",
|
||||
encoded_names,
|
||||
"" if total_failure_count == 1 else "s",
|
||||
" has" if total_failure_count == 1 else "s have")
|
||||
} else {
|
||||
fmt.wprintf(batch_writer, "\nTo run only the failed test%s, use:\n\t-define:ODIN_TEST_NAMES=",
|
||||
"" if total_failure_count == 1 else "s")
|
||||
for test_index in sorted_failed_test_reasons {
|
||||
#no_bounds_check it := internal_tests[test_index]
|
||||
fmt.wprintf(batch_writer, "%s.%s,", it.pkg, it.name)
|
||||
}
|
||||
fmt.wprint(batch_writer, "\n\nIf your terminal supports OSC 52, you may use -define:ODIN_TEST_CLIPBOARD to have this copied directly to your clipboard.")
|
||||
}
|
||||
|
||||
fmt.wprintln(batch_writer)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.wprint(stdout, ansi.CSI + ansi.DECTCEM_SHOW)
|
||||
|
||||
when FANCY_OUTPUT {
|
||||
if signals_were_raised {
|
||||
fmt.wprintln(batch_writer, `
|
||||
Signals were raised during this test run. Log messages are likely to have collided with each other.
|
||||
To partly mitigate this, redirect STDERR to a file or use the -define:ODIN_TEST_FANCY=false option.`)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.wprintln(stderr, bytes.buffer_to_string(&batch_buffer))
|
||||
|
||||
return total_success_count == total_test_count
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
//+private
|
||||
//+build !windows
|
||||
package testing
|
||||
|
||||
import "core:time"
|
||||
|
||||
run_internal_test :: proc(t: ^T, it: Internal_Test) {
|
||||
// TODO(bill): Catch panics on other platforms
|
||||
it.p(t)
|
||||
}
|
||||
|
||||
_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) {
|
||||
|
||||
}
|
||||
@@ -1,235 +0,0 @@
|
||||
//+private
|
||||
//+build windows
|
||||
package testing
|
||||
|
||||
import win32 "core:sys/windows"
|
||||
import "base:runtime"
|
||||
import "base:intrinsics"
|
||||
import "core:time"
|
||||
|
||||
Sema :: struct {
|
||||
count: i32,
|
||||
}
|
||||
|
||||
sema_reset :: proc "contextless" (s: ^Sema) {
|
||||
intrinsics.atomic_store(&s.count, 0)
|
||||
}
|
||||
sema_wait :: proc "contextless" (s: ^Sema) {
|
||||
for {
|
||||
original_count := s.count
|
||||
for original_count == 0 {
|
||||
win32.WaitOnAddress(&s.count, &original_count, size_of(original_count), win32.INFINITE)
|
||||
original_count = s.count
|
||||
}
|
||||
if original_count == intrinsics.atomic_compare_exchange_strong(&s.count, original_count-1, original_count) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
sema_wait_with_timeout :: proc "contextless" (s: ^Sema, duration: time.Duration) -> bool {
|
||||
if duration <= 0 {
|
||||
return false
|
||||
}
|
||||
for {
|
||||
|
||||
original_count := intrinsics.atomic_load(&s.count)
|
||||
for start := time.tick_now(); original_count == 0; /**/ {
|
||||
if intrinsics.atomic_load(&s.count) != original_count {
|
||||
remaining := duration - time.tick_since(start)
|
||||
if remaining < 0 {
|
||||
return false
|
||||
}
|
||||
ms := u32(remaining/time.Millisecond)
|
||||
if !win32.WaitOnAddress(&s.count, &original_count, size_of(original_count), ms) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
original_count = s.count
|
||||
}
|
||||
if original_count == intrinsics.atomic_compare_exchange_strong(&s.count, original_count-1, original_count) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sema_post :: proc "contextless" (s: ^Sema, count := 1) {
|
||||
intrinsics.atomic_add(&s.count, i32(count))
|
||||
if count == 1 {
|
||||
win32.WakeByAddressSingle(&s.count)
|
||||
} else {
|
||||
win32.WakeByAddressAll(&s.count)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Thread_Proc :: #type proc(^Thread)
|
||||
|
||||
MAX_USER_ARGUMENTS :: 8
|
||||
|
||||
Thread :: struct {
|
||||
using specific: Thread_Os_Specific,
|
||||
procedure: Thread_Proc,
|
||||
|
||||
t: ^T,
|
||||
it: Internal_Test,
|
||||
success: bool,
|
||||
|
||||
init_context: Maybe(runtime.Context),
|
||||
|
||||
creation_allocator: runtime.Allocator,
|
||||
|
||||
internal_fail_timeout: time.Duration,
|
||||
internal_fail_timeout_loc: runtime.Source_Code_Location,
|
||||
}
|
||||
|
||||
Thread_Os_Specific :: struct {
|
||||
win32_thread: win32.HANDLE,
|
||||
win32_thread_id: win32.DWORD,
|
||||
done: bool, // see note in `is_done`
|
||||
}
|
||||
|
||||
thread_create :: proc(procedure: Thread_Proc) -> ^Thread {
|
||||
__windows_thread_entry_proc :: proc "system" (t_: rawptr) -> win32.DWORD {
|
||||
t := (^Thread)(t_)
|
||||
context = t.init_context.? or_else runtime.default_context()
|
||||
|
||||
t.procedure(t)
|
||||
|
||||
if t.init_context == nil {
|
||||
if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
|
||||
runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data)
|
||||
}
|
||||
}
|
||||
|
||||
intrinsics.atomic_store(&t.done, true)
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
thread := new(Thread)
|
||||
if thread == nil {
|
||||
return nil
|
||||
}
|
||||
thread.creation_allocator = context.allocator
|
||||
|
||||
win32_thread_id: win32.DWORD
|
||||
win32_thread := win32.CreateThread(nil, 0, __windows_thread_entry_proc, thread, win32.CREATE_SUSPENDED, &win32_thread_id)
|
||||
if win32_thread == nil {
|
||||
free(thread, thread.creation_allocator)
|
||||
return nil
|
||||
}
|
||||
thread.procedure = procedure
|
||||
thread.win32_thread = win32_thread
|
||||
thread.win32_thread_id = win32_thread_id
|
||||
thread.init_context = context
|
||||
|
||||
return thread
|
||||
}
|
||||
|
||||
thread_start :: proc "contextless" (thread: ^Thread) {
|
||||
win32.ResumeThread(thread.win32_thread)
|
||||
}
|
||||
|
||||
thread_join_and_destroy :: proc(thread: ^Thread) {
|
||||
if thread.win32_thread != win32.INVALID_HANDLE {
|
||||
win32.WaitForSingleObject(thread.win32_thread, win32.INFINITE)
|
||||
win32.CloseHandle(thread.win32_thread)
|
||||
thread.win32_thread = win32.INVALID_HANDLE
|
||||
}
|
||||
free(thread, thread.creation_allocator)
|
||||
}
|
||||
|
||||
thread_terminate :: proc "contextless" (thread: ^Thread, exit_code: int) {
|
||||
win32.TerminateThread(thread.win32_thread, u32(exit_code))
|
||||
}
|
||||
|
||||
|
||||
_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) {
|
||||
assert(global_fail_timeout_thread == nil, "set_fail_timeout previously called", loc)
|
||||
|
||||
thread := thread_create(proc(thread: ^Thread) {
|
||||
t := thread.t
|
||||
timeout := thread.internal_fail_timeout
|
||||
if !sema_wait_with_timeout(&global_fail_timeout_semaphore, timeout) {
|
||||
fail_now(t, "TIMEOUT", thread.internal_fail_timeout_loc)
|
||||
}
|
||||
})
|
||||
thread.internal_fail_timeout = duration
|
||||
thread.internal_fail_timeout_loc = loc
|
||||
thread.t = t
|
||||
global_fail_timeout_thread = thread
|
||||
thread_start(thread)
|
||||
}
|
||||
|
||||
global_fail_timeout_thread: ^Thread
|
||||
global_fail_timeout_semaphore: Sema
|
||||
|
||||
global_threaded_runner_semaphore: Sema
|
||||
global_exception_handler: rawptr
|
||||
global_current_thread: ^Thread
|
||||
global_current_t: ^T
|
||||
|
||||
run_internal_test :: proc(t: ^T, it: Internal_Test) {
|
||||
thread := thread_create(proc(thread: ^Thread) {
|
||||
exception_handler_proc :: proc "system" (ExceptionInfo: ^win32.EXCEPTION_POINTERS) -> win32.LONG {
|
||||
switch ExceptionInfo.ExceptionRecord.ExceptionCode {
|
||||
case
|
||||
win32.EXCEPTION_DATATYPE_MISALIGNMENT,
|
||||
win32.EXCEPTION_BREAKPOINT,
|
||||
win32.EXCEPTION_ACCESS_VIOLATION,
|
||||
win32.EXCEPTION_ILLEGAL_INSTRUCTION,
|
||||
win32.EXCEPTION_ARRAY_BOUNDS_EXCEEDED,
|
||||
win32.EXCEPTION_STACK_OVERFLOW:
|
||||
|
||||
sema_post(&global_threaded_runner_semaphore)
|
||||
return win32.EXCEPTION_EXECUTE_HANDLER
|
||||
}
|
||||
|
||||
return win32.EXCEPTION_CONTINUE_SEARCH
|
||||
}
|
||||
global_exception_handler = win32.AddVectoredExceptionHandler(0, exception_handler_proc)
|
||||
|
||||
context.assertion_failure_proc = proc(prefix, message: string, loc: runtime.Source_Code_Location) -> ! {
|
||||
errorf(global_current_t, "%s %s", prefix, message, loc=loc)
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
t := thread.t
|
||||
|
||||
global_fail_timeout_thread = nil
|
||||
sema_reset(&global_fail_timeout_semaphore)
|
||||
|
||||
thread.it.p(t)
|
||||
|
||||
sema_post(&global_fail_timeout_semaphore)
|
||||
if global_fail_timeout_thread != nil do thread_join_and_destroy(global_fail_timeout_thread)
|
||||
|
||||
thread.success = true
|
||||
sema_post(&global_threaded_runner_semaphore)
|
||||
})
|
||||
|
||||
sema_reset(&global_threaded_runner_semaphore)
|
||||
global_current_t = t
|
||||
|
||||
t._fail_now = proc() -> ! {
|
||||
intrinsics.trap()
|
||||
}
|
||||
|
||||
thread.t = t
|
||||
thread.it = it
|
||||
thread.success = false
|
||||
thread_start(thread)
|
||||
|
||||
sema_wait(&global_threaded_runner_semaphore)
|
||||
thread_terminate(thread, int(!thread.success))
|
||||
thread_join_and_destroy(thread)
|
||||
|
||||
win32.RemoveVectoredExceptionHandler(global_exception_handler)
|
||||
|
||||
if !thread.success && t.error_count == 0 {
|
||||
t.error_count += 1
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//+private
|
||||
package testing
|
||||
|
||||
import "base:runtime"
|
||||
import pkg_log "core:log"
|
||||
|
||||
Stop_Reason :: enum {
|
||||
Unknown,
|
||||
Illegal_Instruction,
|
||||
Arithmetic_Error,
|
||||
Segmentation_Fault,
|
||||
}
|
||||
|
||||
test_assertion_failure_proc :: proc(prefix, message: string, loc: runtime.Source_Code_Location) -> ! {
|
||||
pkg_log.fatalf("%s: %s", prefix, message, location = loc)
|
||||
runtime.trap()
|
||||
}
|
||||
|
||||
setup_signal_handler :: proc() {
|
||||
_setup_signal_handler()
|
||||
}
|
||||
|
||||
setup_task_signal_handler :: proc(test_index: int) {
|
||||
_setup_task_signal_handler(test_index)
|
||||
}
|
||||
|
||||
should_stop_runner :: proc() -> bool {
|
||||
return _should_stop_runner()
|
||||
}
|
||||
|
||||
should_stop_test :: proc() -> (test_index: int, reason: Stop_Reason, ok: bool) {
|
||||
return _should_stop_test()
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
//+private
|
||||
//+build windows, linux, darwin, freebsd, openbsd, netbsd, haiku
|
||||
package testing
|
||||
|
||||
import "base:intrinsics"
|
||||
import "core:c/libc"
|
||||
import "core:encoding/ansi"
|
||||
import "core:sync"
|
||||
@require import "core:sys/unix"
|
||||
|
||||
@(private="file") stop_runner_flag: libc.sig_atomic_t
|
||||
|
||||
@(private="file") stop_test_gate: sync.Mutex
|
||||
@(private="file") stop_test_index: libc.sig_atomic_t
|
||||
@(private="file") stop_test_reason: libc.sig_atomic_t
|
||||
@(private="file") stop_test_alert: libc.sig_atomic_t
|
||||
|
||||
@(private="file", thread_local)
|
||||
local_test_index: libc.sig_atomic_t
|
||||
|
||||
@(private="file")
|
||||
stop_runner_callback :: proc "c" (sig: libc.int) {
|
||||
intrinsics.atomic_store(&stop_runner_flag, 1)
|
||||
}
|
||||
|
||||
@(private="file")
|
||||
stop_test_callback :: proc "c" (sig: libc.int) {
|
||||
if local_test_index == -1 {
|
||||
// We're the test runner, and we ourselves have caught a signal from
|
||||
// which there is no recovery.
|
||||
//
|
||||
// The most we can do now is make sure the user's cursor is visible,
|
||||
// nuke the entire processs, and hope a useful core dump survives.
|
||||
|
||||
// NOTE(Feoramund): Using these write calls in a signal handler is
|
||||
// undefined behavior in C99 but possibly tolerated in POSIX 2008.
|
||||
// Either way, we may as well try to salvage what we can.
|
||||
show_cursor := ansi.CSI + ansi.DECTCEM_SHOW
|
||||
libc.fwrite(raw_data(show_cursor), size_of(byte), len(show_cursor), libc.stdout)
|
||||
libc.fflush(libc.stdout)
|
||||
|
||||
// This is an attempt at being compliant by avoiding printf.
|
||||
sigbuf: [8]byte
|
||||
sigstr: string
|
||||
{
|
||||
signum := cast(int)sig
|
||||
i := len(sigbuf) - 2
|
||||
for signum > 0 {
|
||||
m := signum % 10
|
||||
signum /= 10
|
||||
sigbuf[i] = cast(u8)('0' + m)
|
||||
i -= 1
|
||||
}
|
||||
sigstr = cast(string)sigbuf[1 + i:len(sigbuf) - 1]
|
||||
}
|
||||
|
||||
advisory_a := `
|
||||
The test runner's main thread has caught an unrecoverable error (signal `
|
||||
advisory_b := `) and will now forcibly terminate.
|
||||
This is a dire bug and should be reported to the Odin developers.
|
||||
`
|
||||
libc.fwrite(raw_data(advisory_a), size_of(byte), len(advisory_a), libc.stderr)
|
||||
libc.fwrite(raw_data(sigstr), size_of(byte), len(sigstr), libc.stderr)
|
||||
libc.fwrite(raw_data(advisory_b), size_of(byte), len(advisory_b), libc.stderr)
|
||||
|
||||
// Try to get a core dump.
|
||||
libc.abort()
|
||||
}
|
||||
|
||||
if sync.mutex_guard(&stop_test_gate) {
|
||||
intrinsics.atomic_store(&stop_test_index, local_test_index)
|
||||
intrinsics.atomic_store(&stop_test_reason, cast(libc.sig_atomic_t)sig)
|
||||
intrinsics.atomic_store(&stop_test_alert, 1)
|
||||
|
||||
for {
|
||||
// Idle until this thread is terminated by the runner,
|
||||
// otherwise we may continue to generate signals.
|
||||
intrinsics.cpu_relax()
|
||||
|
||||
when ODIN_OS != .Windows {
|
||||
// NOTE(Feoramund): Some UNIX-like platforms may require this.
|
||||
//
|
||||
// During testing, I found that NetBSD 10.0 refused to
|
||||
// terminate a task thread, even when its thread had been
|
||||
// properly set to PTHREAD_CANCEL_ASYNCHRONOUS.
|
||||
//
|
||||
// The runner would stall after returning from `pthread_cancel`.
|
||||
|
||||
unix.pthread_testcancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_setup_signal_handler :: proc() {
|
||||
local_test_index = -1
|
||||
|
||||
// Catch user interrupt / CTRL-C.
|
||||
libc.signal(libc.SIGINT, stop_runner_callback)
|
||||
// Catch polite termination request.
|
||||
libc.signal(libc.SIGTERM, stop_runner_callback)
|
||||
|
||||
// For tests:
|
||||
// Catch asserts and panics.
|
||||
libc.signal(libc.SIGILL, stop_test_callback)
|
||||
// Catch arithmetic errors.
|
||||
libc.signal(libc.SIGFPE, stop_test_callback)
|
||||
// Catch segmentation faults (illegal memory access).
|
||||
libc.signal(libc.SIGSEGV, stop_test_callback)
|
||||
}
|
||||
|
||||
_setup_task_signal_handler :: proc(test_index: int) {
|
||||
local_test_index = cast(libc.sig_atomic_t)test_index
|
||||
}
|
||||
|
||||
_should_stop_runner :: proc() -> bool {
|
||||
return intrinsics.atomic_load(&stop_runner_flag) == 1
|
||||
}
|
||||
|
||||
@(private="file")
|
||||
unlock_stop_test_gate :: proc(_: int, _: Stop_Reason, ok: bool) {
|
||||
if ok {
|
||||
sync.mutex_unlock(&stop_test_gate)
|
||||
}
|
||||
}
|
||||
|
||||
@(deferred_out=unlock_stop_test_gate)
|
||||
_should_stop_test :: proc() -> (test_index: int, reason: Stop_Reason, ok: bool) {
|
||||
if intrinsics.atomic_load(&stop_test_alert) == 1 {
|
||||
intrinsics.atomic_store(&stop_test_alert, 0)
|
||||
|
||||
test_index = cast(int)intrinsics.atomic_load(&stop_test_index)
|
||||
switch intrinsics.atomic_load(&stop_test_reason) {
|
||||
case libc.SIGFPE: reason = .Arithmetic_Error
|
||||
case libc.SIGILL: reason = .Illegal_Instruction
|
||||
case libc.SIGSEGV: reason = .Segmentation_Fault
|
||||
}
|
||||
ok = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//+private
|
||||
//+build !windows !linux !darwin !freebsd !openbsd !netbsd !haiku
|
||||
package testing
|
||||
|
||||
_setup_signal_handler :: proc() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
_setup_task_signal_handler :: proc(test_index: int) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
_should_stop_runner :: proc() -> bool {
|
||||
return false
|
||||
}
|
||||
|
||||
_should_stop_test :: proc() -> (test_index: int, reason: Stop_Reason, ok: bool) {
|
||||
return 0, {}, false
|
||||
}
|
||||
+43
-26
@@ -1,10 +1,11 @@
|
||||
package testing
|
||||
|
||||
import "core:fmt"
|
||||
import "core:io"
|
||||
import "core:time"
|
||||
import "base:intrinsics"
|
||||
import "base:runtime"
|
||||
import pkg_log "core:log"
|
||||
import "core:reflect"
|
||||
import "core:sync/chan"
|
||||
import "core:time"
|
||||
|
||||
_ :: reflect // alias reflect to nothing to force visibility for -vet
|
||||
|
||||
@@ -22,44 +23,55 @@ Internal_Test :: struct {
|
||||
Internal_Cleanup :: struct {
|
||||
procedure: proc(rawptr),
|
||||
user_data: rawptr,
|
||||
ctx: runtime.Context,
|
||||
}
|
||||
|
||||
T :: struct {
|
||||
error_count: int,
|
||||
|
||||
w: io.Writer,
|
||||
// If your test needs to perform random operations, it's advised to use
|
||||
// this value to seed a local random number generator rather than relying
|
||||
// on the non-thread-safe global one.
|
||||
//
|
||||
// This way, your results will be deterministic.
|
||||
//
|
||||
// This value is chosen at startup of the test runner, logged, and may be
|
||||
// specified by the user. It is the same for all tests of a single run.
|
||||
seed: u64,
|
||||
|
||||
channel: Update_Channel_Sender,
|
||||
|
||||
cleanups: [dynamic]Internal_Cleanup,
|
||||
|
||||
// This allocator is shared between the test runner and its threads for
|
||||
// cloning log strings, so they can outlive the lifetime of individual
|
||||
// tests during channel transmission.
|
||||
_log_allocator: runtime.Allocator,
|
||||
|
||||
_fail_now: proc() -> !,
|
||||
}
|
||||
|
||||
|
||||
@(deprecated="prefer `log.error`")
|
||||
error :: proc(t: ^T, args: ..any, loc := #caller_location) {
|
||||
fmt.wprintf(t.w, "%v: ", loc)
|
||||
fmt.wprintln(t.w, ..args)
|
||||
t.error_count += 1
|
||||
pkg_log.error(..args, location = loc)
|
||||
}
|
||||
|
||||
@(deprecated="prefer `log.errorf`")
|
||||
errorf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
|
||||
fmt.wprintf(t.w, "%v: ", loc)
|
||||
fmt.wprintf(t.w, format, ..args)
|
||||
fmt.wprintln(t.w)
|
||||
t.error_count += 1
|
||||
pkg_log.errorf(format, ..args, location = loc)
|
||||
}
|
||||
|
||||
fail :: proc(t: ^T, loc := #caller_location) {
|
||||
error(t, "FAIL", loc=loc)
|
||||
t.error_count += 1
|
||||
pkg_log.error("FAIL", location=loc)
|
||||
}
|
||||
|
||||
fail_now :: proc(t: ^T, msg := "", loc := #caller_location) {
|
||||
if msg != "" {
|
||||
error(t, "FAIL:", msg, loc=loc)
|
||||
pkg_log.error("FAIL:", msg, location=loc)
|
||||
} else {
|
||||
error(t, "FAIL", loc=loc)
|
||||
pkg_log.error("FAIL", location=loc)
|
||||
}
|
||||
t.error_count += 1
|
||||
if t._fail_now != nil {
|
||||
t._fail_now()
|
||||
}
|
||||
@@ -69,32 +81,34 @@ failed :: proc(t: ^T) -> bool {
|
||||
return t.error_count != 0
|
||||
}
|
||||
|
||||
@(deprecated="prefer `log.info`")
|
||||
log :: proc(t: ^T, args: ..any, loc := #caller_location) {
|
||||
fmt.wprintln(t.w, ..args)
|
||||
pkg_log.info(..args, location = loc)
|
||||
}
|
||||
|
||||
@(deprecated="prefer `log.infof`")
|
||||
logf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
|
||||
fmt.wprintf(t.w, format, ..args)
|
||||
fmt.wprintln(t.w)
|
||||
pkg_log.infof(format, ..args, location = loc)
|
||||
}
|
||||
|
||||
|
||||
// cleanup registers a procedure and user_data, which will be called when the test, and all its subtests, complete
|
||||
// cleanup procedures will be called in LIFO (last added, first called) order.
|
||||
// cleanup registers a procedure and user_data, which will be called when the test, and all its subtests, complete.
|
||||
// Cleanup procedures will be called in LIFO (last added, first called) order.
|
||||
// Each procedure will use a copy of the context at the time of registering.
|
||||
cleanup :: proc(t: ^T, procedure: proc(rawptr), user_data: rawptr) {
|
||||
append(&t.cleanups, Internal_Cleanup{procedure, user_data})
|
||||
append(&t.cleanups, Internal_Cleanup{procedure, user_data, context})
|
||||
}
|
||||
|
||||
expect :: proc(t: ^T, ok: bool, msg: string = "", loc := #caller_location) -> bool {
|
||||
if !ok {
|
||||
error(t, msg, loc=loc)
|
||||
pkg_log.error(msg, location=loc)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
expectf :: proc(t: ^T, ok: bool, format: string, args: ..any, loc := #caller_location) -> bool {
|
||||
if !ok {
|
||||
errorf(t, format, ..args, loc=loc)
|
||||
pkg_log.errorf(format, ..args, location=loc)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
@@ -102,12 +116,15 @@ expectf :: proc(t: ^T, ok: bool, format: string, args: ..any, loc := #caller_loc
|
||||
expect_value :: proc(t: ^T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
|
||||
ok := value == expected || reflect.is_nil(value) && reflect.is_nil(expected)
|
||||
if !ok {
|
||||
errorf(t, "expected %v, got %v", expected, value, loc=loc)
|
||||
pkg_log.errorf("expected %v, got %v", expected, value, location=loc)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
|
||||
set_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) {
|
||||
_fail_timeout(t, duration, loc)
|
||||
chan.send(t.channel, Event_Set_Fail_Timeout {
|
||||
at_time = time.time_add(time.now(), duration),
|
||||
location = loc,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -162,8 +162,6 @@ parse_qt_linguist_file :: proc(filename: string, options := DEFAULT_PARSE_OPTION
|
||||
context.allocator = allocator
|
||||
|
||||
data, data_ok := os.read_entire_file(filename)
|
||||
defer delete(data)
|
||||
|
||||
if !data_ok { return {}, .File_Error }
|
||||
|
||||
return parse_qt_linguist_from_bytes(data, options, pluralizer, allocator)
|
||||
|
||||
+118
-15
@@ -44,6 +44,29 @@ Pool :: struct {
|
||||
tasks_done: [dynamic]Task,
|
||||
}
|
||||
|
||||
Pool_Thread_Data :: struct {
|
||||
pool: ^Pool,
|
||||
task: Task,
|
||||
}
|
||||
|
||||
@(private="file")
|
||||
pool_thread_runner :: proc(t: ^Thread) {
|
||||
data := cast(^Pool_Thread_Data)t.data
|
||||
pool := data.pool
|
||||
|
||||
for intrinsics.atomic_load(&pool.is_running) {
|
||||
sync.wait(&pool.sem_available)
|
||||
|
||||
if task, ok := pool_pop_waiting(pool); ok {
|
||||
data.task = task
|
||||
pool_do_work(pool, task)
|
||||
data.task = {}
|
||||
}
|
||||
}
|
||||
|
||||
sync.post(&pool.sem_available, 1)
|
||||
}
|
||||
|
||||
// Once initialized, the pool's memory address is not allowed to change until
|
||||
// it is destroyed.
|
||||
//
|
||||
@@ -58,21 +81,11 @@ pool_init :: proc(pool: ^Pool, allocator: mem.Allocator, thread_count: int) {
|
||||
pool.is_running = true
|
||||
|
||||
for _, i in pool.threads {
|
||||
t := create(proc(t: ^Thread) {
|
||||
pool := (^Pool)(t.data)
|
||||
|
||||
for intrinsics.atomic_load(&pool.is_running) {
|
||||
sync.wait(&pool.sem_available)
|
||||
|
||||
if task, ok := pool_pop_waiting(pool); ok {
|
||||
pool_do_work(pool, task)
|
||||
}
|
||||
}
|
||||
|
||||
sync.post(&pool.sem_available, 1)
|
||||
})
|
||||
t := create(pool_thread_runner)
|
||||
data := new(Pool_Thread_Data)
|
||||
data.pool = pool
|
||||
t.user_index = i
|
||||
t.data = pool
|
||||
t.data = data
|
||||
pool.threads[i] = t
|
||||
}
|
||||
}
|
||||
@@ -82,6 +95,8 @@ pool_destroy :: proc(pool: ^Pool) {
|
||||
delete(pool.tasks_done)
|
||||
|
||||
for &t in pool.threads {
|
||||
data := cast(^Pool_Thread_Data)t.data
|
||||
free(data, pool.allocator)
|
||||
destroy(t)
|
||||
}
|
||||
|
||||
@@ -103,7 +118,7 @@ pool_join :: proc(pool: ^Pool) {
|
||||
|
||||
yield()
|
||||
|
||||
started_count: int
|
||||
started_count: int
|
||||
for started_count < len(pool.threads) {
|
||||
started_count = 0
|
||||
for t in pool.threads {
|
||||
@@ -138,6 +153,94 @@ pool_add_task :: proc(pool: ^Pool, allocator: mem.Allocator, procedure: Task_Pro
|
||||
sync.post(&pool.sem_available, 1)
|
||||
}
|
||||
|
||||
// Forcibly stop a running task by its user index.
|
||||
//
|
||||
// This will terminate the underlying thread. Ideally, you should use some
|
||||
// means of communication to stop a task, as thread termination may leave
|
||||
// resources unclaimed.
|
||||
//
|
||||
// The thread will be restarted to accept new tasks.
|
||||
//
|
||||
// Returns true if the task was found and terminated.
|
||||
pool_stop_task :: proc(pool: ^Pool, user_index: int, exit_code: int = 1) -> bool {
|
||||
sync.guard(&pool.mutex)
|
||||
|
||||
for t, i in pool.threads {
|
||||
data := cast(^Pool_Thread_Data)t.data
|
||||
if data.task.user_index == user_index && data.task.procedure != nil {
|
||||
terminate(t, exit_code)
|
||||
|
||||
append(&pool.tasks_done, data.task)
|
||||
intrinsics.atomic_add(&pool.num_done, 1)
|
||||
intrinsics.atomic_sub(&pool.num_outstanding, 1)
|
||||
intrinsics.atomic_sub(&pool.num_in_processing, 1)
|
||||
|
||||
destroy(t)
|
||||
|
||||
replacement := create(pool_thread_runner)
|
||||
replacement.user_index = t.user_index
|
||||
replacement.data = data
|
||||
data.task = {}
|
||||
pool.threads[i] = replacement
|
||||
|
||||
start(replacement)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Forcibly stop all running tasks.
|
||||
//
|
||||
// The same notes from `pool_stop_task` apply here.
|
||||
pool_stop_all_tasks :: proc(pool: ^Pool, exit_code: int = 1) {
|
||||
sync.guard(&pool.mutex)
|
||||
|
||||
for t, i in pool.threads {
|
||||
data := cast(^Pool_Thread_Data)t.data
|
||||
if data.task.procedure != nil {
|
||||
terminate(t, exit_code)
|
||||
|
||||
append(&pool.tasks_done, data.task)
|
||||
intrinsics.atomic_add(&pool.num_done, 1)
|
||||
intrinsics.atomic_sub(&pool.num_outstanding, 1)
|
||||
intrinsics.atomic_sub(&pool.num_in_processing, 1)
|
||||
|
||||
destroy(t)
|
||||
|
||||
replacement := create(pool_thread_runner)
|
||||
replacement.user_index = t.user_index
|
||||
replacement.data = data
|
||||
data.task = {}
|
||||
pool.threads[i] = replacement
|
||||
|
||||
start(replacement)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Force the pool to stop all of its threads and put it into a state where
|
||||
// it will no longer run any more tasks.
|
||||
//
|
||||
// The pool must still be destroyed after this.
|
||||
pool_shutdown :: proc(pool: ^Pool, exit_code: int = 1) {
|
||||
intrinsics.atomic_store(&pool.is_running, false)
|
||||
sync.guard(&pool.mutex)
|
||||
|
||||
for t in pool.threads {
|
||||
terminate(t, exit_code)
|
||||
|
||||
data := cast(^Pool_Thread_Data)t.data
|
||||
if data.task.procedure != nil {
|
||||
append(&pool.tasks_done, data.task)
|
||||
intrinsics.atomic_add(&pool.num_done, 1)
|
||||
intrinsics.atomic_sub(&pool.num_outstanding, 1)
|
||||
intrinsics.atomic_sub(&pool.num_in_processing, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Number of tasks waiting to be processed. Only informational, mostly for
|
||||
// debugging. Don't rely on this value being consistent with other num_*
|
||||
// values.
|
||||
|
||||
@@ -25,6 +25,7 @@ import rbtree "core:container/rbtree"
|
||||
import topological_sort "core:container/topological_sort"
|
||||
|
||||
import crypto "core:crypto"
|
||||
import aes "core:crypto/aes"
|
||||
import blake2b "core:crypto/blake2b"
|
||||
import blake2s "core:crypto/blake2s"
|
||||
import chacha20 "core:crypto/chacha20"
|
||||
@@ -150,6 +151,7 @@ _ :: rbtree
|
||||
_ :: topological_sort
|
||||
_ :: crypto
|
||||
_ :: crypto_hash
|
||||
_ :: aes
|
||||
_ :: blake2b
|
||||
_ :: blake2s
|
||||
_ :: chacha20
|
||||
|
||||
+23
-3
@@ -696,6 +696,12 @@ enum TimingsExportFormat : i32 {
|
||||
TimingsExportCSV = 2,
|
||||
};
|
||||
|
||||
enum DependenciesExportFormat : i32 {
|
||||
DependenciesExportUnspecified = 0,
|
||||
DependenciesExportMake = 1,
|
||||
DependenciesExportJson = 2,
|
||||
};
|
||||
|
||||
enum ErrorPosStyle {
|
||||
ErrorPosStyle_Default, // path(line:column) msg
|
||||
ErrorPosStyle_Unix, // path:line:column: msg
|
||||
@@ -833,6 +839,8 @@ struct BuildContext {
|
||||
bool show_timings;
|
||||
TimingsExportFormat export_timings_format;
|
||||
String export_timings_file;
|
||||
DependenciesExportFormat export_dependencies_format;
|
||||
String export_dependencies_file;
|
||||
bool show_unused;
|
||||
bool show_unused_with_location;
|
||||
bool show_more_timings;
|
||||
@@ -893,7 +901,6 @@ struct BuildContext {
|
||||
u32 cmd_doc_flags;
|
||||
Array<String> extra_packages;
|
||||
|
||||
StringSet test_names;
|
||||
bool test_all_packages;
|
||||
|
||||
gbAffinity affinity;
|
||||
@@ -1032,6 +1039,13 @@ gb_global TargetMetrics target_netbsd_amd64 = {
|
||||
str_lit("x86_64-unknown-netbsd-elf"),
|
||||
};
|
||||
|
||||
gb_global TargetMetrics target_netbsd_arm64 = {
|
||||
TargetOs_netbsd,
|
||||
TargetArch_arm64,
|
||||
8, 8, 16, 16,
|
||||
str_lit("aarch64-unknown-netbsd-elf"),
|
||||
};
|
||||
|
||||
gb_global TargetMetrics target_haiku_amd64 = {
|
||||
TargetOs_haiku,
|
||||
TargetArch_amd64,
|
||||
@@ -1147,8 +1161,10 @@ gb_global NamedTargetMetrics named_targets[] = {
|
||||
{ str_lit("freebsd_amd64"), &target_freebsd_amd64 },
|
||||
{ str_lit("freebsd_arm64"), &target_freebsd_arm64 },
|
||||
|
||||
{ str_lit("openbsd_amd64"), &target_openbsd_amd64 },
|
||||
{ str_lit("netbsd_amd64"), &target_netbsd_amd64 },
|
||||
{ str_lit("netbsd_arm64"), &target_netbsd_arm64 },
|
||||
|
||||
{ str_lit("openbsd_amd64"), &target_openbsd_amd64 },
|
||||
{ str_lit("haiku_amd64"), &target_haiku_amd64 },
|
||||
|
||||
{ str_lit("freestanding_wasm32"), &target_freestanding_wasm32 },
|
||||
@@ -1909,7 +1925,11 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta
|
||||
#elif defined(GB_SYSTEM_OPENBSD)
|
||||
metrics = &target_openbsd_amd64;
|
||||
#elif defined(GB_SYSTEM_NETBSD)
|
||||
metrics = &target_netbsd_amd64;
|
||||
#if defined(GB_CPU_ARM)
|
||||
metrics = &target_netbsd_arm64;
|
||||
#else
|
||||
metrics = &target_netbsd_amd64;
|
||||
#endif
|
||||
#elif defined(GB_SYSTEM_HAIKU)
|
||||
metrics = &target_haiku_amd64;
|
||||
#elif defined(GB_CPU_ARM)
|
||||
|
||||
+72
-24
@@ -1079,7 +1079,7 @@ gb_internal bool check_builtin_simd_operation(CheckerContext *c, Operand *operan
|
||||
return false;
|
||||
}
|
||||
|
||||
gb_internal bool cache_load_file_directive(CheckerContext *c, Ast *call, String const &original_string, bool err_on_not_found, LoadFileCache **cache_) {
|
||||
gb_internal bool cache_load_file_directive(CheckerContext *c, Ast *call, String const &original_string, bool err_on_not_found, LoadFileCache **cache_, LoadFileTier tier) {
|
||||
ast_node(ce, CallExpr, call);
|
||||
ast_node(bd, BasicDirective, ce->proc);
|
||||
String builtin_name = bd->name.string;
|
||||
@@ -1105,12 +1105,16 @@ gb_internal bool cache_load_file_directive(CheckerContext *c, Ast *call, String
|
||||
|
||||
gbFileError file_error = gbFileError_None;
|
||||
String data = {};
|
||||
bool exists = false;
|
||||
LoadFileTier cache_tier = LoadFileTier_Invalid;
|
||||
|
||||
LoadFileCache **cache_ptr = string_map_get(&c->info->load_file_cache, path);
|
||||
LoadFileCache *cache = cache_ptr ? *cache_ptr : nullptr;
|
||||
if (cache) {
|
||||
file_error = cache->file_error;
|
||||
data = cache->data;
|
||||
exists = cache->exists;
|
||||
cache_tier = cache->tier;
|
||||
}
|
||||
defer ({
|
||||
if (cache == nullptr) {
|
||||
@@ -1118,60 +1122,78 @@ gb_internal bool cache_load_file_directive(CheckerContext *c, Ast *call, String
|
||||
new_cache->path = path;
|
||||
new_cache->data = data;
|
||||
new_cache->file_error = file_error;
|
||||
new_cache->exists = exists;
|
||||
new_cache->tier = cache_tier;
|
||||
string_map_init(&new_cache->hashes, 32);
|
||||
string_map_set(&c->info->load_file_cache, path, new_cache);
|
||||
if (cache_) *cache_ = new_cache;
|
||||
} else {
|
||||
cache->data = data;
|
||||
cache->file_error = file_error;
|
||||
cache->exists = exists;
|
||||
cache->tier = cache_tier;
|
||||
if (cache_) *cache_ = cache;
|
||||
}
|
||||
});
|
||||
|
||||
TEMPORARY_ALLOCATOR_GUARD();
|
||||
char *c_str = alloc_cstring(temporary_allocator(), path);
|
||||
if (tier > cache_tier) {
|
||||
cache_tier = tier;
|
||||
|
||||
gbFile f = {};
|
||||
if (cache == nullptr) {
|
||||
TEMPORARY_ALLOCATOR_GUARD();
|
||||
char *c_str = alloc_cstring(temporary_allocator(), path);
|
||||
|
||||
gbFile f = {};
|
||||
file_error = gb_file_open(&f, c_str);
|
||||
defer (gb_file_close(&f));
|
||||
|
||||
if (file_error == gbFileError_None) {
|
||||
exists = true;
|
||||
|
||||
switch(tier) {
|
||||
case LoadFileTier_Exists:
|
||||
// Nothing to do.
|
||||
break;
|
||||
case LoadFileTier_Contents: {
|
||||
isize file_size = cast(isize)gb_file_size(&f);
|
||||
if (file_size > 0) {
|
||||
u8 *ptr = cast(u8 *)gb_alloc(permanent_allocator(), file_size+1);
|
||||
gb_file_read_at(&f, ptr, file_size, 0);
|
||||
ptr[file_size] = '\0';
|
||||
data.text = ptr;
|
||||
data.len = file_size;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
GB_PANIC("Unhandled LoadFileTier");
|
||||
};
|
||||
}
|
||||
}
|
||||
defer (gb_file_close(&f));
|
||||
|
||||
switch (file_error) {
|
||||
default:
|
||||
case gbFileError_Invalid:
|
||||
if (err_on_not_found) {
|
||||
error(ce->proc, "Failed to `#%.*s` file: %s; invalid file or cannot be found", LIT(builtin_name), c_str);
|
||||
error(ce->proc, "Failed to `#%.*s` file: %.*s; invalid file or cannot be found", LIT(builtin_name), LIT(path));
|
||||
}
|
||||
call->state_flags |= StateFlag_DirectiveWasFalse;
|
||||
return false;
|
||||
case gbFileError_NotExists:
|
||||
if (err_on_not_found) {
|
||||
error(ce->proc, "Failed to `#%.*s` file: %s; file cannot be found", LIT(builtin_name), c_str);
|
||||
error(ce->proc, "Failed to `#%.*s` file: %.*s; file cannot be found", LIT(builtin_name), LIT(path));
|
||||
}
|
||||
call->state_flags |= StateFlag_DirectiveWasFalse;
|
||||
return false;
|
||||
case gbFileError_Permission:
|
||||
if (err_on_not_found) {
|
||||
error(ce->proc, "Failed to `#%.*s` file: %s; file permissions problem", LIT(builtin_name), c_str);
|
||||
error(ce->proc, "Failed to `#%.*s` file: %.*s; file permissions problem", LIT(builtin_name), LIT(path));
|
||||
}
|
||||
call->state_flags |= StateFlag_DirectiveWasFalse;
|
||||
return false;
|
||||
case gbFileError_None:
|
||||
// Okay
|
||||
break;
|
||||
}
|
||||
|
||||
if (cache == nullptr) {
|
||||
isize file_size = cast(isize)gb_file_size(&f);
|
||||
if (file_size > 0) {
|
||||
u8 *ptr = cast(u8 *)gb_alloc(permanent_allocator(), file_size+1);
|
||||
gb_file_read_at(&f, ptr, file_size, 0);
|
||||
ptr[file_size] = '\0';
|
||||
data.text = ptr;
|
||||
data.len = file_size;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1263,7 +1285,7 @@ gb_internal LoadDirectiveResult check_load_directive(CheckerContext *c, Operand
|
||||
operand->mode = Addressing_Constant;
|
||||
|
||||
LoadFileCache *cache = nullptr;
|
||||
if (cache_load_file_directive(c, call, o.value.value_string, err_on_not_found, &cache)) {
|
||||
if (cache_load_file_directive(c, call, o.value.value_string, err_on_not_found, &cache, LoadFileTier_Contents)) {
|
||||
operand->value = exact_value_string(cache->data);
|
||||
return LoadDirective_Success;
|
||||
}
|
||||
@@ -1345,6 +1367,8 @@ gb_internal LoadDirectiveResult check_load_directory_directive(CheckerContext *c
|
||||
map_set(&c->info->load_directory_map, call, new_cache);
|
||||
} else {
|
||||
cache->file_error = file_error;
|
||||
|
||||
map_set(&c->info->load_directory_map, call, cache);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1389,7 +1413,7 @@ gb_internal LoadDirectiveResult check_load_directory_directive(CheckerContext *c
|
||||
|
||||
for (FileInfo fi : list) {
|
||||
LoadFileCache *cache = nullptr;
|
||||
if (cache_load_file_directive(c, call, fi.fullpath, err_on_not_found, &cache)) {
|
||||
if (cache_load_file_directive(c, call, fi.fullpath, err_on_not_found, &cache, LoadFileTier_Contents)) {
|
||||
array_add(&file_caches, cache);
|
||||
} else {
|
||||
result = LoadDirective_Error;
|
||||
@@ -1488,6 +1512,30 @@ gb_internal bool check_builtin_procedure_directive(CheckerContext *c, Operand *o
|
||||
|
||||
operand->type = t_source_code_location;
|
||||
operand->mode = Addressing_Value;
|
||||
} else if (name == "exists") {
|
||||
if (ce->args.count != 1) {
|
||||
error(ce->close, "'#exists' expects 1 argument, got %td", ce->args.count);
|
||||
return false;
|
||||
}
|
||||
|
||||
Operand o = {};
|
||||
check_expr(c, &o, ce->args[0]);
|
||||
if (o.mode != Addressing_Constant || !is_type_string(o.type)) {
|
||||
error(ce->args[0], "'#exists' expected a constant string argument");
|
||||
return false;
|
||||
}
|
||||
|
||||
operand->type = t_untyped_bool;
|
||||
operand->mode = Addressing_Constant;
|
||||
|
||||
String original_string = o.value.value_string;
|
||||
LoadFileCache *cache = nullptr;
|
||||
if (cache_load_file_directive(c, call, original_string, /* err_on_not_found=*/ false, &cache, LoadFileTier_Exists)) {
|
||||
operand->value = exact_value_bool(cache->exists);
|
||||
} else {
|
||||
operand->value = exact_value_bool(false);
|
||||
}
|
||||
|
||||
} else if (name == "load") {
|
||||
return check_load_directive(c, operand, call, type_hint, true) == LoadDirective_Success;
|
||||
} else if (name == "load_directory") {
|
||||
@@ -1540,7 +1588,7 @@ gb_internal bool check_builtin_procedure_directive(CheckerContext *c, Operand *o
|
||||
String hash_kind = o_hash.value.value_string;
|
||||
|
||||
LoadFileCache *cache = nullptr;
|
||||
if (cache_load_file_directive(c, call, original_string, true, &cache)) {
|
||||
if (cache_load_file_directive(c, call, original_string, true, &cache, LoadFileTier_Contents)) {
|
||||
MUTEX_GUARD(&c->info->load_file_mutex);
|
||||
// TODO(bill): make these procedures fast :P
|
||||
u64 hash_value = 0;
|
||||
|
||||
+22
-1
@@ -125,6 +125,8 @@ gb_internal Entity *find_polymorphic_record_entity(GenTypesData *found_gen_types
|
||||
|
||||
gb_internal bool complete_soa_type(Checker *checker, Type *t, bool wait_to_finish);
|
||||
|
||||
gb_internal bool check_is_castable_to(CheckerContext *c, Operand *operand, Type *y);
|
||||
|
||||
enum LoadDirectiveResult {
|
||||
LoadDirective_Success = 0,
|
||||
LoadDirective_Error = 1,
|
||||
@@ -2252,6 +2254,17 @@ gb_internal bool check_representable_as_constant(CheckerContext *c, ExactValue i
|
||||
gb_internal bool check_integer_exceed_suggestion(CheckerContext *c, Operand *o, Type *type, i64 max_bit_size=0) {
|
||||
if (is_type_integer(type) && o->value.kind == ExactValue_Integer) {
|
||||
gbString b = type_to_string(type);
|
||||
defer (gb_string_free(b));
|
||||
|
||||
if (is_type_enum(o->type)) {
|
||||
if (check_is_castable_to(c, o, type)) {
|
||||
gbString ot = type_to_string(o->type);
|
||||
error_line("\tSuggestion: Try casting the '%s' expression to '%s'", ot, b);
|
||||
gb_string_free(ot);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
i64 sz = type_size_of(type);
|
||||
i64 bit_size = 8*sz;
|
||||
@@ -2301,7 +2314,6 @@ gb_internal bool check_integer_exceed_suggestion(CheckerContext *c, Operand *o,
|
||||
}
|
||||
}
|
||||
|
||||
gb_string_free(b);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -7408,6 +7420,7 @@ gb_internal ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *c
|
||||
String name = bd->name.string;
|
||||
if (
|
||||
name == "location" ||
|
||||
name == "exists" ||
|
||||
name == "assert" ||
|
||||
name == "panic" ||
|
||||
name == "region" ||
|
||||
@@ -8331,6 +8344,7 @@ gb_internal ExprKind check_basic_directive_expr(CheckerContext *c, Operand *o, A
|
||||
name == "assert" ||
|
||||
name == "defined" ||
|
||||
name == "config" ||
|
||||
name == "exists" ||
|
||||
name == "load" ||
|
||||
name == "load_hash" ||
|
||||
name == "load_directory" ||
|
||||
@@ -8854,6 +8868,10 @@ gb_internal void check_compound_literal_field_values(CheckerContext *c, Slice<As
|
||||
case Type_Array:
|
||||
ft = bt->Array.elem;
|
||||
break;
|
||||
case Type_BitField:
|
||||
is_constant = false;
|
||||
ft = bt->BitField.fields[index]->type;
|
||||
break;
|
||||
default:
|
||||
GB_PANIC("invalid type: %s", type_to_string(ft));
|
||||
break;
|
||||
@@ -8880,6 +8898,9 @@ gb_internal void check_compound_literal_field_values(CheckerContext *c, Slice<As
|
||||
case Type_Array:
|
||||
nested_ft = bt->Array.elem;
|
||||
break;
|
||||
case Type_BitField:
|
||||
nested_ft = bt->BitField.fields[index]->type;
|
||||
break;
|
||||
default:
|
||||
GB_PANIC("invalid type %s", type_to_string(nested_ft));
|
||||
break;
|
||||
|
||||
@@ -5852,35 +5852,6 @@ gb_internal void remove_neighbouring_duplicate_entires_from_sorted_array(Array<E
|
||||
gb_internal void check_test_procedures(Checker *c) {
|
||||
array_sort(c->info.testing_procedures, init_procedures_cmp);
|
||||
remove_neighbouring_duplicate_entires_from_sorted_array(&c->info.testing_procedures);
|
||||
|
||||
if (build_context.test_names.entries.count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
AstPackage *pkg = c->info.init_package;
|
||||
Scope *s = pkg->scope;
|
||||
|
||||
for (String const &name : build_context.test_names) {
|
||||
Entity *e = scope_lookup(s, name);
|
||||
if (e == nullptr) {
|
||||
Token tok = {};
|
||||
if (pkg->files.count != 0) {
|
||||
tok = pkg->files[0]->tokens[0];
|
||||
}
|
||||
error(tok, "Unable to find the test '%.*s' in 'package %.*s' ", LIT(name), LIT(pkg->name));
|
||||
}
|
||||
}
|
||||
|
||||
for (isize i = 0; i < c->info.testing_procedures.count; /**/) {
|
||||
Entity *e = c->info.testing_procedures[i];
|
||||
String name = e->token.string;
|
||||
if (!string_set_exists(&build_context.test_names, name)) {
|
||||
array_ordered_remove(&c->info.testing_procedures, i);
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -336,7 +336,16 @@ struct ObjcMsgData {
|
||||
ObjcMsgKind kind;
|
||||
Type *proc_type;
|
||||
};
|
||||
|
||||
enum LoadFileTier {
|
||||
LoadFileTier_Invalid,
|
||||
LoadFileTier_Exists,
|
||||
LoadFileTier_Contents,
|
||||
};
|
||||
|
||||
struct LoadFileCache {
|
||||
LoadFileTier tier;
|
||||
bool exists;
|
||||
String path;
|
||||
gbFileError file_error;
|
||||
String data;
|
||||
|
||||
@@ -256,6 +256,7 @@ extern "C" {
|
||||
|
||||
#if defined(GB_SYSTEM_NETBSD)
|
||||
#include <stdio.h>
|
||||
#include <lwp.h>
|
||||
#define lseek64 lseek
|
||||
#endif
|
||||
|
||||
@@ -3027,6 +3028,8 @@ gb_inline u32 gb_thread_current_id(void) {
|
||||
thread_id = find_thread(NULL);
|
||||
#elif defined(GB_SYSTEM_FREEBSD)
|
||||
thread_id = pthread_getthreadid_np();
|
||||
#elif defined(GB_SYSTEM_NETBSD)
|
||||
thread_id = (u32)_lwp_self();
|
||||
#else
|
||||
#error Unsupported architecture for gb_thread_current_id()
|
||||
#endif
|
||||
|
||||
@@ -46,6 +46,15 @@ gb_internal LLVMMetadataRef lb_debug_end_location_from_ast(lbProcedure *p, Ast *
|
||||
return lb_debug_location_from_token_pos(p, ast_end_token(node).pos);
|
||||
}
|
||||
|
||||
gb_internal void lb_debug_file_line(lbModule *m, Ast *node, LLVMMetadataRef *file, unsigned *line) {
|
||||
if (*file == nullptr) {
|
||||
if (node) {
|
||||
*file = lb_get_llvm_metadata(m, node->file());
|
||||
*line = cast(unsigned)ast_token(node).pos.line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gb_internal LLVMMetadataRef lb_debug_type_internal_proc(lbModule *m, Type *type) {
|
||||
i64 size = type_size_of(type); // Check size
|
||||
gb_unused(size);
|
||||
@@ -117,6 +126,8 @@ gb_internal LLVMMetadataRef lb_debug_basic_struct(lbModule *m, String const &nam
|
||||
gb_internal LLVMMetadataRef lb_debug_struct(lbModule *m, Type *type, Type *bt, String name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line) {
|
||||
GB_ASSERT(bt->kind == Type_Struct);
|
||||
|
||||
lb_debug_file_line(m, bt->Struct.node, &file, &line);
|
||||
|
||||
unsigned tag = DW_TAG_structure_type;
|
||||
if (is_type_raw_union(bt)) {
|
||||
tag = DW_TAG_union_type;
|
||||
@@ -336,6 +347,8 @@ gb_internal LLVMMetadataRef lb_debug_union(lbModule *m, Type *type, String name,
|
||||
Type *bt = base_type(type);
|
||||
GB_ASSERT(bt->kind == Type_Union);
|
||||
|
||||
lb_debug_file_line(m, bt->Union.node, &file, &line);
|
||||
|
||||
u64 size_in_bits = 8*type_size_of(bt);
|
||||
u32 align_in_bits = 8*cast(u32)type_align_of(bt);
|
||||
|
||||
@@ -415,6 +428,8 @@ gb_internal LLVMMetadataRef lb_debug_bitset(lbModule *m, Type *type, String name
|
||||
Type *bt = base_type(type);
|
||||
GB_ASSERT(bt->kind == Type_BitSet);
|
||||
|
||||
lb_debug_file_line(m, bt->BitSet.node, &file, &line);
|
||||
|
||||
u64 size_in_bits = 8*type_size_of(bt);
|
||||
u32 align_in_bits = 8*cast(u32)type_align_of(bt);
|
||||
|
||||
@@ -494,6 +509,8 @@ gb_internal LLVMMetadataRef lb_debug_enum(lbModule *m, Type *type, String name,
|
||||
Type *bt = base_type(type);
|
||||
GB_ASSERT(bt->kind == Type_Enum);
|
||||
|
||||
lb_debug_file_line(m, bt->Enum.node, &file, &line);
|
||||
|
||||
u64 size_in_bits = 8*type_size_of(bt);
|
||||
u32 align_in_bits = 8*cast(u32)type_align_of(bt);
|
||||
|
||||
|
||||
@@ -4533,10 +4533,26 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) {
|
||||
if (lb_is_nested_possibly_constant(type, sel, elem)) {
|
||||
continue;
|
||||
}
|
||||
lbValue dst = lb_emit_deep_field_gep(p, comp_lit_ptr, sel);
|
||||
field_expr = lb_build_expr(p, elem);
|
||||
field_expr = lb_emit_conv(p, field_expr, sel.entity->type);
|
||||
lb_emit_store(p, dst, field_expr);
|
||||
if (sel.is_bit_field) {
|
||||
Selection sub_sel = trim_selection(sel);
|
||||
lbValue trimmed_dst = lb_emit_deep_field_gep(p, comp_lit_ptr, sub_sel);
|
||||
Type *bf = base_type(type_deref(trimmed_dst.type));
|
||||
if (is_type_pointer(bf)) {
|
||||
trimmed_dst = lb_emit_load(p, trimmed_dst);
|
||||
bf = base_type(type_deref(trimmed_dst.type));
|
||||
}
|
||||
GB_ASSERT(bf->kind == Type_BitField);
|
||||
|
||||
isize idx = sel.index[sel.index.count-1];
|
||||
lbAddr dst = lb_addr_bit_field(trimmed_dst, bf->BitField.fields[idx]->type, bf->BitField.bit_offsets[idx], bf->BitField.bit_sizes[idx]);
|
||||
lb_addr_store(p, dst, field_expr);
|
||||
|
||||
} else {
|
||||
lbValue dst = lb_emit_deep_field_gep(p, comp_lit_ptr, sel);
|
||||
lb_emit_store(p, dst, field_expr);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -1097,15 +1097,7 @@ gb_internal lbValue lb_emit_call(lbProcedure *p, lbValue value, Array<lbValue> c
|
||||
ptr = lb_address_from_load_or_generate_local(p, x);
|
||||
}
|
||||
} else {
|
||||
if (LLVMIsConstant(x.value)) {
|
||||
// NOTE(bill): if the value is already constant, then just it as a global variable
|
||||
// and pass it by pointer
|
||||
lbAddr addr = lb_add_global_generated(p->module, original_type, x);
|
||||
lb_make_global_private_const(addr);
|
||||
ptr = addr.addr;
|
||||
} else {
|
||||
ptr = lb_copy_value_to_ptr(p, x, original_type, 16);
|
||||
}
|
||||
ptr = lb_copy_value_to_ptr(p, x, original_type, 16);
|
||||
}
|
||||
array_add(&processed_args, ptr);
|
||||
}
|
||||
|
||||
+130
-25
@@ -234,6 +234,8 @@ enum BuildFlagKind {
|
||||
BuildFlag_ShowMoreTimings,
|
||||
BuildFlag_ExportTimings,
|
||||
BuildFlag_ExportTimingsFile,
|
||||
BuildFlag_ExportDependencies,
|
||||
BuildFlag_ExportDependenciesFile,
|
||||
BuildFlag_ShowSystemCalls,
|
||||
BuildFlag_ThreadCount,
|
||||
BuildFlag_KeepTempFiles,
|
||||
@@ -276,8 +278,6 @@ enum BuildFlagKind {
|
||||
BuildFlag_RelocMode,
|
||||
BuildFlag_DisableRedZone,
|
||||
|
||||
BuildFlag_TestName,
|
||||
|
||||
BuildFlag_DisallowDo,
|
||||
BuildFlag_DefaultToNilAllocator,
|
||||
BuildFlag_DefaultToPanicAllocator,
|
||||
@@ -320,7 +320,6 @@ enum BuildFlagKind {
|
||||
BuildFlag_Subsystem,
|
||||
#endif
|
||||
|
||||
|
||||
BuildFlag_COUNT,
|
||||
};
|
||||
|
||||
@@ -427,6 +426,8 @@ gb_internal bool parse_build_flags(Array<String> args) {
|
||||
add_flag(&build_flags, BuildFlag_ShowMoreTimings, str_lit("show-more-timings"), BuildFlagParam_None, Command__does_check);
|
||||
add_flag(&build_flags, BuildFlag_ExportTimings, str_lit("export-timings"), BuildFlagParam_String, Command__does_check);
|
||||
add_flag(&build_flags, BuildFlag_ExportTimingsFile, str_lit("export-timings-file"), BuildFlagParam_String, Command__does_check);
|
||||
add_flag(&build_flags, BuildFlag_ExportDependencies, str_lit("export-dependencies"), BuildFlagParam_String, Command__does_build);
|
||||
add_flag(&build_flags, BuildFlag_ExportDependenciesFile, str_lit("export-dependencies-file"), BuildFlagParam_String, Command__does_build);
|
||||
add_flag(&build_flags, BuildFlag_ShowUnused, str_lit("show-unused"), BuildFlagParam_None, Command_check);
|
||||
add_flag(&build_flags, BuildFlag_ShowUnusedWithLocation, str_lit("show-unused-with-location"), BuildFlagParam_None, Command_check);
|
||||
add_flag(&build_flags, BuildFlag_ShowSystemCalls, str_lit("show-system-calls"), BuildFlagParam_None, Command_all);
|
||||
@@ -471,8 +472,6 @@ gb_internal bool parse_build_flags(Array<String> args) {
|
||||
add_flag(&build_flags, BuildFlag_RelocMode, str_lit("reloc-mode"), BuildFlagParam_String, Command__does_build);
|
||||
add_flag(&build_flags, BuildFlag_DisableRedZone, str_lit("disable-red-zone"), BuildFlagParam_None, Command__does_build);
|
||||
|
||||
add_flag(&build_flags, BuildFlag_TestName, str_lit("test-name"), BuildFlagParam_String, Command_test);
|
||||
|
||||
add_flag(&build_flags, BuildFlag_DisallowDo, str_lit("disallow-do"), BuildFlagParam_None, Command__does_check);
|
||||
add_flag(&build_flags, BuildFlag_DefaultToNilAllocator, str_lit("default-to-nil-allocator"), BuildFlagParam_None, Command__does_check);
|
||||
add_flag(&build_flags, BuildFlag_DefaultToPanicAllocator, str_lit("default-to-panic-allocator"),BuildFlagParam_None, Command__does_check);
|
||||
@@ -753,6 +752,36 @@ gb_internal bool parse_build_flags(Array<String> args) {
|
||||
|
||||
break;
|
||||
}
|
||||
case BuildFlag_ExportDependencies: {
|
||||
GB_ASSERT(value.kind == ExactValue_String);
|
||||
|
||||
if (value.value_string == "make") {
|
||||
build_context.export_dependencies_format = DependenciesExportMake;
|
||||
} else if (value.value_string == "json") {
|
||||
build_context.export_dependencies_format = DependenciesExportJson;
|
||||
} else {
|
||||
gb_printf_err("Invalid export format for -export-dependencies:<string>, got %.*s\n", LIT(value.value_string));
|
||||
gb_printf_err("Valid export formats:\n");
|
||||
gb_printf_err("\tmake\n");
|
||||
gb_printf_err("\tjson\n");
|
||||
bad_flags = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case BuildFlag_ExportDependenciesFile: {
|
||||
GB_ASSERT(value.kind == ExactValue_String);
|
||||
|
||||
String export_path = string_trim_whitespace(value.value_string);
|
||||
if (is_build_flag_path_valid(export_path)) {
|
||||
build_context.export_dependencies_file = path_to_full_path(heap_allocator(), export_path);
|
||||
} else {
|
||||
gb_printf_err("Invalid -export-dependencies path, got %.*s\n", LIT(export_path));
|
||||
bad_flags = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case BuildFlag_ShowSystemCalls: {
|
||||
GB_ASSERT(value.kind == ExactValue_Invalid);
|
||||
build_context.show_system_calls = true;
|
||||
@@ -1119,21 +1148,6 @@ gb_internal bool parse_build_flags(Array<String> args) {
|
||||
case BuildFlag_DisableRedZone:
|
||||
build_context.disable_red_zone = true;
|
||||
break;
|
||||
case BuildFlag_TestName: {
|
||||
GB_ASSERT(value.kind == ExactValue_String);
|
||||
{
|
||||
String name = value.value_string;
|
||||
if (!string_is_valid_identifier(name)) {
|
||||
gb_printf_err("Test name '%.*s' must be a valid identifier\n", LIT(name));
|
||||
bad_flags = true;
|
||||
break;
|
||||
}
|
||||
string_set_add(&build_context.test_names, name);
|
||||
|
||||
// NOTE(bill): Allow for multiple -test-name
|
||||
continue;
|
||||
}
|
||||
}
|
||||
case BuildFlag_DisallowDo:
|
||||
build_context.disallow_do = true;
|
||||
break;
|
||||
@@ -1635,6 +1649,74 @@ gb_internal void show_timings(Checker *c, Timings *t) {
|
||||
}
|
||||
}
|
||||
|
||||
gb_internal void export_dependencies(Parser *p) {
|
||||
GB_ASSERT(build_context.export_dependencies_format != DependenciesExportUnspecified);
|
||||
|
||||
if (build_context.export_dependencies_file.len <= 0) {
|
||||
gb_printf_err("No dependency file specified with `-export-dependencies-file`\n");
|
||||
exit_with_errors();
|
||||
return;
|
||||
}
|
||||
|
||||
gbFile f = {};
|
||||
char * fileName = (char *)build_context.export_dependencies_file.text;
|
||||
gbFileError err = gb_file_open_mode(&f, gbFileMode_Write, fileName);
|
||||
if (err != gbFileError_None) {
|
||||
gb_printf_err("Failed to export dependencies to: %s\n", fileName);
|
||||
exit_with_errors();
|
||||
return;
|
||||
}
|
||||
defer (gb_file_close(&f));
|
||||
|
||||
if (build_context.export_dependencies_format == DependenciesExportMake) {
|
||||
String exe_name = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_Output]);
|
||||
defer (gb_free(heap_allocator(), exe_name.text));
|
||||
|
||||
gb_fprintf(&f, "%.*s:", LIT(exe_name));
|
||||
|
||||
isize current_line_length = exe_name.len + 1;
|
||||
|
||||
for(AstPackage *pkg : p->packages) {
|
||||
for(AstFile *file : pkg->files) {
|
||||
/* Arbitrary line break value. Maybe make this better? */
|
||||
if (current_line_length >= 80-2) {
|
||||
gb_file_write(&f, " \\\n ", 4);
|
||||
current_line_length = 1;
|
||||
}
|
||||
|
||||
gb_file_write(&f, " ", 1);
|
||||
current_line_length++;
|
||||
|
||||
for (isize k = 0; k < file->fullpath.len; k++) {
|
||||
char part = file->fullpath.text[k];
|
||||
if (part == ' ') {
|
||||
gb_file_write(&f, "\\", 1);
|
||||
current_line_length++;
|
||||
}
|
||||
gb_file_write(&f, &part, 1);
|
||||
current_line_length++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gb_fprintf(&f, "\n");
|
||||
} else if (build_context.export_dependencies_format == DependenciesExportJson) {
|
||||
gb_fprintf(&f, "{\n");
|
||||
|
||||
gb_fprintf(&f, "\t\"source_files\": [\n");
|
||||
|
||||
for(AstPackage *pkg : p->packages) {
|
||||
for(AstFile *file : pkg->files) {
|
||||
gb_fprintf(&f, "\t\t\"%.*s\",\n", LIT(file->fullpath));
|
||||
}
|
||||
}
|
||||
|
||||
gb_fprintf(&f, "\t],\n");
|
||||
|
||||
gb_fprintf(&f, "}\n");
|
||||
}
|
||||
}
|
||||
|
||||
gb_internal void remove_temp_files(lbGenerator *gen) {
|
||||
if (build_context.keep_temp_files) return;
|
||||
|
||||
@@ -1790,6 +1872,18 @@ gb_internal void print_show_help(String const arg0, String const &command) {
|
||||
print_usage_line(2, "Example: -export-timings-file:timings.json");
|
||||
print_usage_line(0, "");
|
||||
|
||||
print_usage_line(1, "-export-dependencies:<format>");
|
||||
print_usage_line(2, "Exports dependencies to one of a few formats. Requires `-export-dependencies-file`.");
|
||||
print_usage_line(2, "Available options:");
|
||||
print_usage_line(3, "-export-dependencies:make Exports in Makefile format");
|
||||
print_usage_line(3, "-export-dependencies:json Exports in JSON format");
|
||||
print_usage_line(0, "");
|
||||
|
||||
print_usage_line(1, "-export-dependencies-file:<filename>");
|
||||
print_usage_line(2, "Specifies the filename for `-export-dependencies`.");
|
||||
print_usage_line(2, "Example: -export-dependencies-file:dependencies.d");
|
||||
print_usage_line(0, "");
|
||||
|
||||
print_usage_line(1, "-thread-count:<integer>");
|
||||
print_usage_line(2, "Overrides the number of threads the compiler will use to compile with.");
|
||||
print_usage_line(2, "Example: -thread-count:2");
|
||||
@@ -1962,10 +2056,6 @@ gb_internal void print_show_help(String const arg0, String const &command) {
|
||||
}
|
||||
|
||||
if (test_only) {
|
||||
print_usage_line(1, "-test-name:<string>");
|
||||
print_usage_line(2, "Runs specific test only by name.");
|
||||
print_usage_line(0, "");
|
||||
|
||||
print_usage_line(1, "-all-packages");
|
||||
print_usage_line(2, "Tests all packages imported into the given initial package.");
|
||||
print_usage_line(0, "");
|
||||
@@ -2489,7 +2579,6 @@ int main(int arg_count, char const **arg_ptr) {
|
||||
TIME_SECTION("init args");
|
||||
map_init(&build_context.defined_values);
|
||||
build_context.extra_packages.allocator = heap_allocator();
|
||||
string_set_init(&build_context.test_names);
|
||||
|
||||
Array<String> args = setup_args(arg_count, arg_ptr);
|
||||
|
||||
@@ -2657,6 +2746,10 @@ int main(int arg_count, char const **arg_ptr) {
|
||||
gb_printf_err("Expected either a directory or a .odin file, got '%.*s'\n", LIT(init_filename));
|
||||
return 1;
|
||||
}
|
||||
if (!gb_file_exists(cast(const char*)init_filename.text)) {
|
||||
gb_printf_err("The file '%.*s' was not found.\n", LIT(init_filename));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2881,6 +2974,10 @@ int main(int arg_count, char const **arg_ptr) {
|
||||
if (build_context.show_timings) {
|
||||
show_timings(checker, &global_timings);
|
||||
}
|
||||
|
||||
if (build_context.export_dependencies_format != DependenciesExportUnspecified) {
|
||||
export_dependencies(parser);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
break;
|
||||
@@ -2903,6 +3000,10 @@ int main(int arg_count, char const **arg_ptr) {
|
||||
if (build_context.show_timings) {
|
||||
show_timings(checker, &global_timings);
|
||||
}
|
||||
|
||||
if (build_context.export_dependencies_format != DependenciesExportUnspecified) {
|
||||
export_dependencies(parser);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
break;
|
||||
@@ -2916,6 +3017,10 @@ int main(int arg_count, char const **arg_ptr) {
|
||||
show_timings(checker, &global_timings);
|
||||
}
|
||||
|
||||
if (build_context.export_dependencies_format != DependenciesExportUnspecified) {
|
||||
export_dependencies(parser);
|
||||
}
|
||||
|
||||
if (run_output) {
|
||||
String exe_name = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_Output]);
|
||||
defer (gb_free(heap_allocator(), exe_name.text));
|
||||
|
||||
@@ -494,6 +494,8 @@ gb_internal u32 thread_current_id(void) {
|
||||
thread_id = find_thread(NULL);
|
||||
#elif defined(GB_SYSTEM_FREEBSD)
|
||||
thread_id = pthread_getthreadid_np();
|
||||
#elif defined(GB_SYSTEM_NETBSD)
|
||||
thread_id = (u32)_lwp_self();
|
||||
#else
|
||||
#error Unsupported architecture for thread_current_id()
|
||||
#endif
|
||||
|
||||
@@ -457,6 +457,15 @@ gb_internal Selection sub_selection(Selection const &sel, isize offset) {
|
||||
return res;
|
||||
}
|
||||
|
||||
gb_internal Selection trim_selection(Selection const &sel) {
|
||||
Selection res = {};
|
||||
res.index.data = sel.index.data;
|
||||
res.index.count = gb_max(sel.index.count - 1, 0);
|
||||
res.index.capacity = res.index.count;
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
gb_global Type basic_types[] = {
|
||||
{Type_Basic, {Basic_Invalid, 0, 0, STR_LIT("invalid type")}},
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
ODIN=../../odin
|
||||
COMMON=-no-bounds-check -vet -strict-style -define:ODIN_TEST_FANCY=false
|
||||
|
||||
all: crypto_bench \
|
||||
hash_bench
|
||||
|
||||
crypto_bench:
|
||||
$(ODIN) test crypto $(COMMON) -o:speed -out:bench_crypto
|
||||
|
||||
hash_bench:
|
||||
$(ODIN) test hash $(COMMON) -o:speed -out:bench_hash
|
||||
|
||||
clean:
|
||||
rm bench_*
|
||||
@@ -0,0 +1,13 @@
|
||||
@echo off
|
||||
set COMMON=-no-bounds-check -vet -strict-style -define:ODIN_TEST_FANCY=false
|
||||
set PATH_TO_ODIN==..\..\odin
|
||||
|
||||
echo ---
|
||||
echo Running core:crypto benchmarks
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test crypto %COMMON% -o:speed -out:bench_crypto.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:hash benchmarks
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test hash %COMMON% -o:speed -out:bench_hash.exe || exit /b
|
||||
@@ -0,0 +1,356 @@
|
||||
package benchmark_core_crypto
|
||||
|
||||
import "base:runtime"
|
||||
import "core:encoding/hex"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
import "core:time"
|
||||
|
||||
import "core:crypto/aes"
|
||||
import "core:crypto/chacha20"
|
||||
import "core:crypto/chacha20poly1305"
|
||||
import "core:crypto/ed25519"
|
||||
import "core:crypto/poly1305"
|
||||
import "core:crypto/x25519"
|
||||
|
||||
// Cryptographic primitive benchmarks.
|
||||
|
||||
@(test)
|
||||
benchmark_crypto :: proc(t: ^testing.T) {
|
||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||
|
||||
str: strings.Builder
|
||||
strings.builder_init(&str, context.allocator)
|
||||
defer {
|
||||
log.info(strings.to_string(str))
|
||||
strings.builder_destroy(&str)
|
||||
}
|
||||
|
||||
{
|
||||
name := "ChaCha20 64 bytes"
|
||||
options := &time.Benchmark_Options {
|
||||
rounds = 1_000,
|
||||
bytes = 64,
|
||||
setup = _setup_sized_buf,
|
||||
bench = _benchmark_chacha20,
|
||||
teardown = _teardown_sized_buf,
|
||||
}
|
||||
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
|
||||
name = "ChaCha20 1024 bytes"
|
||||
options.bytes = 1024
|
||||
err = time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
|
||||
name = "ChaCha20 65536 bytes"
|
||||
options.bytes = 65536
|
||||
err = time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "Poly1305 64 zero bytes"
|
||||
options := &time.Benchmark_Options {
|
||||
rounds = 1_000,
|
||||
bytes = 64,
|
||||
setup = _setup_sized_buf,
|
||||
bench = _benchmark_poly1305,
|
||||
teardown = _teardown_sized_buf,
|
||||
}
|
||||
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
|
||||
name = "Poly1305 1024 zero bytes"
|
||||
options.bytes = 1024
|
||||
err = time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "chacha20poly1305 64 bytes"
|
||||
options := &time.Benchmark_Options {
|
||||
rounds = 1_000,
|
||||
bytes = 64,
|
||||
setup = _setup_sized_buf,
|
||||
bench = _benchmark_chacha20poly1305,
|
||||
teardown = _teardown_sized_buf,
|
||||
}
|
||||
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
|
||||
name = "chacha20poly1305 1024 bytes"
|
||||
options.bytes = 1024
|
||||
err = time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
|
||||
name = "chacha20poly1305 65536 bytes"
|
||||
options.bytes = 65536
|
||||
err = time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "AES256-GCM 64 bytes"
|
||||
options := &time.Benchmark_Options {
|
||||
rounds = 1_000,
|
||||
bytes = 64,
|
||||
setup = _setup_sized_buf,
|
||||
bench = _benchmark_aes256_gcm,
|
||||
teardown = _teardown_sized_buf,
|
||||
}
|
||||
|
||||
key := [aes.KEY_SIZE_256]byte {
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
}
|
||||
ctx: aes.Context_GCM
|
||||
aes.init_gcm(&ctx, key[:])
|
||||
|
||||
context.user_ptr = &ctx
|
||||
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
|
||||
name = "AES256-GCM 1024 bytes"
|
||||
options.bytes = 1024
|
||||
err = time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
|
||||
name = "AES256-GCM 65536 bytes"
|
||||
options.bytes = 65536
|
||||
err = time.benchmark(options, context.allocator)
|
||||
testing.expect(t, err == nil, name)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
iters :: 10000
|
||||
|
||||
priv_str := "cafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabe"
|
||||
priv_bytes, _ := hex.decode(transmute([]byte)(priv_str), context.temp_allocator)
|
||||
priv_key: ed25519.Private_Key
|
||||
start := time.now()
|
||||
for i := 0; i < iters; i = i + 1 {
|
||||
ok := ed25519.private_key_set_bytes(&priv_key, priv_bytes)
|
||||
assert(ok, "private key should deserialize")
|
||||
}
|
||||
elapsed := time.since(start)
|
||||
fmt.sbprintfln(&str,
|
||||
"ed25519.private_key_set_bytes: ~%f us/op",
|
||||
time.duration_microseconds(elapsed) / iters,
|
||||
)
|
||||
|
||||
pub_bytes := priv_key._pub_key._b[:] // "I know what I am doing"
|
||||
pub_key: ed25519.Public_Key
|
||||
start = time.now()
|
||||
for i := 0; i < iters; i = i + 1 {
|
||||
ok := ed25519.public_key_set_bytes(&pub_key, pub_bytes[:])
|
||||
assert(ok, "public key should deserialize")
|
||||
}
|
||||
elapsed = time.since(start)
|
||||
fmt.sbprintfln(&str,
|
||||
"ed25519.public_key_set_bytes: ~%f us/op",
|
||||
time.duration_microseconds(elapsed) / iters,
|
||||
)
|
||||
|
||||
msg := "Got a job for you, 621."
|
||||
sig_bytes: [ed25519.SIGNATURE_SIZE]byte
|
||||
msg_bytes := transmute([]byte)(msg)
|
||||
start = time.now()
|
||||
for i := 0; i < iters; i = i + 1 {
|
||||
ed25519.sign(&priv_key, msg_bytes, sig_bytes[:])
|
||||
}
|
||||
elapsed = time.since(start)
|
||||
fmt.sbprintfln(&str,
|
||||
"ed25519.sign: ~%f us/op",
|
||||
time.duration_microseconds(elapsed) / iters,
|
||||
)
|
||||
|
||||
start = time.now()
|
||||
for i := 0; i < iters; i = i + 1 {
|
||||
ok := ed25519.verify(&pub_key, msg_bytes, sig_bytes[:])
|
||||
assert(ok, "signature should validate")
|
||||
}
|
||||
elapsed = time.since(start)
|
||||
fmt.sbprintfln(&str,
|
||||
"ed25519.verify: ~%f us/op",
|
||||
time.duration_microseconds(elapsed) / iters,
|
||||
)
|
||||
}
|
||||
{
|
||||
point_str := "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
|
||||
scalar_str := "cafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabe"
|
||||
|
||||
point, _ := hex.decode(transmute([]byte)(point_str), context.temp_allocator)
|
||||
scalar, _ := hex.decode(transmute([]byte)(scalar_str), context.temp_allocator)
|
||||
out: [x25519.POINT_SIZE]byte = ---
|
||||
|
||||
iters :: 10000
|
||||
start := time.now()
|
||||
for i := 0; i < iters; i = i + 1 {
|
||||
x25519.scalarmult(out[:], scalar[:], point[:])
|
||||
}
|
||||
elapsed := time.since(start)
|
||||
|
||||
fmt.sbprintfln(&str,
|
||||
"x25519.scalarmult: ~%f us/op",
|
||||
time.duration_microseconds(elapsed) / iters,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@(private)
|
||||
_setup_sized_buf :: proc(
|
||||
options: ^time.Benchmark_Options,
|
||||
allocator := context.allocator,
|
||||
) -> (
|
||||
err: time.Benchmark_Error,
|
||||
) {
|
||||
assert(options != nil)
|
||||
|
||||
options.input = make([]u8, options.bytes, allocator)
|
||||
return nil if len(options.input) == options.bytes else .Allocation_Error
|
||||
}
|
||||
|
||||
@(private)
|
||||
_teardown_sized_buf :: proc(
|
||||
options: ^time.Benchmark_Options,
|
||||
allocator := context.allocator,
|
||||
) -> (
|
||||
err: time.Benchmark_Error,
|
||||
) {
|
||||
assert(options != nil)
|
||||
|
||||
delete(options.input)
|
||||
return nil
|
||||
}
|
||||
|
||||
@(private)
|
||||
_benchmark_chacha20 :: proc(
|
||||
options: ^time.Benchmark_Options,
|
||||
allocator := context.allocator,
|
||||
) -> (
|
||||
err: time.Benchmark_Error,
|
||||
) {
|
||||
buf := options.input
|
||||
key := [chacha20.KEY_SIZE]byte {
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
}
|
||||
nonce := [chacha20.NONCE_SIZE]byte {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
}
|
||||
|
||||
ctx: chacha20.Context = ---
|
||||
chacha20.init(&ctx, key[:], nonce[:])
|
||||
|
||||
for _ in 0 ..= options.rounds {
|
||||
chacha20.xor_bytes(&ctx, buf, buf)
|
||||
}
|
||||
options.count = options.rounds
|
||||
options.processed = options.rounds * options.bytes
|
||||
return nil
|
||||
}
|
||||
|
||||
@(private)
|
||||
_benchmark_poly1305 :: proc(
|
||||
options: ^time.Benchmark_Options,
|
||||
allocator := context.allocator,
|
||||
) -> (
|
||||
err: time.Benchmark_Error,
|
||||
) {
|
||||
buf := options.input
|
||||
key := [poly1305.KEY_SIZE]byte {
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
}
|
||||
|
||||
tag: [poly1305.TAG_SIZE]byte = ---
|
||||
for _ in 0 ..= options.rounds {
|
||||
poly1305.sum(tag[:], buf, key[:])
|
||||
}
|
||||
options.count = options.rounds
|
||||
options.processed = options.rounds * options.bytes
|
||||
//options.hash = u128(h)
|
||||
return nil
|
||||
}
|
||||
|
||||
@(private)
|
||||
_benchmark_chacha20poly1305 :: proc(
|
||||
options: ^time.Benchmark_Options,
|
||||
allocator := context.allocator,
|
||||
) -> (
|
||||
err: time.Benchmark_Error,
|
||||
) {
|
||||
buf := options.input
|
||||
key := [chacha20.KEY_SIZE]byte {
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
|
||||
}
|
||||
nonce := [chacha20.NONCE_SIZE]byte {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
}
|
||||
|
||||
tag: [chacha20poly1305.TAG_SIZE]byte = ---
|
||||
|
||||
for _ in 0 ..= options.rounds {
|
||||
chacha20poly1305.encrypt(buf, tag[:], key[:], nonce[:], nil, buf)
|
||||
}
|
||||
options.count = options.rounds
|
||||
options.processed = options.rounds * options.bytes
|
||||
return nil
|
||||
}
|
||||
|
||||
_benchmark_aes256_gcm :: proc(
|
||||
options: ^time.Benchmark_Options,
|
||||
allocator := context.allocator,
|
||||
) -> (
|
||||
err: time.Benchmark_Error,
|
||||
) {
|
||||
buf := options.input
|
||||
nonce: [aes.GCM_NONCE_SIZE]byte
|
||||
tag: [aes.GCM_TAG_SIZE]byte = ---
|
||||
|
||||
ctx := transmute(^aes.Context_GCM)context.user_ptr
|
||||
|
||||
for _ in 0 ..= options.rounds {
|
||||
aes.seal_gcm(ctx, buf, tag[:], nonce[:], nil, buf)
|
||||
}
|
||||
options.count = options.rounds
|
||||
options.processed = options.rounds * options.bytes
|
||||
return nil
|
||||
}
|
||||
|
||||
@(private)
|
||||
benchmark_print :: proc(str: ^strings.Builder, name: string, options: ^time.Benchmark_Options, loc := #caller_location) {
|
||||
fmt.sbprintfln(str, "[%v] %v rounds, %v bytes processed in %v ns\n\t\t%5.3f rounds/s, %5.3f MiB/s\n",
|
||||
name,
|
||||
options.rounds,
|
||||
options.processed,
|
||||
time.duration_nanoseconds(options.duration),
|
||||
options.rounds_per_second,
|
||||
options.megabytes_per_second,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
package benchmark_core_hash
|
||||
|
||||
import "core:fmt"
|
||||
import "core:hash/xxhash"
|
||||
import "base:intrinsics"
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
import "core:time"
|
||||
|
||||
@(test)
|
||||
benchmark_hash :: proc(t: ^testing.T) {
|
||||
str: strings.Builder
|
||||
strings.builder_init(&str, context.allocator)
|
||||
defer {
|
||||
fmt.println(strings.to_string(str))
|
||||
strings.builder_destroy(&str)
|
||||
}
|
||||
|
||||
{
|
||||
name := "XXH32 100 zero bytes"
|
||||
options := &time.Benchmark_Options{
|
||||
rounds = 1_000,
|
||||
bytes = 100,
|
||||
setup = setup_xxhash,
|
||||
bench = benchmark_xxh32,
|
||||
teardown = teardown_xxhash,
|
||||
}
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expectf(t, err == nil, "%s failed with err %v", name, err)
|
||||
hash := u128(0x85f6413c)
|
||||
testing.expectf(t, options.hash == hash, "%v hash expected to be %v, got %v", name, hash, options.hash)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "XXH32 1 MiB zero bytes"
|
||||
options := &time.Benchmark_Options{
|
||||
rounds = 1_000,
|
||||
bytes = 1_048_576,
|
||||
setup = setup_xxhash,
|
||||
bench = benchmark_xxh32,
|
||||
teardown = teardown_xxhash,
|
||||
}
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expectf(t, err == nil, "%s failed with err %v", name, err)
|
||||
hash := u128(0x9430f97f)
|
||||
testing.expectf(t, options.hash == hash, "%v hash expected to be %v, got %v", name, hash, options.hash)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "XXH64 100 zero bytes"
|
||||
options := &time.Benchmark_Options{
|
||||
rounds = 1_000,
|
||||
bytes = 100,
|
||||
setup = setup_xxhash,
|
||||
bench = benchmark_xxh64,
|
||||
teardown = teardown_xxhash,
|
||||
}
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expectf(t, err == nil, "%s failed with err %v", name, err)
|
||||
hash := u128(0x17bb1103c92c502f)
|
||||
testing.expectf(t, options.hash == hash, "%v hash expected to be %v, got %v", name, hash, options.hash)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "XXH64 1 MiB zero bytes"
|
||||
options := &time.Benchmark_Options{
|
||||
rounds = 1_000,
|
||||
bytes = 1_048_576,
|
||||
setup = setup_xxhash,
|
||||
bench = benchmark_xxh64,
|
||||
teardown = teardown_xxhash,
|
||||
}
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expectf(t, err == nil, "%s failed with err %v", name, err)
|
||||
hash := u128(0x87d2a1b6e1163ef1)
|
||||
testing.expectf(t, options.hash == hash, "%v hash expected to be %v, got %v", name, hash, options.hash)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "XXH3_64 100 zero bytes"
|
||||
options := &time.Benchmark_Options{
|
||||
rounds = 1_000,
|
||||
bytes = 100,
|
||||
setup = setup_xxhash,
|
||||
bench = benchmark_xxh3_64,
|
||||
teardown = teardown_xxhash,
|
||||
}
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expectf(t, err == nil, "%s failed with err %v", name, err)
|
||||
hash := u128(0x801fedc74ccd608c)
|
||||
testing.expectf(t, options.hash == hash, "%v hash expected to be %v, got %v", name, hash, options.hash)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "XXH3_64 1 MiB zero bytes"
|
||||
options := &time.Benchmark_Options{
|
||||
rounds = 1_000,
|
||||
bytes = 1_048_576,
|
||||
setup = setup_xxhash,
|
||||
bench = benchmark_xxh3_64,
|
||||
teardown = teardown_xxhash,
|
||||
}
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expectf(t, err == nil, "%s failed with err %v", name, err)
|
||||
hash := u128(0x918780b90550bf34)
|
||||
testing.expectf(t, options.hash == hash, "%v hash expected to be %v, got %v", name, hash, options.hash)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "XXH3_128 100 zero bytes"
|
||||
options := &time.Benchmark_Options{
|
||||
rounds = 1_000,
|
||||
bytes = 100,
|
||||
setup = setup_xxhash,
|
||||
bench = benchmark_xxh3_128,
|
||||
teardown = teardown_xxhash,
|
||||
}
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expectf(t, err == nil, "%s failed with err %v", name, err)
|
||||
hash := u128(0x6ba30a4e9dffe1ff801fedc74ccd608c)
|
||||
testing.expectf(t, options.hash == hash, "%v hash expected to be %v, got %v", name, hash, options.hash)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
{
|
||||
name := "XXH3_128 1 MiB zero bytes"
|
||||
options := &time.Benchmark_Options{
|
||||
rounds = 1_000,
|
||||
bytes = 1_048_576,
|
||||
setup = setup_xxhash,
|
||||
bench = benchmark_xxh3_128,
|
||||
teardown = teardown_xxhash,
|
||||
}
|
||||
err := time.benchmark(options, context.allocator)
|
||||
testing.expectf(t, err == nil, "%s failed with err %v", name, err)
|
||||
hash := u128(0xb6ef17a3448492b6918780b90550bf34)
|
||||
testing.expectf(t, options.hash == hash, "%v hash expected to be %v, got %v", name, hash, options.hash)
|
||||
benchmark_print(&str, name, options)
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmarks
|
||||
|
||||
setup_xxhash :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
|
||||
assert(options != nil)
|
||||
|
||||
options.input = make([]u8, options.bytes, allocator)
|
||||
return nil if len(options.input) == options.bytes else .Allocation_Error
|
||||
}
|
||||
|
||||
teardown_xxhash :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
|
||||
assert(options != nil)
|
||||
|
||||
delete(options.input)
|
||||
return nil
|
||||
}
|
||||
|
||||
benchmark_xxh32 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
|
||||
buf := options.input
|
||||
|
||||
h: u32
|
||||
for _ in 0..=options.rounds {
|
||||
h = xxhash.XXH32(buf)
|
||||
}
|
||||
options.count = options.rounds
|
||||
options.processed = options.rounds * options.bytes
|
||||
options.hash = u128(h)
|
||||
return nil
|
||||
}
|
||||
|
||||
benchmark_xxh64 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
|
||||
buf := options.input
|
||||
|
||||
h: u64
|
||||
for _ in 0..=options.rounds {
|
||||
h = xxhash.XXH64(buf)
|
||||
}
|
||||
options.count = options.rounds
|
||||
options.processed = options.rounds * options.bytes
|
||||
options.hash = u128(h)
|
||||
return nil
|
||||
}
|
||||
|
||||
benchmark_xxh3_64 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
|
||||
buf := options.input
|
||||
|
||||
h: u64
|
||||
for _ in 0..=options.rounds {
|
||||
h = xxhash.XXH3_64(buf)
|
||||
}
|
||||
options.count = options.rounds
|
||||
options.processed = options.rounds * options.bytes
|
||||
options.hash = u128(h)
|
||||
return nil
|
||||
}
|
||||
|
||||
benchmark_xxh3_128 :: proc(options: ^time.Benchmark_Options, allocator := context.allocator) -> (err: time.Benchmark_Error) {
|
||||
buf := options.input
|
||||
|
||||
h: u128
|
||||
for _ in 0..=options.rounds {
|
||||
h = xxhash.XXH3_128(buf)
|
||||
}
|
||||
options.count = options.rounds
|
||||
options.processed = options.rounds * options.bytes
|
||||
options.hash = h
|
||||
return nil
|
||||
}
|
||||
|
||||
benchmark_print :: proc(str: ^strings.Builder, name: string, options: ^time.Benchmark_Options, loc := #caller_location) {
|
||||
fmt.sbprintfln(str, "[%v] %v rounds, %v bytes processed in %v ns\n\t\t%5.3f rounds/s, %5.3f MiB/s\n",
|
||||
name,
|
||||
options.rounds,
|
||||
options.processed,
|
||||
time.duration_nanoseconds(options.duration),
|
||||
options.rounds_per_second,
|
||||
options.megabytes_per_second,
|
||||
)
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
// Boilerplate for tests
|
||||
package common
|
||||
|
||||
import "core:testing"
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:strings"
|
||||
|
||||
TEST_count := 0
|
||||
TEST_fail := 0
|
||||
|
||||
when ODIN_TEST {
|
||||
expect :: testing.expect
|
||||
log :: testing.log
|
||||
errorf :: testing.errorf
|
||||
} else {
|
||||
expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
|
||||
TEST_count += 1
|
||||
if !condition {
|
||||
TEST_fail += 1
|
||||
fmt.printf("[%v:%s] FAIL %v\n", loc, loc.procedure, message)
|
||||
return
|
||||
}
|
||||
}
|
||||
errorf :: proc(t: ^testing.T, message: string, args: ..any, loc := #caller_location) {
|
||||
TEST_fail += 1
|
||||
fmt.printf("[%v:%s] Error %v\n", loc, loc.procedure, fmt.tprintf(message, ..args))
|
||||
return
|
||||
}
|
||||
log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
|
||||
fmt.printf("[%v] ", loc)
|
||||
fmt.printf("log: %v\n", v)
|
||||
}
|
||||
}
|
||||
|
||||
report :: proc(t: ^testing.T) {
|
||||
if TEST_fail > 0 {
|
||||
if TEST_fail > 1 {
|
||||
fmt.printf("%v/%v tests successful, %v tests failed.\n", TEST_count - TEST_fail, TEST_count, TEST_fail)
|
||||
} else {
|
||||
fmt.printf("%v/%v tests successful, 1 test failed.\n", TEST_count - TEST_fail, TEST_count)
|
||||
}
|
||||
os.exit(1)
|
||||
} else {
|
||||
fmt.printf("%v/%v tests successful.\n", TEST_count, TEST_count)
|
||||
}
|
||||
}
|
||||
|
||||
// Returns absolute path to `sub_path` where `sub_path` is within the "tests/" sub-directory of the Odin project root
|
||||
// and we're being run from the Odin project root or from a sub-directory of "tests/"
|
||||
// e.g. get_data_path("assets/blah") will return "/Odin_root/tests/assets/blah" if run within "/Odin_root",
|
||||
// "/Odin_root/tests" or "/Odin_root/tests/subdir" etc
|
||||
get_data_path :: proc(t: ^testing.T, sub_path: string) -> (data_path: string) {
|
||||
|
||||
cwd := os.get_current_directory()
|
||||
defer delete(cwd)
|
||||
|
||||
when ODIN_OS == .Windows {
|
||||
norm, was_allocation := strings.replace_all(cwd, "\\", "/")
|
||||
if !was_allocation {
|
||||
norm = strings.clone(norm)
|
||||
}
|
||||
defer delete(norm)
|
||||
} else {
|
||||
norm := cwd
|
||||
}
|
||||
|
||||
last_index := strings.last_index(norm, "/tests/")
|
||||
if last_index == -1 {
|
||||
len := len(norm)
|
||||
if len >= 6 && norm[len-6:] == "/tests" {
|
||||
data_path = fmt.tprintf("%s/%s", norm, sub_path)
|
||||
} else {
|
||||
data_path = fmt.tprintf("%s/tests/%s", norm, sub_path)
|
||||
}
|
||||
} else {
|
||||
data_path = fmt.tprintf("%s/tests/%s", norm[:last_index], sub_path)
|
||||
}
|
||||
|
||||
return data_path
|
||||
}
|
||||
+66
-57
@@ -1,16 +1,15 @@
|
||||
ODIN=../../odin
|
||||
PYTHON=$(shell which python3)
|
||||
COMMON=-vet -strict-style
|
||||
COLLECTION=-collection:tests=..
|
||||
COMMON=-no-bounds-check -vet -strict-style -define:ODIN_TEST_FANCY=false
|
||||
|
||||
all: all_bsd \
|
||||
net_test
|
||||
|
||||
all_bsd: c_libc_test \
|
||||
all_bsd: download_test_assets \
|
||||
c_libc_test \
|
||||
compress_test \
|
||||
container_test \
|
||||
crypto_test \
|
||||
download_test_assets \
|
||||
encoding_test \
|
||||
filepath_test \
|
||||
fmt_test \
|
||||
@@ -21,86 +20,96 @@ all_bsd: c_libc_test \
|
||||
match_test \
|
||||
math_test \
|
||||
noise_test \
|
||||
odin_test \
|
||||
os_exit_test \
|
||||
reflect_test \
|
||||
runtime_test \
|
||||
slice_test \
|
||||
strconv_test \
|
||||
strings_test \
|
||||
thread_test \
|
||||
runtime_test \
|
||||
time_test \
|
||||
fmt_test
|
||||
time_test
|
||||
|
||||
download_test_assets:
|
||||
$(PYTHON) download_assets.py
|
||||
|
||||
image_test:
|
||||
$(ODIN) run image $(COMMON) -out:test_core_image
|
||||
c_libc_test:
|
||||
$(ODIN) test c/libc $(COMMON) -out:test_core_libc
|
||||
|
||||
compress_test:
|
||||
$(ODIN) run compress $(COMMON) -out:test_core_compress
|
||||
$(ODIN) test compress $(COMMON) -out:test_core_compress
|
||||
|
||||
container_test:
|
||||
$(ODIN) run container $(COMMON) $(COLLECTION) -out:test_core_container
|
||||
|
||||
strings_test:
|
||||
$(ODIN) run strings $(COMMON) -out:test_core_strings
|
||||
|
||||
hash_test:
|
||||
$(ODIN) run hash $(COMMON) -o:speed -no-bounds-check -out:test_hash
|
||||
$(ODIN) test container $(COMMON) -out:test_core_container
|
||||
|
||||
crypto_test:
|
||||
$(ODIN) run crypto $(COMMON) $(COLLECTION) -o:speed -no-bounds-check -out:test_crypto
|
||||
|
||||
noise_test:
|
||||
$(ODIN) run math/noise $(COMMON) -out:test_noise
|
||||
$(ODIN) test crypto $(COMMON) -o:speed -out:test_crypto
|
||||
|
||||
encoding_test:
|
||||
$(ODIN) run encoding/hxa $(COMMON) $(COLLECTION) -out:test_hxa
|
||||
$(ODIN) run encoding/json $(COMMON) -out:test_json
|
||||
$(ODIN) run encoding/varint $(COMMON) -out:test_varint
|
||||
$(ODIN) run encoding/xml $(COMMON) -out:test_xml
|
||||
$(ODIN) run encoding/cbor $(COMMON) -out:test_cbor
|
||||
$(ODIN) run encoding/hex $(COMMON) -out:test_hex
|
||||
$(ODIN) run encoding/base64 $(COMMON) -out:test_base64
|
||||
|
||||
math_test:
|
||||
$(ODIN) run math $(COMMON) $(COLLECTION) -out:test_core_math
|
||||
|
||||
linalg_glsl_math_test:
|
||||
$(ODIN) run math/linalg/glsl $(COMMON) $(COLLECTION) -out:test_linalg_glsl_math
|
||||
$(ODIN) test encoding/base64 $(COMMON) -out:test_base64
|
||||
$(ODIN) test encoding/cbor $(COMMON) -out:test_cbor
|
||||
$(ODIN) test encoding/hex $(COMMON) -out:test_hex
|
||||
$(ODIN) test encoding/hxa $(COMMON) -out:test_hxa
|
||||
$(ODIN) test encoding/json $(COMMON) -out:test_json
|
||||
$(ODIN) test encoding/varint $(COMMON) -out:test_varint
|
||||
$(ODIN) test encoding/xml $(COMMON) -out:test_xml
|
||||
|
||||
filepath_test:
|
||||
$(ODIN) run path/filepath $(COMMON) $(COLLECTION) -out:test_core_filepath
|
||||
$(ODIN) test path/filepath $(COMMON) -out:test_core_filepath
|
||||
|
||||
reflect_test:
|
||||
$(ODIN) run reflect $(COMMON) $(COLLECTION) -out:test_core_reflect
|
||||
fmt_test:
|
||||
$(ODIN) test fmt $(COMMON) -out:test_core_fmt
|
||||
|
||||
slice_test:
|
||||
$(ODIN) run slice $(COMMON) -out:test_core_slice
|
||||
hash_test:
|
||||
$(ODIN) test hash $(COMMON) -o:speed -out:test_hash
|
||||
|
||||
image_test:
|
||||
$(ODIN) test image $(COMMON) -out:test_core_image
|
||||
|
||||
i18n_test:
|
||||
$(ODIN) test text/i18n $(COMMON) -out:test_core_i18n
|
||||
|
||||
match_test:
|
||||
$(ODIN) test text/match $(COMMON) -out:test_core_match
|
||||
|
||||
math_test:
|
||||
$(ODIN) test math $(COMMON) -out:test_core_math
|
||||
|
||||
linalg_glsl_math_test:
|
||||
$(ODIN) test math/linalg/glsl $(COMMON) -out:test_linalg_glsl_math
|
||||
|
||||
noise_test:
|
||||
$(ODIN) test math/noise $(COMMON) -out:test_noise
|
||||
|
||||
net_test:
|
||||
$(ODIN) test net $(COMMON) -out:test_core_net
|
||||
|
||||
os_exit_test:
|
||||
$(ODIN) run os/test_core_os_exit.odin -file -out:test_core_os_exit && exit 1 || exit 0
|
||||
|
||||
i18n_test:
|
||||
$(ODIN) run text/i18n $(COMMON) -out:test_core_i18n
|
||||
odin_test:
|
||||
$(ODIN) test odin $(COMMON) -out:test_core_odin
|
||||
|
||||
match_test:
|
||||
$(ODIN) run text/match $(COMMON) -out:test_core_match
|
||||
|
||||
c_libc_test:
|
||||
$(ODIN) run c/libc $(COMMON) -out:test_core_libc
|
||||
|
||||
net_test:
|
||||
$(ODIN) run net $(COMMON) -out:test_core_net
|
||||
|
||||
fmt_test:
|
||||
$(ODIN) run fmt $(COMMON) -out:test_core_fmt
|
||||
|
||||
thread_test:
|
||||
$(ODIN) run thread $(COMMON) -out:test_core_thread
|
||||
reflect_test:
|
||||
$(ODIN) test reflect $(COMMON) -out:test_core_reflect
|
||||
|
||||
runtime_test:
|
||||
$(ODIN) run runtime $(COMMON) -out:test_core_runtime
|
||||
$(ODIN) test runtime $(COMMON) -out:test_core_runtime
|
||||
|
||||
slice_test:
|
||||
$(ODIN) test slice $(COMMON) -out:test_core_slice
|
||||
|
||||
strconv_test:
|
||||
$(ODIN) test strconv $(COMMON) -out:test_core_strconv
|
||||
|
||||
strings_test:
|
||||
$(ODIN) test strings $(COMMON) -out:test_core_strings
|
||||
|
||||
thread_test:
|
||||
$(ODIN) test thread $(COMMON) -out:test_core_thread
|
||||
|
||||
time_test:
|
||||
$(ODIN) run time $(COMMON) -out:test_core_time
|
||||
$(ODIN) test time $(COMMON) -out:test_core_time
|
||||
|
||||
clean:
|
||||
rm test_*
|
||||
+81
-67
@@ -1,110 +1,124 @@
|
||||
@echo off
|
||||
set COMMON=-no-bounds-check -vet -strict-style
|
||||
set COLLECTION=-collection:tests=..
|
||||
set COMMON=-no-bounds-check -vet -strict-style -define:ODIN_TEST_FANCY=false
|
||||
set PATH_TO_ODIN==..\..\odin
|
||||
python3 download_assets.py
|
||||
echo ---
|
||||
echo Running core:c/libc tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test c\libc %COMMON% -out:test_libc.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:compress tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run compress %COMMON% -out:test_core_compress.exe || exit /b
|
||||
%PATH_TO_ODIN% test compress %COMMON% -out:test_core_compress.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:container tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run container %COMMON% %COLLECTION% -out:test_core_container.exe || exit /b
|
||||
%PATH_TO_ODIN% test container %COMMON% -out:test_core_container.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:crypto tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run crypto %COMMON% %COLLECTION% -out:test_crypto.exe || exit /b
|
||||
%PATH_TO_ODIN% test crypto %COMMON% -o:speed -out:test_crypto.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:encoding tests
|
||||
echo ---
|
||||
rem %PATH_TO_ODIN% run encoding/hxa %COMMON% %COLLECTION% -out:test_hxa.exe || exit /b
|
||||
%PATH_TO_ODIN% run encoding/json %COMMON% -out:test_json.exe || exit /b
|
||||
%PATH_TO_ODIN% run encoding/varint %COMMON% -out:test_varint.exe || exit /b
|
||||
%PATH_TO_ODIN% run encoding/xml %COMMON% -out:test_xml.exe || exit /b
|
||||
%PATH_TO_ODIN% test encoding/cbor %COMMON% -out:test_cbor.exe || exit /b
|
||||
%PATH_TO_ODIN% run encoding/hex %COMMON% -out:test_hex.exe || exit /b
|
||||
%PATH_TO_ODIN% run encoding/base64 %COMMON% -out:test_base64.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:fmt tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run fmt %COMMON% %COLLECTION% -out:test_core_fmt.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:hash tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run hash %COMMON% -o:size -out:test_core_hash.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:image tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run image %COMMON% -out:test_core_image.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:math tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run math %COMMON% %COLLECTION% -out:test_core_math.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:math/linalg/glsl tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run math/linalg/glsl %COMMON% %COLLECTION% -out:test_linalg_glsl.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:math/noise tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run math/noise %COMMON% -out:test_noise.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:net
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run net %COMMON% -out:test_core_net.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:odin tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run odin %COMMON% -o:size -out:test_core_odin.exe || exit /b
|
||||
%PATH_TO_ODIN% test encoding/base64 %COMMON% -out:test_base64.exe || exit /b
|
||||
%PATH_TO_ODIN% test encoding/cbor %COMMON% -out:test_cbor.exe || exit /b
|
||||
%PATH_TO_ODIN% test encoding/hex %COMMON% -out:test_hex.exe || exit /b
|
||||
%PATH_TO_ODIN% test encoding/hxa %COMMON% -out:test_hxa.exe || exit /b
|
||||
%PATH_TO_ODIN% test encoding/json %COMMON% -out:test_json.exe || exit /b
|
||||
%PATH_TO_ODIN% test encoding/varint %COMMON% -out:test_varint.exe || exit /b
|
||||
%PATH_TO_ODIN% test encoding/xml %COMMON% -out:test_xml.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:path/filepath tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run path/filepath %COMMON% %COLLECTION% -out:test_core_filepath.exe || exit /b
|
||||
%PATH_TO_ODIN% test path/filepath %COMMON% -out:test_core_filepath.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:reflect tests
|
||||
echo Running core:fmt tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run reflect %COMMON% %COLLECTION% -out:test_core_reflect.exe || exit /b
|
||||
%PATH_TO_ODIN% test fmt %COMMON% -out:test_core_fmt.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:runtime tests
|
||||
echo Running core:hash tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run runtime %COMMON% %COLLECTION% -out:test_core_runtime.exe || exit /b
|
||||
%PATH_TO_ODIN% test hash %COMMON% -o:speed -out:test_core_hash.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:slice tests
|
||||
echo Running core:image tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run slice %COMMON% -out:test_core_slice.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:strings tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run strings %COMMON% -out:test_core_strings.exe || exit /b
|
||||
%PATH_TO_ODIN% test image %COMMON% -out:test_core_image.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:text/i18n tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run text\i18n %COMMON% -out:test_core_i18n.exe || exit /b
|
||||
%PATH_TO_ODIN% test text\i18n %COMMON% -out:test_core_i18n.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running text:match tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test text/match %COMMON% -out:test_core_match.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:math tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test math %COMMON% -out:test_core_math.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:math/linalg/glsl tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test math/linalg/glsl %COMMON% -out:test_linalg_glsl.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:math/noise tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test math/noise %COMMON% -out:test_noise.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:net
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test net %COMMON% -out:test_core_net.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:odin tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test odin %COMMON% -o:size -out:test_core_odin.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:reflect tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test reflect %COMMON% -out:test_core_reflect.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:runtime tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test runtime %COMMON% -out:test_core_runtime.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:slice tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test slice %COMMON% -out:test_core_slice.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:strconv tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test strconv %COMMON% -out:test_core_strconv.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:strings tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% test strings %COMMON% -out:test_core_strings.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:thread tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run thread %COMMON% %COLLECTION% -out:test_core_thread.exe || exit /b
|
||||
%PATH_TO_ODIN% test thread %COMMON% -out:test_core_thread.exe || exit /b
|
||||
|
||||
echo ---
|
||||
echo Running core:time tests
|
||||
echo ---
|
||||
%PATH_TO_ODIN% run time %COMMON% %COLLECTION% -out:test_core_time.exe || exit /b
|
||||
%PATH_TO_ODIN% test time %COMMON% -out:test_core_time.exe || exit /b
|
||||
@@ -1,36 +0,0 @@
|
||||
package test_core_libc
|
||||
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:testing"
|
||||
|
||||
TEST_count := 0
|
||||
TEST_fail := 0
|
||||
|
||||
when ODIN_TEST {
|
||||
expect :: testing.expect
|
||||
log :: testing.log
|
||||
} else {
|
||||
expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
|
||||
TEST_count += 1
|
||||
if !condition {
|
||||
TEST_fail += 1
|
||||
fmt.printf("[%v] %v\n", loc, message)
|
||||
return
|
||||
}
|
||||
}
|
||||
log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
|
||||
fmt.printf("[%v] ", loc)
|
||||
fmt.printf("log: %v\n", v)
|
||||
}
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
t := testing.T{}
|
||||
test_libc_complex(&t)
|
||||
|
||||
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
||||
if TEST_fail > 0 {
|
||||
os.exit(1)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package test_core_libc
|
||||
|
||||
import "core:testing"
|
||||
import "core:fmt"
|
||||
import "core:c/libc"
|
||||
import "core:log"
|
||||
|
||||
reldiff :: proc(lhs, rhs: $T) -> f64 {
|
||||
if lhs == rhs {
|
||||
@@ -14,7 +14,7 @@ reldiff :: proc(lhs, rhs: $T) -> f64 {
|
||||
return out
|
||||
}
|
||||
|
||||
isclose :: proc(lhs, rhs: $T, rtol:f64 = 1e-12, atol:f64 = 1e-12) -> bool {
|
||||
isclose :: proc(t: ^testing.T, lhs, rhs: $T, rtol:f64 = 1e-12, atol:f64 = 1e-12) -> bool {
|
||||
adiff := f64(abs(lhs - rhs))
|
||||
if adiff < atol {
|
||||
return true
|
||||
@@ -23,7 +23,7 @@ isclose :: proc(lhs, rhs: $T, rtol:f64 = 1e-12, atol:f64 = 1e-12) -> bool {
|
||||
if rdiff < rtol {
|
||||
return true
|
||||
}
|
||||
fmt.printf("not close -- lhs:%v rhs:%v -- adiff:%e rdiff:%e\n",lhs, rhs, adiff, rdiff)
|
||||
log.infof("not close -- lhs:%v rhs:%v -- adiff:%e rdiff:%e\n",lhs, rhs, adiff, rdiff)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -44,7 +44,6 @@ test_libc_complex :: proc(t: ^testing.T) {
|
||||
test_libc_pow_binding(t, libc.complex_float, f32, libc_powf, 1e-12, 1e-5)
|
||||
}
|
||||
|
||||
@test
|
||||
test_libc_pow_binding :: proc(t: ^testing.T, $LIBC_COMPLEX:typeid, $F:typeid, pow: proc(LIBC_COMPLEX, LIBC_COMPLEX) -> LIBC_COMPLEX,
|
||||
rtol: f64, atol: f64) {
|
||||
// Tests that c/libc/pow(f) functions have two arguments and that the function works as expected for simple inputs
|
||||
@@ -56,8 +55,8 @@ test_libc_pow_binding :: proc(t: ^testing.T, $LIBC_COMPLEX:typeid, $F:typeid, po
|
||||
for n in -4..=4 {
|
||||
complex_power := LIBC_COMPLEX(complex(F(n), F(0.)))
|
||||
result := pow(complex_base, complex_power)
|
||||
expect(t, isclose(expected_real, F(real(result)), rtol, atol), fmt.tprintf("ftype:%T, n:%v reldiff(%v, re(%v)) is greater than specified rtol:%e", F{}, n, expected_real, result, rtol))
|
||||
expect(t, isclose(expected_imag, F(imag(result)), rtol, atol), fmt.tprintf("ftype:%T, n:%v reldiff(%v, im(%v)) is greater than specified rtol:%e", F{}, n, expected_imag, result, rtol))
|
||||
testing.expectf(t, isclose(t, expected_real, F(real(result)), rtol, atol), "ftype:%T, n:%v reldiff(%v, re(%v)) is greater than specified rtol:%e", F{}, n, expected_real, result, rtol)
|
||||
testing.expectf(t, isclose(t, expected_imag, F(imag(result)), rtol, atol), "ftype:%T, n:%v reldiff(%v, im(%v)) is greater than specified rtol:%e", F{}, n, expected_imag, result, rtol)
|
||||
expected_real *= 2
|
||||
}
|
||||
}
|
||||
@@ -83,8 +82,8 @@ test_libc_pow_binding :: proc(t: ^testing.T, $LIBC_COMPLEX:typeid, $F:typeid, po
|
||||
expected_real = 0.
|
||||
expected_imag = -value
|
||||
}
|
||||
expect(t, isclose(expected_real, F(real(result)), rtol, atol), fmt.tprintf("ftype:%T, n:%v reldiff(%v, re(%v)) is greater than specified rtol:%e", F{}, n, expected_real, result, rtol))
|
||||
expect(t, isclose(expected_imag, F(imag(result)), rtol, atol), fmt.tprintf("ftype:%T, n:%v reldiff(%v, im(%v)) is greater than specified rtol:%e", F{}, n, expected_imag, result, rtol))
|
||||
testing.expectf(t, isclose(t, expected_real, F(real(result)), rtol, atol), "ftype:%T, n:%v reldiff(%v, re(%v)) is greater than specified rtol:%e", F{}, n, expected_real, result, rtol)
|
||||
testing.expectf(t, isclose(t, expected_imag, F(imag(result)), rtol, atol), "ftype:%T, n:%v reldiff(%v, im(%v)) is greater than specified rtol:%e", F{}, n, expected_imag, result, rtol)
|
||||
value *= 2
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,47 +15,7 @@ import "core:testing"
|
||||
import "core:compress/zlib"
|
||||
import "core:compress/gzip"
|
||||
import "core:compress/shoco"
|
||||
|
||||
import "core:bytes"
|
||||
import "core:fmt"
|
||||
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
import "core:io"
|
||||
|
||||
TEST_count := 0
|
||||
TEST_fail := 0
|
||||
|
||||
when ODIN_TEST {
|
||||
expect :: testing.expect
|
||||
log :: testing.log
|
||||
} else {
|
||||
expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
|
||||
TEST_count += 1
|
||||
if !condition {
|
||||
TEST_fail += 1
|
||||
fmt.printf("[%v] %v\n", loc, message)
|
||||
return
|
||||
}
|
||||
}
|
||||
log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
|
||||
fmt.printf("[%v] ", loc)
|
||||
fmt.printf("log: %v\n", v)
|
||||
}
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
w := io.to_writer(os.stream_from_handle(os.stdout))
|
||||
t := testing.T{w=w}
|
||||
zlib_test(&t)
|
||||
gzip_test(&t)
|
||||
shoco_test(&t)
|
||||
|
||||
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
||||
if TEST_fail > 0 {
|
||||
os.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@test
|
||||
zlib_test :: proc(t: ^testing.T) {
|
||||
@@ -80,26 +40,14 @@ zlib_test :: proc(t: ^testing.T) {
|
||||
}
|
||||
|
||||
buf: bytes.Buffer
|
||||
err := zlib.inflate(ODIN_DEMO, &buf)
|
||||
|
||||
track: mem.Tracking_Allocator
|
||||
mem.tracking_allocator_init(&track, context.allocator)
|
||||
context.allocator = mem.tracking_allocator(&track)
|
||||
|
||||
err := zlib.inflate(ODIN_DEMO, &buf)
|
||||
|
||||
expect(t, err == nil, "ZLIB failed to decompress ODIN_DEMO")
|
||||
testing.expect(t, err == nil, "ZLIB failed to decompress ODIN_DEMO")
|
||||
s := bytes.buffer_to_string(&buf)
|
||||
|
||||
expect(t, s[68] == 240 && s[69] == 159 && s[70] == 152, "ZLIB result should've contained 😃 at position 68.")
|
||||
|
||||
expect(t, len(s) == 438, "ZLIB result has an unexpected length.")
|
||||
|
||||
testing.expect(t, s[68] == 240 && s[69] == 159 && s[70] == 152, "ZLIB result should've contained 😃 at position 68.")
|
||||
testing.expect(t, len(s) == 438, "ZLIB result has an unexpected length.")
|
||||
bytes.buffer_destroy(&buf)
|
||||
|
||||
for _, v in track.allocation_map {
|
||||
error := fmt.tprintf("ZLIB test leaked %v bytes", v.size)
|
||||
expect(t, false, error)
|
||||
}
|
||||
}
|
||||
|
||||
@test
|
||||
@@ -117,24 +65,12 @@ gzip_test :: proc(t: ^testing.T) {
|
||||
}
|
||||
|
||||
buf: bytes.Buffer
|
||||
err := gzip.load(TEST, &buf)
|
||||
|
||||
track: mem.Tracking_Allocator
|
||||
mem.tracking_allocator_init(&track, context.allocator)
|
||||
context.allocator = mem.tracking_allocator(&track)
|
||||
|
||||
err := gzip.load(TEST, &buf) // , 438);
|
||||
|
||||
expect(t, err == nil, "GZIP failed to decompress TEST")
|
||||
s := bytes.buffer_to_string(&buf)
|
||||
|
||||
expect(t, s == "payload", "GZIP result wasn't 'payload'")
|
||||
testing.expect(t, err == nil, "GZIP failed to decompress TEST")
|
||||
testing.expect(t, bytes.buffer_to_string(&buf) == "payload", "GZIP result wasn't 'payload'")
|
||||
|
||||
bytes.buffer_destroy(&buf)
|
||||
|
||||
for _, v in track.allocation_map {
|
||||
error := fmt.tprintf("GZIP test leaked %v bytes", v.size)
|
||||
expect(t, false, error)
|
||||
}
|
||||
}
|
||||
|
||||
@test
|
||||
@@ -168,31 +104,26 @@ shoco_test :: proc(t: ^testing.T) {
|
||||
defer delete(buffer)
|
||||
|
||||
size, err := shoco.decompress(v.compressed, buffer[:])
|
||||
msg := fmt.tprintf("Expected `decompress` to return `nil`, got %v", err)
|
||||
expect(t, err == nil, msg)
|
||||
testing.expectf(t, err == nil, "Expected `decompress` to return `nil`, got %v", err)
|
||||
|
||||
msg = fmt.tprintf("Decompressed %v bytes into %v. Expected to decompress into %v bytes.", len(v.compressed), size, expected_raw)
|
||||
expect(t, size == expected_raw, msg)
|
||||
expect(t, string(buffer[:size]) == string(v.raw), "Decompressed contents don't match.")
|
||||
testing.expectf(t, size == expected_raw, "Decompressed %v bytes into %v. Expected to decompress into %v bytes", len(v.compressed), size, expected_raw)
|
||||
testing.expect(t, string(buffer[:size]) == string(v.raw), "Decompressed contents don't match")
|
||||
|
||||
size, err = shoco.compress(string(v.raw), buffer[:])
|
||||
expect(t, err == nil, "Expected `compress` to return `nil`.")
|
||||
testing.expect(t, err == nil, "Expected `compress` to return `nil`.")
|
||||
|
||||
msg = fmt.tprintf("Compressed %v bytes into %v. Expected to compress into %v bytes.", expected_raw, size, expected_compressed)
|
||||
expect(t, size == expected_compressed, msg)
|
||||
testing.expectf(t, size == expected_compressed, "Compressed %v bytes into %v. Expected to compress into %v bytes", expected_raw, size, expected_compressed)
|
||||
|
||||
size, err = shoco.decompress(v.compressed, buffer[:expected_raw - 10])
|
||||
msg = fmt.tprintf("Decompressing into too small a buffer returned %v, expected `.Output_Too_Short`", err)
|
||||
expect(t, err == .Output_Too_Short, msg)
|
||||
testing.expectf(t, err == .Output_Too_Short, "Decompressing into too small a buffer returned %v, expected `.Output_Too_Short`", err)
|
||||
|
||||
size, err = shoco.compress(string(v.raw), buffer[:expected_compressed - 10])
|
||||
msg = fmt.tprintf("Compressing into too small a buffer returned %v, expected `.Output_Too_Short`", err)
|
||||
expect(t, err == .Output_Too_Short, msg)
|
||||
testing.expectf(t, err == .Output_Too_Short, "Compressing into too small a buffer returned %v, expected `.Output_Too_Short`", err)
|
||||
|
||||
size, err = shoco.decompress(v.compressed[:v.short_pack], buffer[:])
|
||||
expect(t, err == .Stream_Too_Short, "Expected `decompress` to return `Stream_Too_Short` because there was no more data after selecting a pack.")
|
||||
testing.expectf(t, err == .Stream_Too_Short, "Insufficient data after pack returned %v, expected `.Stream_Too_Short`", err)
|
||||
|
||||
size, err = shoco.decompress(v.compressed[:v.short_sentinel], buffer[:])
|
||||
expect(t, err == .Stream_Too_Short, "Expected `decompress` to return `Stream_Too_Short` because there was no more data after non-ASCII sentinel.")
|
||||
testing.expectf(t, err == .Stream_Too_Short, "No more data after non-ASCII sentinel returned %v, expected `.Stream_Too_Short`", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,53 +4,54 @@ import "core:container/avl"
|
||||
import "core:math/rand"
|
||||
import "core:slice"
|
||||
import "core:testing"
|
||||
import "core:fmt"
|
||||
import tc "tests:common"
|
||||
import "core:log"
|
||||
|
||||
@(test)
|
||||
test_avl :: proc(t: ^testing.T) {
|
||||
tc.log(t, fmt.tprintf("Testing avl, using random seed %v, add -define:RANDOM_SEED=%v to reuse it.", random_seed, random_seed))
|
||||
log.infof("Testing avl using random seed %v.", t.seed)
|
||||
|
||||
// Initialization.
|
||||
tree: avl.Tree(int)
|
||||
avl.init(&tree, slice.cmp_proc(int))
|
||||
tc.expect(t, avl.len(&tree) == 0, "empty: len should be 0")
|
||||
tc.expect(t, avl.first(&tree) == nil, "empty: first should be nil")
|
||||
tc.expect(t, avl.last(&tree) == nil, "empty: last should be nil")
|
||||
testing.expect(t, avl.len(&tree) == 0, "empty: len should be 0")
|
||||
testing.expect(t, avl.first(&tree) == nil, "empty: first should be nil")
|
||||
testing.expect(t, avl.last(&tree) == nil, "empty: last should be nil")
|
||||
|
||||
iter := avl.iterator(&tree, avl.Direction.Forward)
|
||||
tc.expect(t, avl.iterator_get(&iter) == nil, "empty/iterator: first node should be nil")
|
||||
testing.expect(t, avl.iterator_get(&iter) == nil, "empty/iterator: first node should be nil")
|
||||
|
||||
r: rand.Rand
|
||||
rand.init(&r, random_seed)
|
||||
rand.init(&r, t.seed)
|
||||
|
||||
// Test insertion.
|
||||
NR_INSERTS :: 32 + 1 // Ensure at least 1 collision.
|
||||
inserted_map := make(map[int]^avl.Node(int))
|
||||
defer delete(inserted_map)
|
||||
for i := 0; i < NR_INSERTS; i += 1 {
|
||||
v := int(rand.uint32(&r) & 0x1f)
|
||||
existing_node, in_map := inserted_map[v]
|
||||
|
||||
n, ok, _ := avl.find_or_insert(&tree, v)
|
||||
tc.expect(t, in_map != ok, "insert: ok should match inverse of map lookup")
|
||||
testing.expect(t, in_map != ok, "insert: ok should match inverse of map lookup")
|
||||
if ok {
|
||||
inserted_map[v] = n
|
||||
} else {
|
||||
tc.expect(t, existing_node == n, "insert: expecting existing node")
|
||||
testing.expect(t, existing_node == n, "insert: expecting existing node")
|
||||
}
|
||||
}
|
||||
nrEntries := len(inserted_map)
|
||||
tc.expect(t, avl.len(&tree) == nrEntries, "insert: len after")
|
||||
testing.expect(t, avl.len(&tree) == nrEntries, "insert: len after")
|
||||
validate_avl(t, &tree)
|
||||
|
||||
// Ensure that all entries can be found.
|
||||
for k, v in inserted_map {
|
||||
tc.expect(t, v == avl.find(&tree, k), "Find(): Node")
|
||||
tc.expect(t, k == v.value, "Find(): Node value")
|
||||
testing.expect(t, v == avl.find(&tree, k), "Find(): Node")
|
||||
testing.expect(t, k == v.value, "Find(): Node value")
|
||||
}
|
||||
|
||||
// Test the forward/backward iterators.
|
||||
inserted_values: [dynamic]int
|
||||
defer delete(inserted_values)
|
||||
for k in inserted_map {
|
||||
append(&inserted_values, k)
|
||||
}
|
||||
@@ -60,38 +61,38 @@ test_avl :: proc(t: ^testing.T) {
|
||||
visited: int
|
||||
for node in avl.iterator_next(&iter) {
|
||||
v, idx := node.value, visited
|
||||
tc.expect(t, inserted_values[idx] == v, "iterator/forward: value")
|
||||
tc.expect(t, node == avl.iterator_get(&iter), "iterator/forward: get")
|
||||
testing.expect(t, inserted_values[idx] == v, "iterator/forward: value")
|
||||
testing.expect(t, node == avl.iterator_get(&iter), "iterator/forward: get")
|
||||
visited += 1
|
||||
}
|
||||
tc.expect(t, visited == nrEntries, "iterator/forward: visited")
|
||||
testing.expect(t, visited == nrEntries, "iterator/forward: visited")
|
||||
|
||||
slice.reverse(inserted_values[:])
|
||||
iter = avl.iterator(&tree, avl.Direction.Backward)
|
||||
visited = 0
|
||||
for node in avl.iterator_next(&iter) {
|
||||
v, idx := node.value, visited
|
||||
tc.expect(t, inserted_values[idx] == v, "iterator/backward: value")
|
||||
testing.expect(t, inserted_values[idx] == v, "iterator/backward: value")
|
||||
visited += 1
|
||||
}
|
||||
tc.expect(t, visited == nrEntries, "iterator/backward: visited")
|
||||
testing.expect(t, visited == nrEntries, "iterator/backward: visited")
|
||||
|
||||
// Test removal.
|
||||
rand.shuffle(inserted_values[:], &r)
|
||||
for v, i in inserted_values {
|
||||
node := avl.find(&tree, v)
|
||||
tc.expect(t, node != nil, "remove: find (pre)")
|
||||
testing.expect(t, node != nil, "remove: find (pre)")
|
||||
|
||||
ok := avl.remove(&tree, v)
|
||||
tc.expect(t, ok, "remove: succeeds")
|
||||
tc.expect(t, nrEntries - (i + 1) == avl.len(&tree), "remove: len (post)")
|
||||
testing.expect(t, ok, "remove: succeeds")
|
||||
testing.expect(t, nrEntries - (i + 1) == avl.len(&tree), "remove: len (post)")
|
||||
validate_avl(t, &tree)
|
||||
|
||||
tc.expect(t, nil == avl.find(&tree, v), "remove: find (post")
|
||||
testing.expect(t, nil == avl.find(&tree, v), "remove: find (post")
|
||||
}
|
||||
tc.expect(t, avl.len(&tree) == 0, "remove: len should be 0")
|
||||
tc.expect(t, avl.first(&tree) == nil, "remove: first should be nil")
|
||||
tc.expect(t, avl.last(&tree) == nil, "remove: last should be nil")
|
||||
testing.expect(t, avl.len(&tree) == 0, "remove: len should be 0")
|
||||
testing.expect(t, avl.first(&tree) == nil, "remove: first should be nil")
|
||||
testing.expect(t, avl.last(&tree) == nil, "remove: last should be nil")
|
||||
|
||||
// Refill the tree.
|
||||
for v in inserted_values {
|
||||
@@ -104,25 +105,25 @@ test_avl :: proc(t: ^testing.T) {
|
||||
v := node.value
|
||||
|
||||
ok := avl.iterator_remove(&iter)
|
||||
tc.expect(t, ok, "iterator/remove: success")
|
||||
testing.expect(t, ok, "iterator/remove: success")
|
||||
|
||||
ok = avl.iterator_remove(&iter)
|
||||
tc.expect(t, !ok, "iterator/remove: redundant removes should fail")
|
||||
testing.expect(t, !ok, "iterator/remove: redundant removes should fail")
|
||||
|
||||
tc.expect(t, avl.find(&tree, v) == nil, "iterator/remove: node should be gone")
|
||||
tc.expect(t, avl.iterator_get(&iter) == nil, "iterator/remove: get should return nil")
|
||||
testing.expect(t, avl.find(&tree, v) == nil, "iterator/remove: node should be gone")
|
||||
testing.expect(t, avl.iterator_get(&iter) == nil, "iterator/remove: get should return nil")
|
||||
|
||||
// Ensure that iterator_next still works.
|
||||
node, ok = avl.iterator_next(&iter)
|
||||
tc.expect(t, ok == (avl.len(&tree) > 0), "iterator/remove: next should return false")
|
||||
tc.expect(t, node == avl.first(&tree), "iterator/remove: next should return first")
|
||||
testing.expect(t, ok == (avl.len(&tree) > 0), "iterator/remove: next should return false")
|
||||
testing.expect(t, node == avl.first(&tree), "iterator/remove: next should return first")
|
||||
|
||||
validate_avl(t, &tree)
|
||||
}
|
||||
tc.expect(t, avl.len(&tree) == nrEntries - 1, "iterator/remove: len should drop by 1")
|
||||
testing.expect(t, avl.len(&tree) == nrEntries - 1, "iterator/remove: len should drop by 1")
|
||||
|
||||
avl.destroy(&tree)
|
||||
tc.expect(t, avl.len(&tree) == 0, "destroy: len should be 0")
|
||||
testing.expect(t, avl.len(&tree) == 0, "destroy: len should be 0")
|
||||
}
|
||||
|
||||
@(private)
|
||||
@@ -141,10 +142,10 @@ tree_check_invariants :: proc(
|
||||
}
|
||||
|
||||
// Validate the parent pointer.
|
||||
tc.expect(t, parent == node._parent, "invalid parent pointer")
|
||||
testing.expect(t, parent == node._parent, "invalid parent pointer")
|
||||
|
||||
// Validate that the balance factor is -1, 0, 1.
|
||||
tc.expect(
|
||||
testing.expect(
|
||||
t,
|
||||
node._balance == -1 || node._balance == 0 || node._balance == 1,
|
||||
"invalid balance factor",
|
||||
@@ -155,7 +156,7 @@ tree_check_invariants :: proc(
|
||||
r_height := tree_check_invariants(t, tree, node._right, node)
|
||||
|
||||
// Validate the AVL invariant and the balance factor.
|
||||
tc.expect(t, int(node._balance) == r_height - l_height, "AVL balance factor invariant violated")
|
||||
testing.expect(t, int(node._balance) == r_height - l_height, "AVL balance factor invariant violated")
|
||||
if l_height > r_height {
|
||||
return l_height + 1
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package test_core_container
|
||||
|
||||
import "core:fmt"
|
||||
import "core:testing"
|
||||
|
||||
import tc "tests:common"
|
||||
|
||||
expect_equal :: proc(t: ^testing.T, the_slice, expected: []int, loc := #caller_location) {
|
||||
_eq :: proc(a, b: []int) -> bool {
|
||||
if len(a) != len(b) do return false
|
||||
for a, i in a {
|
||||
if b[i] != a do return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
tc.expect(t, _eq(the_slice, expected), fmt.tprintf("Expected %v, got %v\n", the_slice, expected), loc)
|
||||
}
|
||||
|
||||
main :: proc() {
|
||||
t := testing.T{}
|
||||
|
||||
test_avl(&t)
|
||||
test_rbtree(&t)
|
||||
test_small_array(&t)
|
||||
tc.report(&t)
|
||||
}
|
||||
@@ -3,14 +3,10 @@ package test_core_container
|
||||
import rb "core:container/rbtree"
|
||||
import "core:math/rand"
|
||||
import "core:testing"
|
||||
import "core:fmt"
|
||||
import "base:intrinsics"
|
||||
import "core:mem"
|
||||
import "core:slice"
|
||||
import tc "tests:common"
|
||||
|
||||
RANDOM_SEED :: #config(RANDOM_SEED, 0)
|
||||
random_seed := u64(intrinsics.read_cycle_counter()) when RANDOM_SEED == 0 else u64(RANDOM_SEED)
|
||||
import "core:log"
|
||||
|
||||
test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
|
||||
track: mem.Tracking_Allocator
|
||||
@@ -19,17 +15,17 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
|
||||
context.allocator = mem.tracking_allocator(&track)
|
||||
|
||||
r: rand.Rand
|
||||
rand.init(&r, random_seed)
|
||||
rand.init(&r, t.seed)
|
||||
|
||||
tc.log(t, fmt.tprintf("Testing Red-Black Tree($Key=%v,$Value=%v), using random seed %v, add -define:RANDOM_SEED=%v to reuse it.", type_info_of(Key), type_info_of(Value), random_seed, random_seed))
|
||||
log.infof("Testing Red-Black Tree($Key=%v,$Value=%v) using random seed %v.", type_info_of(Key), type_info_of(Value), t.seed)
|
||||
tree: rb.Tree(Key, Value)
|
||||
rb.init(&tree)
|
||||
|
||||
tc.expect(t, rb.len(&tree) == 0, "empty: len should be 0")
|
||||
tc.expect(t, rb.first(&tree) == nil, "empty: first should be nil")
|
||||
tc.expect(t, rb.last(&tree) == nil, "empty: last should be nil")
|
||||
testing.expect(t, rb.len(&tree) == 0, "empty: len should be 0")
|
||||
testing.expect(t, rb.first(&tree) == nil, "empty: first should be nil")
|
||||
testing.expect(t, rb.last(&tree) == nil, "empty: last should be nil")
|
||||
iter := rb.iterator(&tree, .Forward)
|
||||
tc.expect(t, rb.iterator_get(&iter) == nil, "empty/iterator: first node should be nil")
|
||||
testing.expect(t, rb.iterator_get(&iter) == nil, "empty/iterator: first node should be nil")
|
||||
|
||||
// Test insertion.
|
||||
NR_INSERTS :: 32 + 1 // Ensure at least 1 collision.
|
||||
@@ -45,27 +41,27 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
|
||||
|
||||
existing_node, in_map := inserted_map[k]
|
||||
n, inserted, _ := rb.find_or_insert(&tree, k, v)
|
||||
tc.expect(t, in_map != inserted, "insert: inserted should match inverse of map lookup")
|
||||
testing.expect(t, in_map != inserted, "insert: inserted should match inverse of map lookup")
|
||||
if inserted {
|
||||
inserted_map[k] = n
|
||||
} else {
|
||||
tc.expect(t, existing_node == n, "insert: expecting existing node")
|
||||
testing.expect(t, existing_node == n, "insert: expecting existing node")
|
||||
}
|
||||
}
|
||||
|
||||
entry_count := len(inserted_map)
|
||||
tc.expect(t, rb.len(&tree) == entry_count, "insert: len after")
|
||||
testing.expect(t, rb.len(&tree) == entry_count, "insert: len after")
|
||||
validate_rbtree(t, &tree)
|
||||
|
||||
first := rb.first(&tree)
|
||||
last := rb.last(&tree)
|
||||
tc.expect(t, first != nil && first.key == min_key, fmt.tprintf("insert: first should be present with key %v", min_key))
|
||||
tc.expect(t, last != nil && last.key == max_key, fmt.tprintf("insert: last should be present with key %v", max_key))
|
||||
testing.expectf(t, first != nil && first.key == min_key, "insert: first should be present with key %v", min_key)
|
||||
testing.expectf(t, last != nil && last.key == max_key, "insert: last should be present with key %v", max_key)
|
||||
|
||||
// Ensure that all entries can be found.
|
||||
for k, v in inserted_map {
|
||||
tc.expect(t, v == rb.find(&tree, k), "Find(): Node")
|
||||
tc.expect(t, k == v.key, "Find(): Node key")
|
||||
testing.expect(t, v == rb.find(&tree, k), "Find(): Node")
|
||||
testing.expect(t, k == v.key, "Find(): Node key")
|
||||
}
|
||||
|
||||
// Test the forward/backward iterators.
|
||||
@@ -79,21 +75,21 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
|
||||
visited: int
|
||||
for node in rb.iterator_next(&iter) {
|
||||
k, idx := node.key, visited
|
||||
tc.expect(t, inserted_keys[idx] == k, "iterator/forward: key")
|
||||
tc.expect(t, node == rb.iterator_get(&iter), "iterator/forward: get")
|
||||
testing.expect(t, inserted_keys[idx] == k, "iterator/forward: key")
|
||||
testing.expect(t, node == rb.iterator_get(&iter), "iterator/forward: get")
|
||||
visited += 1
|
||||
}
|
||||
tc.expect(t, visited == entry_count, "iterator/forward: visited")
|
||||
testing.expect(t, visited == entry_count, "iterator/forward: visited")
|
||||
|
||||
slice.reverse(inserted_keys[:])
|
||||
iter = rb.iterator(&tree, rb.Direction.Backward)
|
||||
visited = 0
|
||||
for node in rb.iterator_next(&iter) {
|
||||
k, idx := node.key, visited
|
||||
tc.expect(t, inserted_keys[idx] == k, "iterator/backward: key")
|
||||
testing.expect(t, inserted_keys[idx] == k, "iterator/backward: key")
|
||||
visited += 1
|
||||
}
|
||||
tc.expect(t, visited == entry_count, "iterator/backward: visited")
|
||||
testing.expect(t, visited == entry_count, "iterator/backward: visited")
|
||||
|
||||
// Test removal (and on_remove callback)
|
||||
rand.shuffle(inserted_keys[:], &r)
|
||||
@@ -104,19 +100,19 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
|
||||
}
|
||||
for k, i in inserted_keys {
|
||||
node := rb.find(&tree, k)
|
||||
tc.expect(t, node != nil, "remove: find (pre)")
|
||||
testing.expect(t, node != nil, "remove: find (pre)")
|
||||
|
||||
ok := rb.remove(&tree, k)
|
||||
tc.expect(t, ok, "remove: succeeds")
|
||||
tc.expect(t, entry_count - (i + 1) == rb.len(&tree), "remove: len (post)")
|
||||
testing.expect(t, ok, "remove: succeeds")
|
||||
testing.expect(t, entry_count - (i + 1) == rb.len(&tree), "remove: len (post)")
|
||||
validate_rbtree(t, &tree)
|
||||
|
||||
tc.expect(t, nil == rb.find(&tree, k), "remove: find (post")
|
||||
testing.expect(t, nil == rb.find(&tree, k), "remove: find (post")
|
||||
}
|
||||
tc.expect(t, rb.len(&tree) == 0, "remove: len should be 0")
|
||||
tc.expect(t, callback_count == 0, fmt.tprintf("remove: on_remove should've been called %v times, it was %v", entry_count, callback_count))
|
||||
tc.expect(t, rb.first(&tree) == nil, "remove: first should be nil")
|
||||
tc.expect(t, rb.last(&tree) == nil, "remove: last should be nil")
|
||||
testing.expect(t, rb.len(&tree) == 0, "remove: len should be 0")
|
||||
testing.expectf(t, callback_count == 0, "remove: on_remove should've been called %v times, it was %v", entry_count, callback_count)
|
||||
testing.expect(t, rb.first(&tree) == nil, "remove: first should be nil")
|
||||
testing.expect(t, rb.last(&tree) == nil, "remove: last should be nil")
|
||||
|
||||
// Refill the tree.
|
||||
for k in inserted_keys {
|
||||
@@ -130,32 +126,32 @@ test_rbtree_integer :: proc(t: ^testing.T, $Key: typeid, $Value: typeid) {
|
||||
k := node.key
|
||||
|
||||
ok := rb.iterator_remove(&iter)
|
||||
tc.expect(t, ok, "iterator/remove: success")
|
||||
testing.expect(t, ok, "iterator/remove: success")
|
||||
|
||||
ok = rb.iterator_remove(&iter)
|
||||
tc.expect(t, !ok, "iterator/remove: redundant removes should fail")
|
||||
testing.expect(t, !ok, "iterator/remove: redundant removes should fail")
|
||||
|
||||
tc.expect(t, rb.find(&tree, k) == nil, "iterator/remove: node should be gone")
|
||||
tc.expect(t, rb.iterator_get(&iter) == nil, "iterator/remove: get should return nil")
|
||||
testing.expect(t, rb.find(&tree, k) == nil, "iterator/remove: node should be gone")
|
||||
testing.expect(t, rb.iterator_get(&iter) == nil, "iterator/remove: get should return nil")
|
||||
|
||||
// Ensure that iterator_next still works.
|
||||
node, ok = rb.iterator_next(&iter)
|
||||
tc.expect(t, ok == (rb.len(&tree) > 0), "iterator/remove: next should return false")
|
||||
tc.expect(t, node == rb.first(&tree), "iterator/remove: next should return first")
|
||||
testing.expect(t, ok == (rb.len(&tree) > 0), "iterator/remove: next should return false")
|
||||
testing.expect(t, node == rb.first(&tree), "iterator/remove: next should return first")
|
||||
|
||||
validate_rbtree(t, &tree)
|
||||
}
|
||||
tc.expect(t, rb.len(&tree) == entry_count - 1, "iterator/remove: len should drop by 1")
|
||||
testing.expect(t, rb.len(&tree) == entry_count - 1, "iterator/remove: len should drop by 1")
|
||||
|
||||
rb.destroy(&tree)
|
||||
tc.expect(t, rb.len(&tree) == 0, "destroy: len should be 0")
|
||||
tc.expect(t, callback_count == 0, fmt.tprintf("remove: on_remove should've been called %v times, it was %v", entry_count, callback_count))
|
||||
testing.expect(t, rb.len(&tree) == 0, "destroy: len should be 0")
|
||||
testing.expectf(t, callback_count == 0, "remove: on_remove should've been called %v times, it was %v", entry_count, callback_count)
|
||||
|
||||
// print_tree_node(tree._root)
|
||||
delete(inserted_map)
|
||||
delete(inserted_keys)
|
||||
tc.expect(t, len(track.allocation_map) == 0, fmt.tprintf("Expected 0 leaks, have %v", len(track.allocation_map)))
|
||||
tc.expect(t, len(track.bad_free_array) == 0, fmt.tprintf("Expected 0 bad frees, have %v", len(track.bad_free_array)))
|
||||
testing.expectf(t, len(track.allocation_map) == 0, "Expected 0 leaks, have %v", len(track.allocation_map))
|
||||
testing.expectf(t, len(track.bad_free_array) == 0, "Expected 0 bad frees, have %v", len(track.bad_free_array))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -194,7 +190,7 @@ validate_rbtree :: proc(t: ^testing.T, tree: ^$T/rb.Tree($Key, $Value)) {
|
||||
}
|
||||
|
||||
verify_rbtree_propery_1 :: proc(t: ^testing.T, n: ^$N/rb.Node($Key, $Value)) {
|
||||
tc.expect(t, rb.node_color(n) == .Black || rb.node_color(n) == .Red, "Property #1: Each node is either red or black.")
|
||||
testing.expect(t, rb.node_color(n) == .Black || rb.node_color(n) == .Red, "Property #1: Each node is either red or black.")
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
@@ -203,14 +199,14 @@ verify_rbtree_propery_1 :: proc(t: ^testing.T, n: ^$N/rb.Node($Key, $Value)) {
|
||||
}
|
||||
|
||||
verify_rbtree_propery_2 :: proc(t: ^testing.T, root: ^$N/rb.Node($Key, $Value)) {
|
||||
tc.expect(t, rb.node_color(root) == .Black, "Property #2: Root node should be black.")
|
||||
testing.expect(t, rb.node_color(root) == .Black, "Property #2: Root node should be black.")
|
||||
}
|
||||
|
||||
verify_rbtree_propery_4 :: proc(t: ^testing.T, n: ^$N/rb.Node($Key, $Value)) {
|
||||
if rb.node_color(n) == .Red {
|
||||
// A red node's left, right and parent should be black
|
||||
all_black := rb.node_color(n._left) == .Black && rb.node_color(n._right) == .Black && rb.node_color(n._parent) == .Black
|
||||
tc.expect(t, all_black, "Property #3: Red node's children + parent must be black.")
|
||||
testing.expect(t, all_black, "Property #3: Red node's children + parent must be black.")
|
||||
}
|
||||
if n == nil {
|
||||
return
|
||||
@@ -233,7 +229,7 @@ verify_rbtree_propery_5_helper :: proc(t: ^testing.T, n: ^$N/rb.Node($Key, $Valu
|
||||
if path_black_count^ == -1 {
|
||||
path_black_count^ = black_count
|
||||
} else {
|
||||
tc.expect(t, black_count == path_black_count^, "Property #5: Paths from a node to its leaves contain same black count.")
|
||||
testing.expect(t, black_count == path_black_count^, "Property #5: Paths from a node to its leaves contain same black count.")
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -241,4 +237,4 @@ verify_rbtree_propery_5_helper :: proc(t: ^testing.T, n: ^$N/rb.Node($Key, $Valu
|
||||
verify_rbtree_propery_5_helper(t, n._right, black_count, path_black_count)
|
||||
}
|
||||
// Properties 4 and 5 together guarantee that no path in the tree is more than about twice as long as any other path,
|
||||
// which guarantees that it has O(log n) height.
|
||||
// which guarantees that it has O(log n) height.
|
||||
|
||||
@@ -3,44 +3,47 @@ package test_core_container
|
||||
import "core:testing"
|
||||
import "core:container/small_array"
|
||||
|
||||
import tc "tests:common"
|
||||
|
||||
@(test)
|
||||
test_small_array :: proc(t: ^testing.T) {
|
||||
tc.log(t, "Testing small_array")
|
||||
|
||||
test_small_array_removes(t)
|
||||
test_small_array_inject_at(t)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_small_array_removes :: proc(t: ^testing.T) {
|
||||
array: small_array.Small_Array(10, int)
|
||||
small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
array: small_array.Small_Array(10, int)
|
||||
small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
|
||||
small_array.ordered_remove(&array, 0)
|
||||
expect_equal(t, small_array.slice(&array), []int { 1, 2, 3, 4, 5, 6, 7, 8, 9 })
|
||||
small_array.ordered_remove(&array, 5)
|
||||
expect_equal(t, small_array.slice(&array), []int { 1, 2, 3, 4, 5, 7, 8, 9 })
|
||||
small_array.ordered_remove(&array, 6)
|
||||
expect_equal(t, small_array.slice(&array), []int { 1, 2, 3, 4, 5, 7, 9 })
|
||||
small_array.unordered_remove(&array, 0)
|
||||
expect_equal(t, small_array.slice(&array), []int { 9, 2, 3, 4, 5, 7 })
|
||||
small_array.unordered_remove(&array, 2)
|
||||
expect_equal(t, small_array.slice(&array), []int { 9, 2, 7, 4, 5 })
|
||||
small_array.unordered_remove(&array, 4)
|
||||
expect_equal(t, small_array.slice(&array), []int { 9, 2, 7, 4 })
|
||||
small_array.ordered_remove(&array, 0)
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 1, 2, 3, 4, 5, 6, 7, 8, 9 }))
|
||||
small_array.ordered_remove(&array, 5)
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 1, 2, 3, 4, 5, 7, 8, 9 }))
|
||||
small_array.ordered_remove(&array, 6)
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 1, 2, 3, 4, 5, 7, 9 }))
|
||||
small_array.unordered_remove(&array, 0)
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 9, 2, 3, 4, 5, 7 }))
|
||||
small_array.unordered_remove(&array, 2)
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 9, 2, 7, 4, 5 }))
|
||||
small_array.unordered_remove(&array, 4)
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 9, 2, 7, 4 }))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_small_array_inject_at :: proc(t: ^testing.T) {
|
||||
array: small_array.Small_Array(13, int)
|
||||
small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
array: small_array.Small_Array(13, int)
|
||||
small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
|
||||
tc.expect(t, small_array.inject_at(&array, 0, 0), "Expected to be able to inject into small array")
|
||||
expect_equal(t, small_array.slice(&array), []int { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })
|
||||
tc.expect(t, small_array.inject_at(&array, 0, 5), "Expected to be able to inject into small array")
|
||||
expect_equal(t, small_array.slice(&array), []int { 0, 0, 1, 2, 3, 0, 4, 5, 6, 7, 8, 9 })
|
||||
tc.expect(t, small_array.inject_at(&array, 0, small_array.len(array)), "Expected to be able to inject into small array")
|
||||
expect_equal(t, small_array.slice(&array), []int { 0, 0, 1, 2, 3, 0, 4, 5, 6, 7, 8, 9, 0 })
|
||||
testing.expect(t, small_array.inject_at(&array, 0, 0), "Expected to be able to inject into small array")
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }))
|
||||
testing.expect(t, small_array.inject_at(&array, 0, 5), "Expected to be able to inject into small array")
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 0, 0, 1, 2, 3, 0, 4, 5, 6, 7, 8, 9 }))
|
||||
testing.expect(t, small_array.inject_at(&array, 0, small_array.len(array)), "Expected to be able to inject into small array")
|
||||
testing.expect(t, slice_equal(small_array.slice(&array), []int { 0, 0, 1, 2, 3, 0, 4, 5, 6, 7, 8, 9, 0 }))
|
||||
}
|
||||
|
||||
slice_equal :: proc(a, b: []int) -> bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for a, i in a {
|
||||
if b[i] != a {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -13,40 +13,20 @@ package test_core_crypto
|
||||
*/
|
||||
|
||||
import "core:encoding/hex"
|
||||
import "core:fmt"
|
||||
import "core:mem"
|
||||
import "core:testing"
|
||||
import "base:runtime"
|
||||
import "core:log"
|
||||
|
||||
import "core:crypto"
|
||||
import "core:crypto/chacha20"
|
||||
import "core:crypto/chacha20poly1305"
|
||||
|
||||
import tc "tests:common"
|
||||
|
||||
main :: proc() {
|
||||
t := testing.T{}
|
||||
|
||||
test_rand_bytes(&t)
|
||||
|
||||
test_hash(&t)
|
||||
test_mac(&t)
|
||||
test_kdf(&t) // After hash/mac tests because those should pass first.
|
||||
test_ecc25519(&t)
|
||||
|
||||
test_chacha20(&t)
|
||||
test_chacha20poly1305(&t)
|
||||
test_sha3_variants(&t)
|
||||
|
||||
bench_crypto(&t)
|
||||
|
||||
tc.report(&t)
|
||||
}
|
||||
|
||||
_PLAINTEXT_SUNSCREEN_STR := "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."
|
||||
|
||||
@(test)
|
||||
test_chacha20 :: proc(t: ^testing.T) {
|
||||
tc.log(t, "Testing (X)ChaCha20")
|
||||
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
|
||||
|
||||
// Test cases taken from RFC 8439, and draft-irtf-cfrg-xchacha-03
|
||||
plaintext := transmute([]byte)(_PLAINTEXT_SUNSCREEN_STR)
|
||||
@@ -89,14 +69,12 @@ test_chacha20 :: proc(t: ^testing.T) {
|
||||
chacha20.xor_bytes(&ctx, derived_ciphertext[:], plaintext[:])
|
||||
|
||||
derived_ciphertext_str := string(hex.encode(derived_ciphertext[:], context.temp_allocator))
|
||||
tc.expect(
|
||||
testing.expectf(
|
||||
t,
|
||||
derived_ciphertext_str == ciphertext_str,
|
||||
fmt.tprintf(
|
||||
"Expected %s for xor_bytes(plaintext_str), but got %s instead",
|
||||
ciphertext_str,
|
||||
derived_ciphertext_str,
|
||||
),
|
||||
"Expected %s for xor_bytes(plaintext_str), but got %s instead",
|
||||
ciphertext_str,
|
||||
derived_ciphertext_str,
|
||||
)
|
||||
|
||||
xkey := [chacha20.KEY_SIZE]byte {
|
||||
@@ -136,21 +114,17 @@ test_chacha20 :: proc(t: ^testing.T) {
|
||||
chacha20.xor_bytes(&ctx, derived_ciphertext[:], plaintext[:])
|
||||
|
||||
derived_ciphertext_str = string(hex.encode(derived_ciphertext[:], context.temp_allocator))
|
||||
tc.expect(
|
||||
testing.expectf(
|
||||
t,
|
||||
derived_ciphertext_str == xciphertext_str,
|
||||
fmt.tprintf(
|
||||
"Expected %s for xor_bytes(plaintext_str), but got %s instead",
|
||||
xciphertext_str,
|
||||
derived_ciphertext_str,
|
||||
),
|
||||
"Expected %s for xor_bytes(plaintext_str), but got %s instead",
|
||||
xciphertext_str,
|
||||
derived_ciphertext_str,
|
||||
)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_chacha20poly1305 :: proc(t: ^testing.T) {
|
||||
tc.log(t, "Testing chacha20poly1205")
|
||||
|
||||
plaintext := transmute([]byte)(_PLAINTEXT_SUNSCREEN_STR)
|
||||
|
||||
aad := [12]byte {
|
||||
@@ -208,25 +182,21 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
|
||||
)
|
||||
|
||||
derived_ciphertext_str := string(hex.encode(derived_ciphertext[:], context.temp_allocator))
|
||||
tc.expect(
|
||||
testing.expectf(
|
||||
t,
|
||||
derived_ciphertext_str == ciphertext_str,
|
||||
fmt.tprintf(
|
||||
"Expected ciphertext %s for encrypt(aad, plaintext), but got %s instead",
|
||||
ciphertext_str,
|
||||
derived_ciphertext_str,
|
||||
),
|
||||
"Expected ciphertext %s for encrypt(aad, plaintext), but got %s instead",
|
||||
ciphertext_str,
|
||||
derived_ciphertext_str,
|
||||
)
|
||||
|
||||
derived_tag_str := string(hex.encode(derived_tag[:], context.temp_allocator))
|
||||
tc.expect(
|
||||
testing.expectf(
|
||||
t,
|
||||
derived_tag_str == tag_str,
|
||||
fmt.tprintf(
|
||||
"Expected tag %s for encrypt(aad, plaintext), but got %s instead",
|
||||
tag_str,
|
||||
derived_tag_str,
|
||||
),
|
||||
"Expected tag %s for encrypt(aad, plaintext), but got %s instead",
|
||||
tag_str,
|
||||
derived_tag_str,
|
||||
)
|
||||
|
||||
derived_plaintext: [114]byte
|
||||
@@ -239,15 +209,13 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
|
||||
ciphertext[:],
|
||||
)
|
||||
derived_plaintext_str := string(derived_plaintext[:])
|
||||
tc.expect(t, ok, "Expected true for decrypt(tag, aad, ciphertext)")
|
||||
tc.expect(
|
||||
testing.expect(t, ok, "Expected true for decrypt(tag, aad, ciphertext)")
|
||||
testing.expectf(
|
||||
t,
|
||||
derived_plaintext_str == _PLAINTEXT_SUNSCREEN_STR,
|
||||
fmt.tprintf(
|
||||
"Expected plaintext %s for decrypt(tag, aad, ciphertext), but got %s instead",
|
||||
_PLAINTEXT_SUNSCREEN_STR,
|
||||
derived_plaintext_str,
|
||||
),
|
||||
"Expected plaintext %s for decrypt(tag, aad, ciphertext), but got %s instead",
|
||||
_PLAINTEXT_SUNSCREEN_STR,
|
||||
derived_plaintext_str,
|
||||
)
|
||||
|
||||
derived_ciphertext[0] ~= 0xa5
|
||||
@@ -259,7 +227,7 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
|
||||
aad[:],
|
||||
derived_ciphertext[:],
|
||||
)
|
||||
tc.expect(t, !ok, "Expected false for decrypt(tag, aad, corrupted_ciphertext)")
|
||||
testing.expect(t, !ok, "Expected false for decrypt(tag, aad, corrupted_ciphertext)")
|
||||
|
||||
aad[0] ~= 0xa5
|
||||
ok = chacha20poly1305.decrypt(
|
||||
@@ -270,15 +238,13 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
|
||||
aad[:],
|
||||
ciphertext[:],
|
||||
)
|
||||
tc.expect(t, !ok, "Expected false for decrypt(tag, corrupted_aad, ciphertext)")
|
||||
testing.expect(t, !ok, "Expected false for decrypt(tag, corrupted_aad, ciphertext)")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_rand_bytes :: proc(t: ^testing.T) {
|
||||
tc.log(t, "Testing rand_bytes")
|
||||
|
||||
if !crypto.HAS_RAND_BYTES {
|
||||
tc.log(t, "rand_bytes not supported - skipping")
|
||||
log.info("rand_bytes not supported - skipping")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -306,10 +272,5 @@ test_rand_bytes :: proc(t: ^testing.T) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
tc.expect(
|
||||
t,
|
||||
seems_ok,
|
||||
"Expected to randomize the head and tail of the buffer within a handful of attempts",
|
||||
)
|
||||
testing.expect(t, seems_ok, "Expected to randomize the head and tail of the buffer within a handful of attempts")
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user