Skip to content

Commit b59b06a

Browse files
committed
Added tests from local
1 parent 7a265ed commit b59b06a

File tree

14 files changed

+790
-26
lines changed

14 files changed

+790
-26
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ target_link_libraries(blade PRIVATE libblade)
203203

204204
add_custom_target(blade_lib_files ALL
205205
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/libs" "${OUTPUT_DIR}/libs"
206-
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/tests" "${OUTPUT_DIR}/tests"
207-
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/benchmarks" "${OUTPUT_DIR}/benchmarks"
206+
# COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/tests" "${OUTPUT_DIR}/tests"
207+
# COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/benchmarks" "${OUTPUT_DIR}/benchmarks"
208208
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/apps" "${OUTPUT_DIR}/apps"
209209
COMMAND ${CMAKE_COMMAND} -E copy_directory "${PROJECT_SOURCE_DIR}/bins" "${OUTPUT_DIR}"
210210
COMMENT "Exporting libs, tests, and benchmark scripts..."

tests/anonymous2.b

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class A {
2+
var x = 1
3+
4+
@to_string() {
5+
return 'Some random string ${self.x}'
6+
}
7+
}
8+
9+
@{
10+
echo to_string(A())
11+
}()

tests/catch_in_class.b

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class A {
2+
var a = 5
3+
4+
test2() {
5+
echo 'Test 2 called...'
6+
}
7+
8+
test() {
9+
var b = 20
10+
catch {
11+
echo [][10]
12+
} as e
13+
14+
echo 'Finally...'
15+
self.test2()
16+
if self.a {
17+
echo self.a
18+
}
19+
echo b
20+
21+
if e {
22+
echo 'Error occurred:'
23+
echo e.message
24+
echo e.stacktrace
25+
}
26+
}
27+
}
28+
29+
A().test()

tests/fib.b

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def generate_fibonacci(n) {
2+
var fib = [1] * n
3+
for i in 2..n {
4+
fib[i] = fib[i - 2] + fib[i - 1]
5+
}
6+
return fib
7+
}
8+
9+
var fib = generate_fibonacci(35)
10+
print(' '.join(fib))

tests/libs/c_closures.b

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
def test(a, i) {
2+
return to_string(a) + ' ' + i
3+
}
4+
5+
var text = 'all is well'
6+
for i in 0..100 {
7+
echo text.replace_with('/([a-z]+)/', @(match, val) {
8+
if val == 'is' return 'is not'
9+
return test(val, i)
10+
})
11+
}
12+
13+
[10, 20, 30, 40].each(@(x, y, z) {
14+
echo 'It works! ${y} -> ${x}'
15+
})
16+
17+
{name: 'Richard', age: 40}.each(@(x, y, z) {
18+
echo '${y} -> ${x}'
19+
})
20+
21+
bytes([13,219,79]).each(@(b) {
22+
echo b
23+
})
24+
25+
echo [1,2,3].map(@(v) {
26+
return v * 2
27+
})
28+
29+
echo [1,2,3,4,5,6].filter(@(x) { return x % 2 != 0 })
30+
31+
echo [1,2,3,4,5].some(@(x) { return x % 2 != 0 })
32+
echo [1,2,3,4,5].every(@(x) { return x % 2 != 0 })
33+
34+
var people = {
35+
john: {
36+
name: 'John',
37+
age: 40,
38+
address: 'London, England',
39+
},
40+
daniel: {
41+
name: 'Daniel',
42+
age: 31,
43+
address: 'Lagos, Nigeria',
44+
}
45+
}
46+
47+
echo people.filter(@(x) {
48+
return x.address.index_of('Lagos') != -1
49+
})
50+
51+
echo people.some(@(x){ return x.age > 30 })
52+
echo people.every(@(x){ return x.age > 40 })
53+
54+
echo [1, 100].reduce(@(i, x) { return max(i, x) }, 50)
55+
echo [1, 100].reduce(@(i, x) { return max(i, x) })
56+
echo [].reduce(@(i, x) { return max(i, x) })
57+
58+
echo [15, 16, 17, 18, 19].reduce(
59+
@(accumulator, currentValue) { return accumulator + currentValue },
60+
10
61+
)
62+
63+
def reducer(accumulator, currentValue, index) {
64+
var returns = accumulator + currentValue
65+
echo 'accumulator: ${accumulator}, currentValue: ${currentValue}, index: ${index}, returns: ${returns}'
66+
return returns
67+
}
68+
69+
echo [15, 16, 17, 18, 19].reduce(reducer)
70+
71+
var objects = [{ x: 1 }, { x: 2 }, { x: 3 }]
72+
echo objects.reduce(
73+
@(accumulator, currentValue) { return accumulator + currentValue.x },
74+
0
75+
)
76+
77+
# A LITTLE COMPLEX EXAMPLE
78+
79+
var pipe = @(...) {
80+
var functions = __args__
81+
return @(initialValue) {
82+
return functions.reduce(@(acc, fn) { return fn(acc) }, initialValue)
83+
}
84+
}
85+
86+
# Building blocks to use for composition
87+
var double = @(x) { return 2 * x }
88+
var triple = @(x) { return 3 * x }
89+
var quadruple = @(x) { return 4 * x }
90+
91+
# Composed functions for multiplication of specific values
92+
var multiply6 = pipe(double, triple);
93+
var multiply9 = pipe(triple, triple);
94+
var multiply16 = pipe(quadruple, quadruple);
95+
var multiply24 = pipe(double, triple, quadruple);
96+
97+
# Usage
98+
echo multiply6(6); # 36
99+
echo multiply9(9); # 81
100+
echo multiply16(16); # 256
101+
echo multiply24(10); # 240
102+
103+
104+
echo [1, 2, nil, 4].reduce(@(a, b) { return a + b })
105+
106+
echo {name: 'Richard', age: 40}.reduce(@(x, y, z){ return x += z + ' => ' + y + '\n' }, '')
107+
108+
'name'.replace_with('/m/', @(match, offset) {
109+
echo offset
110+
return match
111+
})

tests/libs/clib.b

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,38 @@ import clib
22
import reflect
33
import struct
44

5-
var c = clib.load('libc')
6-
7-
var tm = clib.named_struct({
8-
tm_sec: clib.int,
9-
tm_min: clib.int,
10-
tm_hour: clib.int,
11-
tm_mday: clib.int,
12-
tm_mon: clib.int,
13-
tm_year: clib.int,
14-
tm_wday: clib.int,
15-
tm_yday: clib.int,
16-
tm_isdst: clib.int,
17-
})
18-
19-
var time = c.define('time', clib.long, clib.ptr)
20-
var localtime = c.define('localtime', clib.ptr, clib.ptr)
21-
22-
var local_time = localtime(struct.pack('i', time(nil)))
23-
echo clib.get(tm, local_time)
24-
25-
var strftime = c.define('strftime', clib.long, clib.ptr, clib.long, clib.char_ptr, clib.ptr)
26-
var buffer = bytes(80)
27-
echo strftime(buffer, 80, 'Today is %A, %B %d, %Y.', local_time)
28-
echo buffer.to_string()
5+
catch {
6+
var c = clib.load('libc')
7+
8+
var tm = clib.named_struct({
9+
tm_sec: clib.int,
10+
tm_min: clib.int,
11+
tm_hour: clib.int,
12+
tm_mday: clib.int,
13+
tm_mon: clib.int,
14+
tm_year: clib.int,
15+
tm_wday: clib.int,
16+
tm_yday: clib.int,
17+
tm_isdst: clib.int,
18+
})
19+
20+
var time = c.define('time', clib.long, clib.ptr)
21+
var localtime = c.define('localtime', clib.ptr, clib.ptr)
22+
23+
var local_time = localtime(struct.pack('i', time(nil)))
24+
echo clib.get(tm, local_time)
25+
26+
var strftime = c.define('strftime', clib.long, clib.ptr, clib.long, clib.char_ptr, clib.ptr)
27+
var buffer = bytes(80)
28+
echo strftime(buffer, 80, 'Today is %A, %B %d, %Y.', local_time)
29+
echo buffer.to_string()
30+
} as e
31+
32+
if e {
33+
echo 'CLIB TEST ERROR:'
34+
echo '======================================================'
35+
echo e.message
36+
echo e.stacktrace
37+
}
2938

3039

tests/libs/imagine.b

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import imagine {*}
2+
import os
3+
4+
catch {
5+
if !os.dir_exists('./tmp')
6+
os.create_dir('./tmp')
7+
8+
Image.new(130, 20, true).use(@(im) {
9+
im.save_alpha()
10+
11+
var bg_color = im.allocate_color(0, 0, 0, 127)
12+
var fore_color = im.allocate_color(233, 14, 91)
13+
14+
im.fill(0, 0, bg_color)
15+
im.string(5, 2, 'A simple text string', FONT_REGULAR, fore_color)
16+
17+
im.export_png('./tmp/image1.png')
18+
})
19+
20+
Image.new(100, 100).use(@(im) {
21+
var black = im.allocate_color(0, 0, 0)
22+
var white = im.allocate_color(255, 255, 255)
23+
24+
im.fill(0, 0, white)
25+
im.filled_arc(50, 50, 98, 98, 0, 204, black)
26+
27+
im.export_webp('./tmp/image2.webp')
28+
})
29+
30+
Image.new(640, 640, true).use(@(im) {
31+
var bg_color = im.allocate_color(0, 0, 0, 127)
32+
im.fill(0, 0, bg_color)
33+
34+
Image.from_webp('./tmp/image2.webp').use(@(im2) {
35+
var meta = im2.meta()
36+
37+
im.copy_resized(im2, 0, 0, 0, 0, 640, 640, meta.width, meta.height)
38+
})
39+
40+
im.export_png('./tmp/image3.png')
41+
})
42+
} as e
43+
44+
if e {
45+
echo ''
46+
echo 'IMAGINE TEST ERROR:'
47+
echo '======================================================'
48+
echo e.message
49+
echo e.stacktrace
50+
}

tests/libs/json_encode.b

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import json
2+
3+
echo json.encode('️Hell"s not\'s here')
4+
echo json.encode(1)
5+
echo json.encode(true)
6+
echo json.encode(nil)
7+
echo json.encode(-2.555551688)
8+
echo json.encode({})
9+
echo json.encode({}, false)
10+
echo json.encode([])
11+
echo json.encode([], false)
12+
echo json.encode({name: 'Richard'})
13+
echo json.encode({name: 'Richard'}, false)
14+
echo json.encode([1, 2, 3, 4, 5])
15+
echo json.encode([1, 2, 3, 4, 5], false)
16+
echo json.encode(['apple', 'mango', 'oranges'])
17+
echo json.encode(['apple', 'mango', 'oranges'], false)
18+
echo json.encode([{name: 'Richard'}])
19+
echo json.encode([{name: 'Richard'}], false) # non compact
20+
21+
echo json.encode([{"precision": "zip",
22+
"Latitude": 37.7668,
23+
"Longitude": -122.3959, "Address": "",
24+
"City": "SAN FRANCISCO",
25+
"State": "CA", "Zip": "94107",
26+
"Country": "US"
27+
}, {
28+
"precision": "zip",
29+
"Latitude": 37.371991, "Longitude": -122.026020,
30+
"Address": "",
31+
"City": "SUNNYVALE",
32+
"State": "CA", "Zip": "94085",
33+
"Country": "US"
34+
}], false)

0 commit comments

Comments
 (0)