xref: /6.6.0/sigar/tests/t_sigar_cpu.c (revision 829a4c03)
1/**
2 * Copyright (c) 2009, Sun Microsystems Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice,
9 *   this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 *   this list of conditions and the following disclaimer in the documentation
12 *   and/or other materials provided with the distribution.
13 * - Neither the name of Sun Microsystems Inc. nor the names of its contributors
14 *   may be used to endorse or promote products derived from this software
15 *   without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29#include <sys/types.h>
30#ifdef HAVE_SYS_TIME_H
31#include <sys/time.h>
32#endif
33#ifdef HAVE_SYS_RESOURCE_H
34#include <sys/resource.h>
35#endif
36
37#include <assert.h>
38#include <stdlib.h>
39#include <stdio.h>
40#include <string.h>
41#include <errno.h>
42
43#include "sigar.h"
44#include "sigar_private.h"
45#include "sigar_format.h"
46#include "sigar_tests.h"
47
48TEST(test_sigar_cpu_get) {
49        sigar_cpu_t cpu;
50        int ret;
51
52        if (SIGAR_OK == (ret = sigar_cpu_get(t, &cpu))) {
53                assert(IS_IMPL_U64(cpu.user));
54                assert(IS_IMPL_U64(cpu.sys));
55#if !(defined(SIGAR_TEST_OS_AIX))
56                assert(IS_IMPL_U64(cpu.nice));
57#endif
58                assert(IS_IMPL_U64(cpu.idle));
59                assert(IS_IMPL_U64(cpu.wait));
60                assert(IS_IMPL_U64(cpu.total));
61        } else {
62                switch (ret) {
63                        /* track the expected error code */
64                default:
65                        fprintf(stderr, "ret = %d (%s)\n", ret, sigar_strerror(t, ret));
66                        assert(ret == SIGAR_OK);
67                        break;
68                }
69        }
70
71        return 0;
72}
73
74
75
76int main() {
77        sigar_t *t;
78        int err = 0;
79
80        assert(SIGAR_OK == sigar_open(&t));
81
82        test_sigar_cpu_get(t);
83
84        sigar_close(t);
85
86        return err ? -1 : 0;
87}
88