xref: /PHP-8.1/ext/shmop/tests/001.phpt (revision 5f090e06)
1--TEST--
2shmop extension test
3--EXTENSIONS--
4shmop
5--FILE--
6<?php
7    $hex_shm_id = 0xff3;
8    $write_d1 = "test #1 of the shmop() extension";
9    $write_d2 = "test #2 append data to shared memory segment";
10
11    echo "shm open for create: ";
12    $shm_id = shmop_open($hex_shm_id, "n", 0644, 1024);
13    if (!$shm_id) {
14        die("failed\n");
15    } else {
16        echo "ok\n";
17    }
18
19    echo "shm size is: " . ($shm_size = shmop_size($shm_id)) . "\n";
20
21    echo "shm write test #1: ";
22    $written = shmop_write($shm_id, $write_d1, 0);
23    if ($written != strlen($write_d1)) {
24        echo "failed\n";
25    } else {
26        echo "ok\n";
27    }
28
29    echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";
30
31    echo "shm open for read only: ";
32    $shm_id = shmop_open($hex_shm_id, "a", 0644, 1024);
33    if (!$shm_id) {
34        echo "failed\n";
35    } else {
36        echo "ok\n";
37    }
38
39    echo "data in memory is: " . shmop_read($shm_id, 0, $written) . "\n";
40
41    /* try to append data to the shared memory segment, this should fail */
42    try {
43        shmop_write($shm_id, $write_d1, $written);
44    } catch (Error $exception) {
45        echo $exception->getMessage() . "\n";
46    }
47
48    echo "shm open for read only: ";
49    $shm_id = shmop_open($hex_shm_id, "w", 0644, 1024);
50    if (!$shm_id) {
51        echo "failed\n";
52    } else {
53        echo "ok\n";
54    }
55
56    echo "shm write test #2: ";
57    $written = shmop_write($shm_id, $write_d2, $written);
58    if ($written != strlen($write_d2)) {
59        die("failed\n");
60    } else {
61        echo "ok\n";
62    }
63
64    echo "data in memory is: " . shmop_read($shm_id, 0, strlen($write_d1 . $write_d2)) . "\n";
65
66    echo "deletion of shm segment: ";
67    if (!shmop_delete($shm_id)) {
68        echo "failed\n";
69    } else {
70        echo "ok\n";
71    }
72?>
73--EXPECTF--
74shm open for create: ok
75shm size is: %d
76shm write test #1: ok
77data in memory is: test #1 of the shmop() extension
78shm open for read only: ok
79data in memory is: test #1 of the shmop() extension
80Read-only segment cannot be written
81shm open for read only: ok
82shm write test #2: ok
83data in memory is: test #1 of the shmop() extensiontest #2 append data to shared memory segment
84deletion of shm segment: ok
85