- 测试基本的 ordered_map 操作,包括整数键浮点数值的存储和更新 - 测试字符串键的 ordered_map 功能及类型处理 - 测试复杂值类型的 ordered_map,包括字符串、数组、对象和变体类型 - 测试精确类值类型的容器功能,验证类型安全和继承关系 - 测试 unsafe_cast 功能及其类型转换行为 - 测试 unsafe_cast 类型不匹配的错误处理 - 测试 ordered_map 与 PHP 数组之间的赋值操作 - 测试相同类型的 ordered_map 之间复制操作 - 测试 unset 操作对 ordered_map 元素的删除功能 - 测试遍历过程中 unset 元素的行为表现pull/3/head
parent
b3e489d334
commit
715d2222e8
10 changed files with 311 additions and 0 deletions
@ -0,0 +1,19 @@ |
||||
--TEST-- |
||||
std ordered_map: 001 |
||||
--FILE-- |
||||
<?php |
||||
function main() { |
||||
$map = std::ordered_map(native_types::type_int, native_types::type_float); |
||||
$map[10] = 1.25; |
||||
$map[10] += 0.75; |
||||
$map[11] = 3.5; |
||||
|
||||
var_dump($map[10] == 2.0); |
||||
var_dump($map[11] == 3.5); |
||||
var_dump(count($map)); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
bool(true) |
||||
bool(true) |
||||
int(2) |
||||
@ -0,0 +1,25 @@ |
||||
--TEST-- |
||||
std ordered_map: string key |
||||
--FILE-- |
||||
<?php |
||||
function main() { |
||||
$map = std::ordered_map(complex_types::type_string, native_types::type_int); |
||||
$map["alpha"] = 10; |
||||
$map["beta"] = 20; |
||||
$map["beta"] += 22; |
||||
|
||||
$key = "alpha"; |
||||
var_dump($map[$key]); |
||||
var_dump($map["beta"]); |
||||
var_dump(count($map)); |
||||
|
||||
$map2 = std::ordered_map(complex_types::type_str, native_types::type_float); |
||||
$map2["pi"] = 3.14; |
||||
var_dump($map2["pi"] == 3.14); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(10) |
||||
int(42) |
||||
int(2) |
||||
bool(true) |
||||
@ -0,0 +1,48 @@ |
||||
--TEST-- |
||||
std ordered_map: complex value types |
||||
--FILE-- |
||||
<?php |
||||
class StdMapComplexValue |
||||
{ |
||||
public function __construct(public int $value) |
||||
{ |
||||
} |
||||
|
||||
public function getValue(): int |
||||
{ |
||||
return $this->value; |
||||
} |
||||
} |
||||
|
||||
function main() { |
||||
$strings = std::ordered_map(native_types::type_int, complex_types::type_str); |
||||
$strings[1] = 456; |
||||
var_dump($strings[1]); |
||||
|
||||
$arrays = std::ordered_map(native_types::type_int, complex_types::type_array); |
||||
$arrays[2] = ["name" => "map", "value" => 84]; |
||||
$array = $arrays[2]; |
||||
var_dump($array["name"]); |
||||
var_dump($array["value"]); |
||||
|
||||
$objects = std::ordered_map(complex_types::type_string, complex_types::type_object); |
||||
$objects["item"] = new StdMapComplexValue(14); |
||||
var_dump($objects["item"] instanceof StdMapComplexValue); |
||||
$object = $objects["item"]->toObject(StdMapComplexValue::class); |
||||
var_dump($object->getValue()); |
||||
|
||||
$variants = std::ordered_map(native_types::type_int, complex_types::type_any); |
||||
$variants[3] = 12.5; |
||||
$variants[4] = "any"; |
||||
var_dump($variants[3]); |
||||
var_dump($variants[4]); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
string(3) "456" |
||||
string(3) "map" |
||||
int(84) |
||||
bool(true) |
||||
int(14) |
||||
float(12.5) |
||||
string(3) "any" |
||||
@ -0,0 +1,67 @@ |
||||
--TEST-- |
||||
std containers: exact class value type |
||||
--FILE-- |
||||
<?php |
||||
class StdContainerClassValue |
||||
{ |
||||
public function __construct(public int $value) |
||||
{ |
||||
} |
||||
|
||||
public function getValue(): int |
||||
{ |
||||
return $this->value; |
||||
} |
||||
} |
||||
|
||||
class StdContainerClassValueChild extends StdContainerClassValue |
||||
{ |
||||
} |
||||
|
||||
class StdContainerClassValueOther |
||||
{ |
||||
} |
||||
|
||||
function std_container_class_value_mixed(mixed $value): mixed |
||||
{ |
||||
return $value; |
||||
} |
||||
|
||||
function main() { |
||||
$map = std::ordered_map(complex_types::type_str, StdContainerClassValue::class); |
||||
$map["a"] = new StdContainerClassValue(1); |
||||
$item = $map["a"]; |
||||
var_dump($item->getValue()); |
||||
|
||||
$vector = std::vector(StdContainerClassValue::class); |
||||
$vector[] = new StdContainerClassValue(2); |
||||
var_dump($vector[0]->getValue()); |
||||
|
||||
$array = std::array(StdContainerClassValue::class, 2); |
||||
$array[0] = new StdContainerClassValue(3); |
||||
var_dump($array[0]->getValue()); |
||||
|
||||
$ordered = std::map(native_types::type_int, StdContainerClassValue::class); |
||||
$unordered[1] = std_container_class_value_mixed(new StdContainerClassValue(4)); |
||||
var_dump($unordered[1]->getValue()); |
||||
|
||||
try { |
||||
$unordered[2] = std_container_class_value_mixed(new StdContainerClassValueChild(5)); |
||||
} catch (Throwable $e) { |
||||
echo $e->getMessage(), "\n"; |
||||
} |
||||
|
||||
try { |
||||
$unordered[3] = std_container_class_value_mixed(new StdContainerClassValueOther()); |
||||
} catch (Throwable $e) { |
||||
echo $e->getMessage(), "\n"; |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(1) |
||||
int(2) |
||||
int(3) |
||||
int(4) |
||||
The parameter `object` must be instance of class `StdContainerClassValue`, object of `StdContainerClassValueChild` given |
||||
The parameter `object` must be instance of class `StdContainerClassValue`, object of `StdContainerClassValueOther` given |
||||
@ -0,0 +1,30 @@ |
||||
--TEST-- |
||||
std ordered_map: unsafe_cast |
||||
--FILE-- |
||||
<?php |
||||
function std_map_unsafe_ptr_update($source): void |
||||
{ |
||||
$map = $source->toStdMap(complex_types::type_str, native_types::type_int); |
||||
var_dump($map["b"]); |
||||
$map["c"] = 9; |
||||
|
||||
unset($source); |
||||
|
||||
$array = (array)$map; |
||||
var_dump(count($array)); |
||||
} |
||||
|
||||
function main() { |
||||
$map = std::ordered_map(complex_types::type_str, native_types::type_int); |
||||
$map["a"] = 1; |
||||
$map["b"] = 7; |
||||
$map["c"] = 3; |
||||
|
||||
std_map_unsafe_ptr_update($map); |
||||
var_dump($map["c"]); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(7) |
||||
int(3) |
||||
int(9) |
||||
@ -0,0 +1,20 @@ |
||||
--TEST-- |
||||
std ordered_map: unsafe_cast type mismatch |
||||
--FILE-- |
||||
<?php |
||||
function std_map_unsafe_ptr_type_mismatch($source): void |
||||
{ |
||||
$map = $source->toStdMap(complex_types::type_str, native_types::type_float); |
||||
} |
||||
|
||||
function main() { |
||||
$map = std::ordered_map(complex_types::type_str, native_types::type_int); |
||||
try { |
||||
std_map_unsafe_ptr_type_mismatch($map); |
||||
} catch (TypeError $e) { |
||||
echo $e->getMessage(), "\n"; |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
std container type mismatch |
||||
@ -0,0 +1,21 @@ |
||||
--TEST-- |
||||
std ordered_map: assign to PHP array |
||||
--FILE-- |
||||
<?php |
||||
function main() { |
||||
$map = std::ordered_map(native_types::type_int, native_types::type_int); |
||||
$map[10] = 100; |
||||
$map[20] = 200; |
||||
|
||||
$copy = $map; |
||||
var_dump(is_array($copy)); |
||||
var_dump(count($copy)); |
||||
var_dump($copy[10]); |
||||
var_dump($copy[20]); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
bool(true) |
||||
int(2) |
||||
int(100) |
||||
int(200) |
||||
@ -0,0 +1,25 @@ |
||||
--TEST-- |
||||
std ordered_map: same type copy |
||||
--FILE-- |
||||
<?php |
||||
function main() { |
||||
$a = std::ordered_map(native_types::type_int, native_types::type_int); |
||||
$b = std::ordered_map(native_types::type_int, native_types::type_int); |
||||
|
||||
$b[10] = 100; |
||||
$b[20] = 200; |
||||
$a = $b; |
||||
|
||||
var_dump(count($a)); |
||||
var_dump($a[10]); |
||||
var_dump($a[20]); |
||||
|
||||
$a[10] = 999; |
||||
var_dump($b[10]); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
int(2) |
||||
int(100) |
||||
int(200) |
||||
int(100) |
||||
@ -0,0 +1,24 @@ |
||||
--TEST-- |
||||
std ordered_map: unset |
||||
--FILE-- |
||||
<?php |
||||
function main() { |
||||
$map = std::ordered_map(complex_types::type_string, native_types::type_int); |
||||
$map["alpha"] = 10; |
||||
$map["beta"] = 20; |
||||
var_dump($map); |
||||
unset($map["alpha"]); |
||||
var_dump($map); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
array(2) { |
||||
["alpha"]=> |
||||
int(10) |
||||
["beta"]=> |
||||
int(20) |
||||
} |
||||
array(1) { |
||||
["beta"]=> |
||||
int(20) |
||||
} |
||||
@ -0,0 +1,32 @@ |
||||
--TEST-- |
||||
std ordered_map: unset |
||||
--FILE-- |
||||
<?php |
||||
function main() { |
||||
$map = std::ordered_map(complex_types::type_string, native_types::type_int); |
||||
$map["alpha"] = 10; |
||||
$map["beta"] = 20; |
||||
$map["gamma"] = 30; |
||||
|
||||
foreach ($map as $k => $v) { |
||||
var_dump($k, $v); |
||||
} |
||||
unset($map["beta"]); |
||||
echo "unset-------------\n"; |
||||
foreach ($map as $k => $v) { |
||||
var_dump($k, $v); |
||||
} |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
string(5) "alpha" |
||||
int(10) |
||||
string(4) "beta" |
||||
int(20) |
||||
string(5) "gamma" |
||||
int(30) |
||||
unset------------- |
||||
string(5) "alpha" |
||||
int(10) |
||||
string(5) "gamma" |
||||
int(30) |
||||
Loading…
Reference in new issue