Skip to content

Commit f35297a

Browse files
committed
test: Add more detect parsers tests
1 parent c45c6af commit f35297a

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

test/parsers/csv-a.c

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %vast-front -vast-emit-mlir=hl %s -o - | %file-check %s -check-prefix=HL
2+
// RUN: %vast-front -vast-show-locs -vast-loc-attrs -vast-emit-mlir=hl %s -o - | %vast-opt -vast-hl-to-lazy-regions -o %t.mlir
3+
// RUN: %vast-detect-parsers -vast-hl-to-parser -vast-parser-reconcile-casts -reconcile-unrealized-casts %t.mlir -o - | %file-check %s -check-prefix=PARSER
24

35
#include <stdio.h>
46
#include <stdlib.h>
@@ -11,6 +13,7 @@ int parse_csv_line(char *line, char *fields[]);
1113

1214
// Non-parsing part: file handling and utility functions
1315
// HL: hl.func @read_csv_file
16+
// PARSER: hl.func @read_csv_file
1417
void read_csv_file(const char *filename) {
1518
FILE *file = fopen(filename, "r");
1619
if (!file) {

test/parsers/csv-b.c

+3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// RUN: %vast-front -vast-emit-mlir=hl %s -o - | %file-check %s -check-prefix=HL
2+
// RUN: %vast-front -vast-show-locs -vast-loc-attrs -vast-emit-mlir=hl %s -o - | %vast-opt -vast-hl-to-lazy-regions -o %t.mlir
3+
// RUN: %vast-detect-parsers -vast-hl-to-parser -vast-parser-reconcile-casts -reconcile-unrealized-casts %t.mlir -o - | %file-check %s -check-prefix=PARSER
24

35
#include <stdio.h>
46
#include <string.h>
57
#include <stdlib.h>
68

79
// Parsing part: A function to split a CSV line into tokens
810
// HL: hl.func @parse_csv_line
11+
// PARSER: hl.func @parse_csv_line
912
char **parse_csv_line(char *line, int *count) {
1013
int capacity = 10; // Initial capacity for fields
1114
char **fields = malloc(capacity * sizeof(char *));

test/parsers/expr-b.c

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %vast-front -vast-emit-mlir=hl %s -o - | %file-check %s -check-prefix=HL
2+
// RUN: %vast-front -vast-show-locs -vast-loc-attrs -vast-emit-mlir=hl %s -o - | %vast-opt -vast-hl-to-lazy-regions -o %t.mlir
3+
// RUN: %vast-detect-parsers -vast-hl-to-parser -vast-parser-reconcile-casts -reconcile-unrealized-casts %t.mlir -o - | %file-check %s -check-prefix=PARSER
24

35
#include <stdio.h>
46
#include <ctype.h>
@@ -13,6 +15,7 @@ typedef struct {
1315
const char *input;
1416

1517
// HL: hl.func @get_next_token
18+
// PARSER: hl.func @get_next_token
1619
Token get_next_token() {
1720
while (isspace(*input)) input++; // Skip spaces
1821

@@ -52,6 +55,7 @@ int parse_term() {
5255
}
5356

5457
// HL: hl.func @parse_factor
58+
// PARSER: hl.func @parse_factor
5559
// Parsing a factor (number)
5660
int parse_factor() {
5761
Token token = get_next_token();
@@ -62,6 +66,7 @@ int parse_factor() {
6266
}
6367

6468
// HL: hl.func @parse_expression
69+
// PARSER: hl.func @parse_expression
6570
// Parsing an expression (term possibly with '+' or '-')
6671
int parse_expression() {
6772
int result = parse_term();

test/parsers/img-a.c

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %vast-front -vast-emit-mlir=hl %s -o - | %file-check %s -check-prefix=HL
2+
// RUN: %vast-front -vast-show-locs -vast-loc-attrs -vast-emit-mlir=hl %s -o - | %vast-opt -vast-hl-to-lazy-regions -o %t.mlir
3+
// RUN: %vast-detect-parsers -vast-hl-to-parser -vast-parser-reconcile-casts -reconcile-unrealized-casts %t.mlir -o - | %file-check %s -check-prefix=PARSER
24

35
#include <stdio.h>
46
#include <string.h>
@@ -7,6 +9,7 @@
79

810
// Function to display the image as ASCII (for grayscale images)
911
// HL: hl.func @display_ascii_image
12+
// PARSER: hl.func @display_ascii_image
1013
void display_ascii_image(uint8_t *pixel_data, uint16_t width, uint16_t height) {
1114
printf("Displaying image as ASCII:\n");
1215
for (uint16_t y = 0; y < height; ++y) {
@@ -22,6 +25,7 @@ void display_ascii_image(uint8_t *pixel_data, uint16_t width, uint16_t height) {
2225

2326
// Function to parse a binary file containing a SIMPL image
2427
// HL: hl.func @parse_simpl_image
28+
// PARSER: hl.func @parse_simpl_image
2529
void parse_simpl_image(const char *filename) {
2630
FILE *file = fopen(filename, "rb");
2731
if (!file) {

test/parsers/json-a.c

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %vast-front -vast-emit-mlir=hl %s -o - | %file-check %s -check-prefix=HL
2+
// RUN: %vast-front -vast-show-locs -vast-loc-attrs -vast-emit-mlir=hl %s -o - | %vast-opt -vast-hl-to-lazy-regions -o %t.mlir
3+
// RUN: %vast-detect-parsers -vast-hl-to-parser -vast-parser-reconcile-casts -reconcile-unrealized-casts %t.mlir -o - | %file-check %s -check-prefix=PARSER
24

35
#include <stdio.h>
46
#include <string.h>
@@ -39,6 +41,7 @@ char *trim_whitespace(char *str) {
3941

4042
// Parsing part: Parse a key-value pair from a line like '"key": "value"'
4143
// HL: hl.func @parse_key_value_pair
44+
// PARSER: hl.func @parse_key_value_pair
4245
KeyValuePair parse_key_value_pair(char *line) {
4346
KeyValuePair kvp = {NULL, NULL};
4447

@@ -68,6 +71,7 @@ KeyValuePair parse_key_value_pair(char *line) {
6871

6972
// Parsing part: Parse a JSON-like object from multiple lines
7073
// HL: hl.func @parse_json_object
74+
// PARSER: hl.func @parse_json_object
7175
JsonObject parse_json_object(FILE *file) {
7276
JsonObject obj;
7377
obj.pairs = NULL;
@@ -113,6 +117,7 @@ void handle_json_object(JsonObject obj) {
113117

114118
// Non-parsing part: Free memory allocated for a JsonObject
115119
// HL: hl.func @free_json_object
120+
// PARSER: hl.func @free_json_object
116121
void free_json_object(JsonObject obj) {
117122
for (size_t i = 0; i < obj.pair_count; ++i) {
118123
free(obj.pairs[i].key);

test/parsers/key-value.c

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %vast-front -vast-emit-mlir=hl %s -o - | %file-check %s -check-prefix=HL
2+
// RUN: %vast-front -vast-show-locs -vast-loc-attrs -vast-emit-mlir=hl %s -o - | %vast-opt -vast-hl-to-lazy-regions -o %t.mlir
3+
// RUN: %vast-detect-parsers -vast-hl-to-parser -vast-parser-reconcile-casts -reconcile-unrealized-casts %t.mlir -o - | %file-check %s -check-prefix=PARSER
24

35
#include <stdio.h>
46
#include <string.h>
@@ -33,6 +35,7 @@ char *trim_whitespace(char *str) {
3335

3436
// Parsing part: A function to parse a key-value pair
3537
// HL: hl.func @parse_key_value
38+
// PARSER: hl.func @parse_key_value
3639
KeyValuePair parse_key_value(char *line) {
3740
KeyValuePair kvp;
3841
char *delimiter = strchr(line, '=');
@@ -53,6 +56,7 @@ KeyValuePair parse_key_value(char *line) {
5356

5457
// Non-parsing part: Handling the parsed data
5558
// HL: hl.func @handle_key_value
59+
// PARSER: hl.func @handle_key_value
5660
void handle_key_value(KeyValuePair kvp) {
5761
if (kvp.key && kvp.value) {
5862
printf("Key: %s, Value: %s\n", kvp.key, kvp.value);

test/parsers/png-a.c

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// RUN: %vast-front -vast-emit-mlir=hl %s -o - | %file-check %s -check-prefix=HL
2+
// RUN: %vast-front -vast-show-locs -vast-loc-attrs -vast-emit-mlir=hl %s -o - | %vast-opt -vast-hl-to-lazy-regions -o %t.mlir
3+
// RUN: %vast-detect-parsers -vast-hl-to-parser -vast-parser-reconcile-casts -reconcile-unrealized-casts %t.mlir -o - | %file-check %s -check-prefix=PARSER
24

35
#include <stdio.h>
46
#include <stdlib.h>
@@ -7,12 +9,14 @@
79

810
// Function to read a 4-byte big-endian integer from a buffer
911
// HL: hl.func @read_uint32_be
12+
// PARSER: hl.func @read_uint32_be
1013
uint32_t read_uint32_be(uint8_t *buffer) {
1114
return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
1215
}
1316

1417
// Function to parse the IHDR chunk of a PNG file
1518
// HL: hl.func @parse_png_ihdr_chunk
19+
// PARSER: hl.func @parse_png_ihdr_chunk
1620
void parse_png_ihdr_chunk(FILE *file) {
1721
uint8_t length_bytes[4];
1822
fread(length_bytes, sizeof(uint8_t), 4, file);
@@ -55,6 +59,7 @@ void parse_png_ihdr_chunk(FILE *file) {
5559

5660
// Function to parse a basic PNG file
5761
// HL: hl.func @parse_png
62+
// PARSE: hl.func @parse_png
5863
void parse_png(const char *filename) {
5964
FILE *file = fopen(filename, "rb");
6065
if (!file) {

test/parsers/record-a.c

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// RUN: %vast-front -vast-emit-mlir=hl %s -o - | %file-check %s -check-prefix=HL
2+
// RUN: %vast-front -vast-show-locs -vast-loc-attrs -vast-emit-mlir=hl %s -o - | %vast-opt -vast-hl-to-lazy-regions -o %t.mlir
3+
// RUN: %vast-detect-parsers -vast-hl-to-parser -vast-parser-reconcile-casts -reconcile-unrealized-casts %t.mlir -o - | %file-check %s -check-prefix=PARSER
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <stdint.h>
8+
9+
// Function to calculate a simple checksum (XOR all bytes)
10+
// HL: hl.func @calculate_checksum
11+
// PARSER: hl.func @calculate_checksum
12+
uint32_t calculate_checksum(uint8_t *data, size_t length) {
13+
uint32_t checksum = 0;
14+
for (size_t i = 0; i < length; ++i) {
15+
checksum ^= data[i];
16+
}
17+
return checksum;
18+
}
19+
20+
// Function to parse a binary file containing multiple records
21+
// HL: hl.func @parse_binary_records
22+
// PARSER: hl.func @parse_binary_records
23+
void parse_binary_records(const char *filename) {
24+
FILE *file = fopen(filename, "rb");
25+
if (!file) {
26+
perror("Error opening file");
27+
return;
28+
}
29+
30+
// Go to the end of the file to find the file length
31+
fseek(file, 0, SEEK_END);
32+
size_t file_length = ftell(file);
33+
rewind(file);
34+
35+
// Read the file contents into a buffer
36+
uint8_t *buffer = (uint8_t *)malloc(file_length);
37+
fread(buffer, sizeof(uint8_t), file_length, file);
38+
39+
// Close the file after reading
40+
fclose(file);
41+
42+
size_t idx = 0;
43+
printf("Parsing records:\n");
44+
45+
// Parsing Part: Read records until the checksum
46+
while (idx < file_length - 4) { // Last 4 bytes are the checksum
47+
// Read record type (2 bytes)
48+
uint16_t record_type = (buffer[idx] << 8) | buffer[idx + 1];
49+
idx += 2;
50+
51+
// Read record length (2 bytes)
52+
uint16_t record_length = (buffer[idx] << 8) | buffer[idx + 1];
53+
idx += 2;
54+
55+
// Read record data based on length
56+
uint8_t *record_data = &buffer[idx];
57+
idx += record_length;
58+
59+
// Print record information
60+
printf("Record Type: %u, Length: %u, Data: ", record_type, record_length);
61+
for (uint16_t i = 0; i < record_length; ++i) {
62+
printf("%02X ", record_data[i]);
63+
}
64+
printf("\n");
65+
}
66+
67+
// Parsing Part: Read and verify the checksum (last 4 bytes)
68+
uint32_t checksum = (buffer[idx] << 24) | (buffer[idx + 1] << 16) | (buffer[idx + 2] << 8) | buffer[idx + 3];
69+
printf("Read checksum: %08X\n", checksum);
70+
71+
// Non-Parsing Part: Calculate checksum of the data
72+
uint32_t calculated_checksum = calculate_checksum(buffer, file_length - 4);
73+
printf("Calculated checksum: %08X\n", calculated_checksum);
74+
75+
// Validate checksum
76+
if (checksum == calculated_checksum) {
77+
printf("Checksum verification passed.\n");
78+
} else {
79+
printf("Checksum verification failed.\n");
80+
}
81+
82+
// Free memory
83+
free(buffer);
84+
}
85+
86+
int main() {
87+
const char *filename = "records_data.bin";
88+
parse_binary_records(filename);
89+
return 0;
90+
}

0 commit comments

Comments
 (0)