File: main.c

package info (click to toggle)
libwebsockets 4.3.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 31,288 kB
  • sloc: ansic: 194,407; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (83 lines) | stat: -rw-r--r-- 1,775 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * lws-api-test-lwsac
 *
 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 */

#include <libwebsockets.h>

struct mytest {
	int payload;
	/* notice doesn't have to be at start of struct */
	lws_list_ptr list_next;
	/* a struct can appear on multiple lists too... */
};

/* converts a ptr to struct mytest .list_next to a ptr to struct mytest */
#define list_to_mytest(p) lws_list_ptr_container(p, struct mytest, list_next)

int main(int argc, const char **argv)
{
	int n, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE, acc;
	lws_list_ptr list_head = NULL, iter;
	struct lwsac *lwsac = NULL;
	struct mytest *m;
	const char *p;

	if ((p = lws_cmdline_option(argc, argv, "-d")))
		logs = atoi(p);

	lws_set_log_level(logs, NULL);
	lwsl_user("LWS API selftest: lwsac\n");

	/*
	 * 1) allocate and create 1000 struct mytest in a linked-list
	 */

	for (n = 0; n < 1000; n++) {
		m = lwsac_use(&lwsac, sizeof(*m), 0);
		if (!m)
			return -1;
		m->payload = n;

		lws_list_ptr_insert(&list_head, &m->list_next, NULL);
	}

	/*
	 * 2) report some debug info about the lwsac state... those 1000
	 * allocations actually only required 4 mallocs
	 */

	lwsac_info(lwsac);

	/* 3) iterate the list, accumulating the payloads */

	acc = 0;
	iter = list_head;
	while (iter) {
		m = list_to_mytest(iter);
		acc += m->payload;

		lws_list_ptr_advance(iter);
	}

	if (acc != 499500) {
		lwsl_err("%s: FAIL acc %d\n", __func__, acc);

		return 1;
	}

	/*
	 * 4) deallocate everything (lwsac is also set to NULL).  It just
	 *    deallocates the 4 mallocs, everything in there is gone accordingly
	 */

	lwsac_free(&lwsac);

	lwsl_user("Completed: PASS\n");

	return 0;
}