Skip to content

Commit

Permalink
Added tests from local
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfriend99 committed Oct 8, 2024
1 parent 7a265ed commit b59b06a
Show file tree
Hide file tree
Showing 14 changed files with 790 additions and 26 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ target_link_libraries(blade PRIVATE libblade)

add_custom_target(blade_lib_files ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/libs" "${OUTPUT_DIR}/libs"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/tests" "${OUTPUT_DIR}/tests"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/benchmarks" "${OUTPUT_DIR}/benchmarks"
# COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/tests" "${OUTPUT_DIR}/tests"
# COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/benchmarks" "${OUTPUT_DIR}/benchmarks"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/apps" "${OUTPUT_DIR}/apps"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/bins" "${OUTPUT_DIR}"
COMMENT "Exporting libs, tests, and benchmark scripts..."
Expand Down
11 changes: 11 additions & 0 deletions tests/anonymous2.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class A {
var x = 1

@to_string() {
return 'Some random string ${self.x}'
}
}

@{
echo to_string(A())
}()
29 changes: 29 additions & 0 deletions tests/catch_in_class.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class A {
var a = 5

test2() {
echo 'Test 2 called...'
}

test() {
var b = 20
catch {
echo [][10]
} as e

echo 'Finally...'
self.test2()
if self.a {
echo self.a
}
echo b

if e {
echo 'Error occurred:'
echo e.message
echo e.stacktrace
}
}
}

A().test()
10 changes: 10 additions & 0 deletions tests/fib.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def generate_fibonacci(n) {
var fib = [1] * n
for i in 2..n {
fib[i] = fib[i - 2] + fib[i - 1]
}
return fib
}

var fib = generate_fibonacci(35)
print(' '.join(fib))
111 changes: 111 additions & 0 deletions tests/libs/c_closures.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
def test(a, i) {
return to_string(a) + ' ' + i
}

var text = 'all is well'
for i in 0..100 {
echo text.replace_with('/([a-z]+)/', @(match, val) {
if val == 'is' return 'is not'
return test(val, i)
})
}

[10, 20, 30, 40].each(@(x, y, z) {
echo 'It works! ${y} -> ${x}'
})

{name: 'Richard', age: 40}.each(@(x, y, z) {
echo '${y} -> ${x}'
})

bytes([13,219,79]).each(@(b) {
echo b
})

echo [1,2,3].map(@(v) {
return v * 2
})

echo [1,2,3,4,5,6].filter(@(x) { return x % 2 != 0 })

echo [1,2,3,4,5].some(@(x) { return x % 2 != 0 })
echo [1,2,3,4,5].every(@(x) { return x % 2 != 0 })

var people = {
john: {
name: 'John',
age: 40,
address: 'London, England',
},
daniel: {
name: 'Daniel',
age: 31,
address: 'Lagos, Nigeria',
}
}

echo people.filter(@(x) {
return x.address.index_of('Lagos') != -1
})

echo people.some(@(x){ return x.age > 30 })
echo people.every(@(x){ return x.age > 40 })

echo [1, 100].reduce(@(i, x) { return max(i, x) }, 50)
echo [1, 100].reduce(@(i, x) { return max(i, x) })
echo [].reduce(@(i, x) { return max(i, x) })

echo [15, 16, 17, 18, 19].reduce(
@(accumulator, currentValue) { return accumulator + currentValue },
10
)

def reducer(accumulator, currentValue, index) {
var returns = accumulator + currentValue
echo 'accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}'
return returns
}

echo [15, 16, 17, 18, 19].reduce(reducer)

var objects = [{ x: 1 }, { x: 2 }, { x: 3 }]
echo objects.reduce(
@(accumulator, currentValue) { return accumulator + currentValue.x },
0
)

# A LITTLE COMPLEX EXAMPLE

var pipe = @(...) {
var functions = __args__
return @(initialValue) {
return functions.reduce(@(acc, fn) { return fn(acc) }, initialValue)
}
}

# Building blocks to use for composition
var double = @(x) { return 2 * x }
var triple = @(x) { return 3 * x }
var quadruple = @(x) { return 4 * x }

# Composed functions for multiplication of specific values
var multiply6 = pipe(double, triple);
var multiply9 = pipe(triple, triple);
var multiply16 = pipe(quadruple, quadruple);
var multiply24 = pipe(double, triple, quadruple);

# Usage
echo multiply6(6); # 36
echo multiply9(9); # 81
echo multiply16(16); # 256
echo multiply24(10); # 240


echo [1, 2, nil, 4].reduce(@(a, b) { return a + b })

echo {name: 'Richard', age: 40}.reduce(@(x, y, z){ return x += z + ' => ' + y + '\n' }, '')

'name'.replace_with('/m/', @(match, offset) {
echo offset
return match
})
57 changes: 33 additions & 24 deletions tests/libs/clib.b
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@ import clib
import reflect
import struct

var c = clib.load('libc')

var tm = clib.named_struct({
tm_sec: clib.int,
tm_min: clib.int,
tm_hour: clib.int,
tm_mday: clib.int,
tm_mon: clib.int,
tm_year: clib.int,
tm_wday: clib.int,
tm_yday: clib.int,
tm_isdst: clib.int,
})

var time = c.define('time', clib.long, clib.ptr)
var localtime = c.define('localtime', clib.ptr, clib.ptr)

var local_time = localtime(struct.pack('i', time(nil)))
echo clib.get(tm, local_time)

var strftime = c.define('strftime', clib.long, clib.ptr, clib.long, clib.char_ptr, clib.ptr)
var buffer = bytes(80)
echo strftime(buffer, 80, 'Today is %A, %B %d, %Y.', local_time)
echo buffer.to_string()
catch {
var c = clib.load('libc')

var tm = clib.named_struct({
tm_sec: clib.int,
tm_min: clib.int,
tm_hour: clib.int,
tm_mday: clib.int,
tm_mon: clib.int,
tm_year: clib.int,
tm_wday: clib.int,
tm_yday: clib.int,
tm_isdst: clib.int,
})

var time = c.define('time', clib.long, clib.ptr)
var localtime = c.define('localtime', clib.ptr, clib.ptr)

var local_time = localtime(struct.pack('i', time(nil)))
echo clib.get(tm, local_time)

var strftime = c.define('strftime', clib.long, clib.ptr, clib.long, clib.char_ptr, clib.ptr)
var buffer = bytes(80)
echo strftime(buffer, 80, 'Today is %A, %B %d, %Y.', local_time)
echo buffer.to_string()
} as e

if e {
echo 'CLIB TEST ERROR:'
echo '======================================================'
echo e.message
echo e.stacktrace
}


50 changes: 50 additions & 0 deletions tests/libs/imagine.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import imagine {*}
import os

catch {
if !os.dir_exists('./tmp')
os.create_dir('./tmp')

Image.new(130, 20, true).use(@(im) {
im.save_alpha()

var bg_color = im.allocate_color(0, 0, 0, 127)
var fore_color = im.allocate_color(233, 14, 91)

im.fill(0, 0, bg_color)
im.string(5, 2, 'A simple text string', FONT_REGULAR, fore_color)

im.export_png('./tmp/image1.png')
})

Image.new(100, 100).use(@(im) {
var black = im.allocate_color(0, 0, 0)
var white = im.allocate_color(255, 255, 255)

im.fill(0, 0, white)
im.filled_arc(50, 50, 98, 98, 0, 204, black)

im.export_webp('./tmp/image2.webp')
})

Image.new(640, 640, true).use(@(im) {
var bg_color = im.allocate_color(0, 0, 0, 127)
im.fill(0, 0, bg_color)

Image.from_webp('./tmp/image2.webp').use(@(im2) {
var meta = im2.meta()

im.copy_resized(im2, 0, 0, 0, 0, 640, 640, meta.width, meta.height)
})

im.export_png('./tmp/image3.png')
})
} as e

if e {
echo ''
echo 'IMAGINE TEST ERROR:'
echo '======================================================'
echo e.message
echo e.stacktrace
}
34 changes: 34 additions & 0 deletions tests/libs/json_encode.b
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json

echo json.encode('️Hell"s not\'s here')
echo json.encode(1)
echo json.encode(true)
echo json.encode(nil)
echo json.encode(-2.555551688)
echo json.encode({})
echo json.encode({}, false)
echo json.encode([])
echo json.encode([], false)
echo json.encode({name: 'Richard'})
echo json.encode({name: 'Richard'}, false)
echo json.encode([1, 2, 3, 4, 5])
echo json.encode([1, 2, 3, 4, 5], false)
echo json.encode(['apple', 'mango', 'oranges'])
echo json.encode(['apple', 'mango', 'oranges'], false)
echo json.encode([{name: 'Richard'}])
echo json.encode([{name: 'Richard'}], false) # non compact

echo json.encode([{"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959, "Address": "",
"City": "SAN FRANCISCO",
"State": "CA", "Zip": "94107",
"Country": "US"
}, {
"precision": "zip",
"Latitude": 37.371991, "Longitude": -122.026020,
"Address": "",
"City": "SUNNYVALE",
"State": "CA", "Zip": "94085",
"Country": "US"
}], false)
Loading

0 comments on commit b59b06a

Please sign in to comment.