Skip to content

Commit

Permalink
test: add new cases
Browse files Browse the repository at this point in the history
  • Loading branch information
luochen1990 committed Jul 21, 2024
1 parent bee764c commit a117747
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
11 changes: 11 additions & 0 deletions test/examples/binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ai_powered import ai_powered

@ai_powered
def binary_search(arr: list[int], x: int) -> int:
''' Returns the index of x in arr if present, else -1 '''
...

def test_binary_search():

assert binary_search([1, 2, 100, 103, 104, 105], 103) == 3
assert binary_search([1, 2, 100, 103, 104, 105], 2) == 1
19 changes: 19 additions & 0 deletions test/examples/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from ai_powered import ai_powered

@ai_powered
def python_expression_generator(expr: str) -> str:
''' 将用户输入的数学表达式转换成合法的python表达式, 注意不要使用任何未定义的函数,如果用户表达式中有类似函数调用的表达,请转换为python内置函数或语法 '''
...

def calculator(expr: str) -> int:
''' calculate the result of the math expression '''
py_expr = python_expression_generator(expr)
print(f"{py_expr =}")
x = eval(py_expr)
return int(x)

def test_calculator():
assert calculator("1+1") == 2
assert calculator("1+2*3") == 7
assert calculator("2^10+3*4") == 1036
assert calculator("2^10+sum(1,10)") == 1079
34 changes: 31 additions & 3 deletions test/examples/extract_user_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,45 @@
from typing import Optional
from ai_powered import ai_powered

@ai_powered
def is_same_country(a: str, b: str) -> bool:
''' judge if a and b is probably alias of the same country '''
...

@dataclass
class UserInfo:
name: str
country: Optional[str]
age: Optional[str]
age: Optional[int]

def consistent_with(self, other: 'UserInfo') -> bool:
name_check = self.name.lower() == other.name.lower()
country_check = is_same_country(self.country, other.country) if (self.country is not None and other.country is not None) else self.country == other.country
age_check = self.age == other.age
return name_check and country_check and age_check

@ai_powered
def extract_user_info(raw_text: str) -> UserInfo:
'''
Obtain complete information as much as possible, unless it is truly not mentioned;
when identifying countries, match them to the closest real existing country
'''
...

def user_info_match(a: UserInfo, b: UserInfo) -> bool:
''' judge if the two user info is consistent '''
...

def test_extract_user_info():
def test_extract_user_info_1():
self_introduction = "Hello, my name is Alice, and I am from USA. I am 10 years old"
user_info = extract_user_info(self_introduction)
assert user_info == UserInfo(name="Alice", country="USA", age="10")
print(f"{user_info =}")

assert user_info.consistent_with(UserInfo("Alice", "USA", 10))

def test_extract_user_info_2():
self_introduction = "Hello, Iama ten years old amecan boy namd bob"
user_info = extract_user_info(self_introduction)
print(f"{user_info =}")

assert user_info.consistent_with(UserInfo("Bob", "USA", 10))

0 comments on commit a117747

Please sign in to comment.