66 * Distributed under the Boost Software License, Version 1.0.
77 */
88
9+ #include "fcontext.h"
10+ #include <assert.h>
911#include <stdio.h>
1012#include <stdlib.h>
11- #include <assert.h>
12- #include "fcontext.h"
1313
1414typedef struct {
1515 int value ;
@@ -18,63 +18,63 @@ typedef struct {
1818/* Function to be executed "on top" of the fiber */
1919fcontext_transfer_t ontop_func (fcontext_transfer_t t ) {
2020 printf (" [ontop] Executing ontop function\n" );
21- test_data_t * data = (test_data_t * )t .data ;
22-
21+ test_data_t * data = (test_data_t * )t .data ;
22+
2323 /* Verify we received the expected data from main */
2424 assert (data -> value == 3 );
25-
25+
2626 /* Modify data to prove we ran */
2727 data -> value += 10 ;
28-
28+
2929 /* Return transfer to resume the fiber */
3030 return t ;
3131}
3232
33- void fiber_func (fcontext_transfer_t t ) {
33+ fcontext_transfer_t fiber_func (fcontext_transfer_t t ) {
3434 printf (" [fiber] Started\n" );
35- test_data_t * data = (test_data_t * )t .data ;
36-
35+ test_data_t * data = (test_data_t * )t .data ;
36+
3737 /* 1. Verify initial value passed from main */
3838 assert (data -> value == 1 );
39-
39+
4040 /* Modify and yield back */
4141 data -> value = 2 ;
4242 printf (" [fiber] Yielding back to main\n" );
43-
43+
4444 /* Use fcontext_swap for standard yield */
4545 t = fcontext_swap (t .prev_context , data );
46-
46+
4747 /* 2. We are back. ontop_fcontext should have executed ontop_func before we resumed. */
4848 printf (" [fiber] Resumed\n" );
49- data = (test_data_t * )t .data ;
50-
49+ data = (test_data_t * )t .data ;
50+
5151 /* Verify ontop_func modification (3 + 10 = 13) */
5252 assert (data -> value == 13 );
53-
53+
5454 printf (" [fiber] Finished\n" );
55- fcontext_swap (t .prev_context , data );
55+ return fcontext_swap (t .prev_context , data );
5656}
5757
5858int main (void ) {
5959 printf ("=== fcontext ontop Test ===\n" );
60-
61- fcontext_stack_t * stack = fcontext_create (16 * 1024 , fiber_func );
62- if (!stack ) {
60+
61+ fcontext_stack_t * stack = fcontext_create (16 * 1024 , fiber_func );
62+ if (!stack ) {
6363 fprintf (stderr , "Failed to create context\n" );
6464 return 1 ;
6565 }
66-
67- test_data_t data = { .value = 1 };
68-
66+
67+ test_data_t data = {.value = 1 };
68+
6969 printf ("[main] Jumping to fiber\n" );
7070 fcontext_transfer_t t = fcontext_swap (stack -> context , & data );
71-
71+
7272 /* Fiber yielded. Value should be 2. */
7373 assert (data .value == 2 );
74-
74+
7575 printf ("[main] Fiber yielded. Calling ontop_fcontext\n" );
7676 data .value = 3 ;
77-
77+
7878 /* Use ontop_fcontext to resume fiber and run ontop_func first */
7979 /* Note: We manually handle ASAN hooks here since ontop_fcontext is raw API */
8080#ifdef __SANITIZE_ADDRESS__
@@ -85,11 +85,11 @@ int main(void) {
8585#ifdef __SANITIZE_ADDRESS__
8686 __sanitizer_finish_switch_fiber (fake_stack_save , NULL , NULL );
8787#endif
88-
88+
8989 printf ("[main] Fiber returned\n" );
90-
90+
9191 fcontext_destroy (stack );
92-
92+
9393 printf ("\n✓ PASS: ontop_fcontext test completed.\n" );
9494 return 0 ;
9595}
0 commit comments