Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow out of source builds
[simgrid.git] / examples / msg / properties / msg_prop.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
8 #include "xbt/sysdep.h"         /* calloc, printf */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Property test");
18
19 int alice(int argc, char *argv[]);
20 int bob(int argc, char *argv[]);
21 int forwarder(int argc, char *argv[]);
22 MSG_error_t test_all(const char *platform_file,
23                      const char *application_file);
24
25 int alice(int argc, char *argv[])
26 {
27   m_host_t host1 = MSG_get_host_by_name("host1");
28   xbt_dict_t props = MSG_host_get_properties(host1);
29   xbt_dict_cursor_t cursor = NULL;
30   char *key, *data;
31   const char *noexist = "Unknown";
32   const char *value;
33   char exist[] = "SG_TEST_Hdd";
34
35   INFO0("== Print the properties of the host");
36   xbt_dict_foreach(props, cursor, key, data)
37       INFO2("  Host property: '%s' -> '%s'", key, data);
38
39   INFO0("== Try to get a host property that does not exist");
40   value = MSG_host_get_property_value(host1, noexist);
41   xbt_assert0(!value, "The key exists (it's not supposed to)");
42
43   INFO0("== Try to get a host property that does exist");
44   value = MSG_host_get_property_value(host1, exist);
45   xbt_assert1(value, "\tProperty %s is undefined (where it should)",
46               exist);
47   xbt_assert2(!strcmp(value, "180"),
48               "\tValue of property %s is defined to %s (where it should be 180)",
49               exist, value);
50   INFO2("   Property: %s old value: %s", exist, value);
51
52   INFO0("== Trying to modify a host property");
53   xbt_dict_set(props, exist, xbt_strdup("250"), xbt_free_f);
54
55   /* Test if we have changed the value */
56   value = MSG_host_get_property_value(host1, exist);
57   xbt_assert1(value, "Property %s is undefined (where it should)", exist);
58   xbt_assert2(!strcmp(value, "250"),
59               "Value of property %s is defined to %s (where it should be 250)",
60               exist, value);
61   INFO2("   Property: %s old value: %s", exist, value);
62
63   return 0;
64 }
65
66 int bob(int argc, char *argv[])
67 {
68   /* Get the property list of current bob process */
69   xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
70   xbt_dict_cursor_t cursor = NULL;
71   char *key, *data;
72   const char *noexist = "UnknownProcessProp";
73   const char *value;
74
75   INFO0("== Print the properties of the process");
76   xbt_dict_foreach(props, cursor, key, data)
77       INFO2("   Process property: %s -> %s", key, data);
78
79   INFO0("== Try to get a process property that does not exist");
80
81   value = MSG_process_get_property_value(MSG_process_self(), noexist);
82   xbt_assert0(!value, "The property is defined (it shouldnt)");
83
84   return 0;
85 }
86
87 /** Test function */
88 MSG_error_t test_all(const char *platform_file,
89                      const char *application_file)
90 {
91   MSG_function_register("alice", alice);
92   MSG_function_register("bob", bob);
93
94   MSG_create_environment(platform_file);
95   MSG_launch_application(application_file);
96
97   return MSG_main();
98 }                               /* end_of_test_all */
99
100
101 /** Main function */
102 int main(int argc, char *argv[])
103 {
104   MSG_error_t res = MSG_OK;
105
106   MSG_global_init(&argc, argv);
107   if (argc < 3) {
108     printf("Usage: %s platform_file deployment_file\n", argv[0]);
109     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
110     exit(1);
111   }
112   res = test_all(argv[1], argv[2]);
113   MSG_clean();
114
115   if (res == MSG_OK)
116     return 0;
117   else
118     return 1;
119 }                               /* end_of_main */