Updating CVS with partially ported benchmark. Sequencer.java's run() function is...
[IRC.git] / Robust / src / Benchmarks / SingleTM / genome / gene.c
1 /* =============================================================================
2  *
3  * gene.c
4  * -- Create random gene
5  *
6  * =============================================================================
7  *
8  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
9  * Author: Chi Cao Minh
10  *
11  * =============================================================================
12  *
13  * For the license of bayes/sort.h and bayes/sort.c, please see the header
14  * of the files.
15  * 
16  * ------------------------------------------------------------------------
17  * 
18  * For the license of kmeans, please see kmeans/LICENSE.kmeans
19  * 
20  * ------------------------------------------------------------------------
21  * 
22  * For the license of ssca2, please see ssca2/COPYRIGHT
23  * 
24  * ------------------------------------------------------------------------
25  * 
26  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
27  * header of the files.
28  * 
29  * ------------------------------------------------------------------------
30  * 
31  * For the license of lib/rbtree.h and lib/rbtree.c, please see
32  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
33  * 
34  * ------------------------------------------------------------------------
35  * 
36  * Unless otherwise noted, the following license applies to STAMP files:
37  * 
38  * Copyright (c) 2007, Stanford University
39  * All rights reserved.
40  * 
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions are
43  * met:
44  * 
45  *     * Redistributions of source code must retain the above copyright
46  *       notice, this list of conditions and the following disclaimer.
47  * 
48  *     * Redistributions in binary form must reproduce the above copyright
49  *       notice, this list of conditions and the following disclaimer in
50  *       the documentation and/or other materials provided with the
51  *       distribution.
52  * 
53  *     * Neither the name of Stanford University nor the names of its
54  *       contributors may be used to endorse or promote products derived
55  *       from this software without specific prior written permission.
56  * 
57  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
67  * THE POSSIBILITY OF SUCH DAMAGE.
68  *
69  * =============================================================================
70  */
71
72
73 #include <assert.h>
74 #include <stdlib.h>
75 #include "gene.h"
76 #include "nucleotide.h"
77 #include "random.h"
78 #include "tm.h"
79
80
81 /* =============================================================================
82  * gene_alloc
83  * -- Does all memory allocation necessary for gene creation
84  * -- Returns NULL on failure
85  * =============================================================================
86  */
87 gene_t*
88 gene_alloc (long length)
89 {
90     gene_t* genePtr;
91
92     assert(length > 1);
93
94     genePtr = (gene_t*)malloc(sizeof(gene_t));
95     if (genePtr == NULL) {
96         return NULL;
97     }
98
99     genePtr->contents = (char*)malloc((length + 1) * sizeof(char));
100     if (genePtr->contents == NULL) {
101         return NULL;
102     }
103     genePtr->contents[length] = '\0';
104     genePtr->length = length;
105
106     genePtr->startBitmapPtr = bitmap_alloc(length);
107     if (genePtr->startBitmapPtr == NULL) {
108         return NULL;
109     }
110
111     return genePtr;
112 }
113
114
115 /* =============================================================================
116  * gene_create
117  * -- Populate contents with random gene
118  * =============================================================================
119  */
120 void
121 gene_create (gene_t* genePtr, random_t* randomPtr)
122 {
123     long length;
124     char* contents;
125     long i;
126     const char nucleotides[] = {
127         NUCLEOTIDE_ADENINE,
128         NUCLEOTIDE_CYTOSINE,
129         NUCLEOTIDE_GUANINE,
130         NUCLEOTIDE_THYMINE,
131     };
132
133     assert(genePtr != NULL);
134     assert(randomPtr != NULL);
135
136     length = genePtr->length;
137     contents = genePtr->contents;
138
139     for (i = 0; i < length; i++) {
140         contents[i] =
141             nucleotides[(random_generate(randomPtr)% NUCLEOTIDE_NUM_TYPE)];
142     }
143 }
144
145
146 /* =============================================================================
147  * gene_free
148  * =============================================================================
149  */
150 void
151 gene_free (gene_t* genePtr)
152 {
153     bitmap_free(genePtr->startBitmapPtr);
154     free(genePtr->contents);
155     free(genePtr);
156 }
157
158
159 /* =============================================================================
160  * TEST_GENE
161  * =============================================================================
162  */
163 #ifdef TEST_GENE
164
165
166 #include <assert.h>
167 #include <stdio.h>
168 #include <string.h>
169
170
171 int
172 main ()
173 {
174     gene_t* gene1Ptr;
175     gene_t* gene2Ptr;
176     gene_t* gene3Ptr;
177     random_t* randomPtr;
178
179     bool_t status = memory_init(1, 4, 2);
180     assert(status);
181
182     puts("Starting...");
183
184     gene1Ptr = gene_alloc(10);
185     gene2Ptr = gene_alloc(10);
186     gene3Ptr = gene_alloc(9);
187     randomPtr = random_alloc();
188
189     random_seed(randomPtr, 0);
190     gene_create(gene1Ptr, randomPtr);
191     random_seed(randomPtr, 1);
192     gene_create(gene2Ptr, randomPtr);
193     random_seed(randomPtr, 0);
194     gene_create(gene3Ptr, randomPtr);
195
196     assert(gene1Ptr->length == strlen(gene1Ptr->contents));
197     assert(gene2Ptr->length == strlen(gene2Ptr->contents));
198     assert(gene3Ptr->length == strlen(gene3Ptr->contents));
199
200     assert(gene1Ptr->length == gene2Ptr->length);
201     assert(strcmp(gene1Ptr->contents, gene2Ptr->contents) != 0);
202
203     assert(gene1Ptr->length == (gene3Ptr->length + 1));
204     assert(strcmp(gene1Ptr->contents, gene3Ptr->contents) != 0);
205     assert(strncmp(gene1Ptr->contents,
206                    gene3Ptr->contents,
207                    gene3Ptr->length) == 0);
208
209     gene_free(gene1Ptr);
210     gene_free(gene2Ptr);
211     gene_free(gene3Ptr);
212     random_free(randomPtr);
213
214     puts("All tests passed.");
215
216     return 0;
217 }
218
219
220 #endif /* TEST_GENE */
221
222
223 /* =============================================================================
224  *
225  * End of gene.c
226  *
227  * =============================================================================
228  */