github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/native/test/main.c (about)

     1  /**
     2   * Copyright 2023 CloudWeGo Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // #include <stdio.h>
    18  #include "../native.c"
    19  #include "xprintf.h"
    20  #include "example.h"
    21  
    22  J2TStateMachine j2t = {};
    23  GoSlice buf = (GoSlice){
    24      .buf = (char[1024]){},
    25      .len = 0,
    26      .cap = 1024,
    27  };
    28  char DBUF[800] = {};
    29  
    30  int main()
    31  {
    32      // FILE *fp = NULL;
    33      // fp = fopen("../testdata/twitter.json", "r");
    34      // char data[1000000];
    35      // size_t rn = fread(data, 1, 1000000, fp);
    36      // if (rn < 0)
    37      // {
    38      //     printf("read err: %zu\n", rn);
    39      //     return 0;
    40      // }
    41  
    42      const char *data = EXAMPLE_JSON;
    43      size_t rn = sizeof(EXAMPLE_JSON);
    44  
    45      StateMachine sm = {};
    46      GoString s = {};
    47      s = (GoString){
    48          .buf = data,
    49          .len = rn,
    50      };
    51  
    52      long p = 0;
    53      long q = skip_one(&s, &p, &sm);
    54  
    55      xprintf("q: %d, p: %d\n", q, p);
    56  
    57      INIT_BASE_DESC();
    58      INIT_EXAMPLE_DESC();
    59  
    60      j2t.reqs_cache = (GoSlice){
    61          .buf = (char[72]){},
    62          .len = 0,
    63          .cap = 72,
    64      };
    65      j2t.key_cache = (GoSlice){
    66          .buf = (char[EXMAPLE_MAX_KEY_LEN]){},
    67          .len = 0,
    68          .cap = EXMAPLE_MAX_KEY_LEN,
    69      };
    70      j2t.sp = 1;
    71      j2t.vt[0] = (J2TState){
    72          .st = J2T_VAL,
    73          .jp = 0,
    74          .td = &EXAMPLE_DESC,
    75      };
    76      j2t.jt = (JsonState){
    77          .dbuf = DBUF,
    78          .dcap = 800,
    79      };
    80  
    81      xprintf("sizof JsonState:%d, StateMachine:%d, J2TStateMachine:%d, J2TExtra:%d\n", sizeof(JsonState), sizeof(StateMachine), sizeof(J2TStateMachine), sizeof(J2TExtra));
    82      xprintf("offsetof: j2t.jt:%d, j2t.reqs_cache:%d, j2t.vt:%d, vt.jp:%d, vt.td:%d, vt.ex:%d\n", __offsetof(J2TStateMachine, jt), __offsetof(J2TStateMachine, reqs_cache), __offsetof(J2TStateMachine, vt), __offsetof(J2TState, jp), __offsetof(J2TState, td), __offsetof(J2TState, ex));
    83  
    84      // for (int i = 0; i < 800; i++)
    85      // {
    86      //     xprintf("%d", j2t.jt.dbuf[i]);
    87      // }
    88      xprintf("j2t_fsm_exec: %d\nbuf:%l\n", j2t_fsm_exec(&j2t, &buf, &s, 0), &buf);
    89      return 0;
    90  }